author | nishanth |
Thu, 04 Feb 2010 22:12:48 +0530 | |
changeset 12 | a93eebabfeb1 |
parent 10 | bf0cbea1bd12 |
permissions | -rw-r--r-- |
12
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
1 |
from datetime import datetime |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
2 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
3 |
from django.http import HttpResponse |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
4 |
from django.shortcuts import render_to_response, redirect |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
5 |
from pytask.taskapp.models import Task, Comment |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
6 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
7 |
def browse_tasks(request): |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
8 |
""" display all the tasks """ |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
9 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
10 |
user = request.user |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
11 |
task_list = Task.objects.order_by('id').reverse() |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
12 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
13 |
context = {'user':user, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
14 |
'task_list':task_list, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
15 |
} |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
16 |
return render_to_response('task/browse.html', context) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
17 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
18 |
def view_task(request, tid): |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
19 |
""" get the task depending on its tid and display accordingly if it is a get. |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
20 |
check for authentication and add a comment if it is a post request. |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
21 |
""" |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
22 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
23 |
task_url = "/task/view/tid=%s"%tid |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
24 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
25 |
user = request.user |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
26 |
task = Task.objects.get(id=tid) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
27 |
comments = Comment.objects.filter(task=task) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
28 |
errors = [] |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
29 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
30 |
is_guest = True if not user.is_authenticated() else False |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
31 |
is_mentor = True if user in task.mentors.all() else False |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
32 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
33 |
context = {'user':user, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
34 |
'task':task, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
35 |
'comments':comments, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
36 |
'is_guest':is_guest, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
37 |
'is_mentor':is_mentor, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
38 |
'errors':errors, |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
39 |
} |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
40 |
|
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
41 |
if request.method == 'POST': |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
42 |
if not is_guest: |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
43 |
data = request.POST["data"] |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
44 |
task = Task.objects.get(id=tid) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
45 |
new_comment = Comment(task=task, data=data, created_by=user, creation_datetime=datetime.now()) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
46 |
new_comment.save() |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
47 |
return redirect(task_url) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
48 |
else: |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
49 |
errors.append("You must be logged in to post a comment") |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
50 |
return render_to_response('task/view.html', context) |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
51 |
else: |
a93eebabfeb1
created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
10
diff
changeset
|
52 |
return render_to_response('task/view.html', context) |