created forms, views, templates, actions for home_page, browse_task.
authornishanth
Thu, 04 Feb 2010 22:12:48 +0530
changeset 12 a93eebabfeb1
parent 11 7c9fea7e28c8
child 13 0fb64b24a1c9
created forms, views, templates, actions for home_page, browse_task.
settings.py
taskapp/events/__init__.py
taskapp/events/user.py
taskapp/forms/__init__.py
taskapp/forms/task.py
taskapp/forms/user.py
taskapp/models.py
taskapp/views/tasks.py
taskapp/views/users.py
templates/base.html
templates/index.html
templates/task/browse.html
templates/task/create.html
templates/task/view.html
urls.py
--- a/settings.py	Thu Feb 04 22:06:22 2010 +0530
+++ b/settings.py	Thu Feb 04 22:12:48 2010 +0530
@@ -81,4 +81,4 @@
     'pytask.taskapp',
 )
 
-AUTH_PROFILE_MODULE = 'taskapp.models.Profile'
+AUTH_PROFILE_MODULE = 'taskapp.Profile'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/taskapp/events/user.py	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,43 @@
+from django.contrib.auth.models import User
+from pytask.taskapp.models import Profile, Task, Comment, Credit
+
+""" A collection of helper methods. note that there is no validation done here.
+we take care of validation and others checks in methods that invoke these methods.
+"""
+
+def updateProfile(user_profile, properties):
+    """ updates the given properties in the profile for a user. 
+    args:
+        user_profile : a profile object
+        properties : a dictionary with attributes to set as keys and corresponding values
+    """
+    
+    for attr,value in properties.items():
+        user_profile.__setattr__(attr,value)
+    user_profile.save()
+
+def createUser(username,email,password,dob,gender):
+    """ create a user and create a profile and update its properties 
+    args:
+        username : a username that does not exist
+        email : a valid email
+        password : a password
+        dob : a date object
+        gender : u'M'/u'F' 
+    """
+
+    user = User(username=username, email=email)
+    user.set_password(password)
+    user.save()
+    properties = {'dob':dob, 'gender':gender}
+    user_profile = Profile(user=user)
+    updateProfile(user_profile, properties)
+    return user
+    
+def createSuUser(username,email,password,**properties):
+    """ create user using createUser method and set the is_superuser flag """
+    
+    su_user = createUser(username,email,password,**properties)
+    su_user.is_staff = True
+    su_user.is_superuser = True
+    su_user.save()
--- a/taskapp/models.py	Thu Feb 04 22:06:22 2010 +0530
+++ b/taskapp/models.py	Thu Feb 04 22:12:48 2010 +0530
@@ -24,12 +24,12 @@
 class Profile(models.Model):
 	
     user = models.ForeignKey(User, unique = True)
-    aboutme = models.TextField()
-    dob = models.DateField()
+    dob = models.DateField(help_text = "YYYY-MM-DD")
     gender = models.CharField(max_length = 1, choices = GENDER_CHOICES)
-    rights = models.CharField(max_length = 2, choices = RIGHTS_CHOICES)
-    credits = models.PositiveSmallIntegerField()
+    rights = models.CharField(max_length = 2, choices = RIGHTS_CHOICES, default = u"CT")
+    credits = models.PositiveSmallIntegerField(default = 0)
     
+    aboutme = models.TextField(blank = True)
     foss_comm = models.CharField(max_length = 80, blank = True)
     phonenum = models.CharField(max_length = 15, blank = True)
     homepage = models.URLField(blank = True)
@@ -63,6 +63,8 @@
     
     creation_datetime = models.DateTimeField()
     
+    #is_claimable = models.BooleanField()
+    
     ## not yet decided if attribs after this are to be included
     ## tasktype = "" ## "bugfix"/"enhancement"
     ## priority = "" ## "very urgent"/"urgent"
--- a/taskapp/views/tasks.py	Thu Feb 04 22:06:22 2010 +0530
+++ b/taskapp/views/tasks.py	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,52 @@
+from datetime import datetime
+
+from django.http import HttpResponse
+from django.shortcuts import render_to_response, redirect
+from pytask.taskapp.models import Task, Comment
+
+def browse_tasks(request):
+    """ display all the tasks """
+    
+    user = request.user
+    task_list = Task.objects.order_by('id').reverse()
+    
+    context = {'user':user,
+               'task_list':task_list,
+               }
+    return render_to_response('task/browse.html', context)
+
+def view_task(request, tid):
+    """ get the task depending on its tid and display accordingly if it is a get.
+    check for authentication and add a comment if it is a post request.
+    """
+    
+    task_url = "/task/view/tid=%s"%tid
+    
+    user = request.user
+    task = Task.objects.get(id=tid)
+    comments = Comment.objects.filter(task=task)
+    errors = []
+    
+    is_guest = True if not user.is_authenticated() else False
+    is_mentor = True if user in task.mentors.all() else False
+    
+    context = {'user':user,
+               'task':task,
+               'comments':comments,
+               'is_guest':is_guest,
+               'is_mentor':is_mentor,
+               'errors':errors,
+               }
+    
+    if request.method == 'POST':
+        if not is_guest:
+            data = request.POST["data"]
+            task = Task.objects.get(id=tid)
+            new_comment = Comment(task=task, data=data, created_by=user, creation_datetime=datetime.now())
+            new_comment.save()
+            return redirect(task_url)
+        else:
+            errors.append("You must be logged in to post a comment")
+            return render_to_response('task/view.html', context)
+    else:
+        return render_to_response('task/view.html', context)
--- a/taskapp/views/users.py	Thu Feb 04 22:06:22 2010 +0530
+++ b/taskapp/views/users.py	Thu Feb 04 22:12:48 2010 +0530
@@ -1,1 +1,41 @@
+from django.http import HttpResponse
+from django.shortcuts import redirect, render_to_response
+from pytask.taskapp.models import Task
 
+def redirect_to_homepage(request):
+    """ simply redirect to homepage """
+    
+    return redirect('/')
+
+def homepage(request):
+    """ check for authentication and display accordingly. """
+    
+    user = request.user
+    is_guest = False
+    is_mentor = False
+    can_create_task = False
+    task_list = []
+    
+    if not user.is_authenticated():
+        is_guest = True
+        disp_num = 10
+        tasks_count = Task.objects.count()
+        if tasks_count <= disp_num:
+            task_list = Task.objects.order_by('id').reverse()
+        else:
+            task_list = Task.objects.order_by('id').reverse()[:10]
+    else:
+        user_profile = user.get_profile()
+        is_mentor = True if user.task_mentors.all() else False
+        can_create_task = False if user_profile.rights == u"CT" else True
+        
+    context = {'user':user,
+               'is_guest':is_guest,
+               'is_mentor':is_mentor,
+               'task_list':task_list,
+               'can_create_task':can_create_task,
+               }
+               
+    return render_to_response('index.html', context)
+               
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/base.html	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,7 @@
+<html>
+<head>
+    <title>{% block title %}PyTasks{% endblock %}</title>
+</head>
+<body>
+<h2><a href="/">PyTasks</a></h2><br />{% block content %}This is the default content{% endblock %}</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/index.html	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,22 @@
+{% extends 'base.html' %}
+{% block content %}
+    {% if is_guest %}
+        Welcome Guest<br>
+        <a href="/accounts/register/">Register</a>
+        <a href="/accounts/login/">Login</a><br /><br />
+        Recent Tasks:<br />
+        {% for task in task_list %}
+            <a href="/task/view/tid={{ task.id }}">{{ task.title }}</a><br />
+        {% endfor %}
+    {% else %}
+        Welcome {{ user.username }} <br />
+        <a href="/accounts/logout/">logout</a><br />
+        <br />
+        <a href="/task/browse/">Tasks</a><br />
+        <a href="/user/browse/">Users</a><br />
+        <a href="/user/view/uid={{user.id}}">My Profile</a><br />
+    {% endif %}
+    {% if can_create_task %}
+        <a href="/task/create/">Create a task</a><br />
+    {% endif %}
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/task/browse.html	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+{% block content %}
+    List of all the tasks:<br />
+    {% for task in task_list %}
+        <a href="/task/view/tid={{ task.id }}">{{ task.title }}</a><br />
+    {% endfor %}
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/task/create.html	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,2 @@
+{% extends 'base.html' %}
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/task/view.html	Thu Feb 04 22:12:48 2010 +0530
@@ -0,0 +1,28 @@
+{% extends 'base.html' %}
+{% block title %}
+    {{task.title}}
+{% endblock %}
+{% block content %}
+    <a href="/task/browse/pg=1">Browse tasks</a>
+    <h3>{{ task.title }}</h3><br />
+    <!-- we have to write our own datetime.strftime filter and use in the next line -->
+    created by <a href="/user/view/uid={{ task.created_by.id }}">{{ task.created_by.username }}</a> on {{ task.creation_datetime.ctime }}<br />
+    <br />Description:<br />
+    <br />{{ task.desc }}<br />
+    {% if comments %}
+        <br/>comments:<br />
+        {% for comment in comments %}
+            <br /><a href="/user/view/uid={{comment.created_by.id}}">{{ comment.created_by.username }}</a> at {{ comment.creation_datetime.ctime }} wrote:<br />
+        {{ comment.data }}<br />
+        {% endfor %}
+    {% endif %}
+    
+    {% if not is_guest %}
+        <br />Add comment:<br />
+        <form action="" method="post">
+        <!-- we might even want to use forms here -->
+        <textarea  name="data"></textarea><br />
+        <input type="submit" value="Submit">
+        </form>
+    {% endif %}
+{% endblock %}
--- a/urls.py	Thu Feb 04 22:06:22 2010 +0530
+++ b/urls.py	Thu Feb 04 22:12:48 2010 +0530
@@ -4,6 +4,9 @@
 from django.contrib import admin
 admin.autodiscover()
 
+from pytask.taskapp.views.users import redirect_to_homepage, homepage
+from pytask.taskapp.views.tasks import browse_tasks, view_task
+
 urlpatterns = patterns('',
     # Example:
     # (r'^pytask/', include('pytask.foo.urls')),
@@ -12,6 +15,10 @@
     # to INSTALLED_APPS to enable admin documentation:
     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
     
+    (r'^$', homepage),
+    (r'^task/browse/$', browse_tasks),
+    (r'^task/view/tid=(\d+)', view_task),
+    
     (r'^admin/', include(admin.site.urls)),
 
 )