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