1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from pytask.taskapp.models import User, Task, Comment, Claim
from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm
from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask
from pytask.taskapp.views.user import show_msg
## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
## do not create su user thro syncdb
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)
mentors = task.mentors.all()
errors = []
is_guest = True if not user.is_authenticated() else False
is_mentor = True if user in task.mentors.all() else False
task_claimable = True if task.status in ["OP", "RE"] else False
user_can_view_claim = True if ( task_claimable and not ( is_guest or is_mentor ) ) else False
context = {'user':user,
'task':task,
'comments':comments,
'mentors':mentors,
'is_guest':is_guest,
'is_mentor':is_mentor,
'user_can_view_claim':user_can_view_claim,
'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)
def create_task(request):
""" check for rights and create a task if applicable.
if user cannot create a task, redirect to homepage.
"""
user = request.user
is_guest = True if not user.is_authenticated() else False
if not is_guest:
user_profile = user.get_profile()
can_create_task = False if user_profile.rights == "CT" else True
if can_create_task:
if request.method == "POST":
form = TaskCreateForm(request.POST)
if form.is_valid():
data = form.cleaned_data
title = data['title']
desc = data['desc']
credits = data['credits']
publish = data['publish']
task = createTask(title,desc,user,credits)
if not task:
error_msg = "Another task with the same title exists"
return render_to_response('task/create.html',{'form':form, 'error_msg':error_msg})
addMentor(task, user)
if publish: publishTask(task)
task_url = '/task/view/tid=%s'%task.id
return redirect(task_url)
else:
return render_to_response('task/create.html',{'form':form})
else:
form = TaskCreateForm()
return render_to_response('task/create.html',{'form':form})
else:
return show_msg('You are not authorised to create a task.')
else:
return show_msg('You are not authorised to create a task.')
def add_mentor(request, tid):
""" check if the current user has the rights to edit the task and add him.
if user is not authenticated, redirect him to concerned page. """
task_url = "/task/view/tid=%s"%tid
user = request.user
task = Task.objects.get(id=tid)
errors = []
is_guest = True if not user.is_authenticated() else False
if (not is_guest) and user in task.mentors.all():
## now iam going for a brute force method
user_list = list(User.objects.all())
for mentor in task.mentors.all():
user_list.remove(mentor)
non_mentors = ((_.id,_.username) for _ in user_list)
form = AddMentorForm(non_mentors)
if request.method == "POST":
uid = request.POST['mentor']
new_mentor = User.objects.get(id=uid)
addMentor(task, new_mentor)
return redirect(task_url)
else:
return render_to_response('task/addmentor.html', {'form':form, 'errors':errors})
else:
return show_msg('You are not authorised to add mentors for this task', task_url, 'view the task')
def add_tasks(request, tid):
""" first display tasks which can be subtasks for the task and do the rest.
"""
task_url = "/task/view/tid=%s"%tid
user = request.user
task = Task.objects.get(id=tid)
errors = []
is_guest = True if not user.is_authenticated() else False
if (not is_guest) and user in task.mentors.all():
if task.status in ["OP", "LO"]:
if request.method == "POST":
## first decide if adding subs and deps can be in same page
## only exclude tasks with status deleted
pass
else:
## write a form just like add mentor and get the form here
pass
else:
errors = ["The task cannot be added subtasks or dependencies in this state"]
# return render_to_response('task/add.html', {'form':form, 'errors':errors})
return show_msg('The task cannot be added subtasks or dependencies now', task_url, 'view the task')
else:
return show_msg('You are not authorised to add subtasks or dependencies for this task', task_url, 'view the task')
def claim_task(request, tid):
""" display a list of claims for get and display submit only if claimable """
## create claims model and create a new database with required tables for it
## see if that "one to n" or "n to one" relationship has a special field
task_url = "/task/view/tid=%s"%tid
user = request.user
task = Task.objects.get(id=tid)
claims = Claim.objects.filter(task=task)
is_guest = True if not user.is_authenticated() else False
task_claimable = True if task.status in ["OP", "RE"] else False
user_can_claim = True if ( task_claimable and not ( is_guest or is_mentor ) and ( user not in task.claimed_users.all() ) ) else False
if not is_guest:
if task_claimable:
pass
else:
return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
else:
return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
|