1 from datetime import datetime |
1 from datetime import datetime |
2 |
2 |
3 from django.http import HttpResponse |
3 from django.http import HttpResponse |
4 from django.shortcuts import render_to_response, redirect |
4 from django.shortcuts import render_to_response, redirect |
5 |
5 |
6 from pytask.taskapp.models import User, Task, Comment, Claim, Credit |
6 from pytask.taskapp.models import User, Task, Comment, Claim, Credit, Request |
7 from pytask.taskapp.utilities.task import getTask |
7 from pytask.taskapp.utilities.task import getTask |
8 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm |
8 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm |
9 from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask |
9 from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask |
10 from pytask.taskapp.views.user import show_msg |
10 from pytask.taskapp.views.user import show_msg |
11 from pytask.taskapp.utilities.user import get_user |
11 from pytask.taskapp.utilities.user import get_user |
157 errors = [] |
157 errors = [] |
158 |
158 |
159 is_guest = True if not user.is_authenticated() else False |
159 is_guest = True if not user.is_authenticated() else False |
160 |
160 |
161 if (not is_guest) and user in task.mentors.all(): |
161 if (not is_guest) and user in task.mentors.all(): |
162 |
162 |
|
163 pending_requests = Request.objects.filter(is_replied=False,is_valid=True,role="MT",task=task).order_by('creation_date').reverse() |
|
164 user_pending_requests = pending_requests.filter(sent_by=user) |
|
165 |
163 ## now iam going for a brute force method |
166 ## now iam going for a brute force method |
164 user_list = list(User.objects.filter(is_active=True)) |
167 user_list = list(User.objects.filter(is_active=True)) |
165 for mentor in task.mentors.all(): |
168 for mentor in task.mentors.all(): |
166 user_list.remove(mentor) |
169 user_list.remove(mentor) |
167 |
170 |
168 for a_user in task.claimed_users.all(): |
171 for a_user in task.claimed_users.all(): |
169 user_list.remove(a_user) |
172 user_list.remove(a_user) |
170 |
173 |
171 for a_user in task.assigned_users.all(): |
174 for a_user in task.assigned_users.all(): |
172 user_list.remove(a_user) |
175 user_list.remove(a_user) |
|
176 |
|
177 for req in user_pending_requests: |
|
178 user_list.remove(req.sent_to.all()[0]) |
173 |
179 |
174 non_mentors = ((_.id,_.username) for _ in user_list) |
180 non_mentors = ((_.id,_.username) for _ in user_list) |
175 ## code till must be made elegant and not brute force like above |
181 ## code till must be made elegant and not brute force like above |
176 |
182 |
177 form = AddMentorForm(non_mentors) |
183 form = AddMentorForm(non_mentors) |
178 if request.method == "POST": |
184 if request.method == "POST": |
179 uid = request.POST['mentor'] |
185 uid = request.POST['mentor'] |
180 new_mentor = User.objects.get(id=uid) |
186 new_mentor = User.objects.get(id=uid) |
181 reqMentor(task, new_mentor, user) |
187 reqMentor(task, new_mentor, user) |
182 return redirect(task_url) |
188 return redirect(task_url) |
183 else: |
189 else: |
184 return render_to_response('task/addmentor.html', {'user':user,'form':form, 'errors':errors}) |
190 return render_to_response('task/addmentor.html', {'user':user,'pending_requests':pending_requests,'form':form, 'errors':errors}) |
185 |
191 |
186 else: |
192 else: |
187 return show_msg(user, 'You are not authorised to add mentors for this task', task_url, 'view the task') |
193 return show_msg(user, 'You are not authorised to add mentors for this task', task_url, 'view the task') |
188 |
194 |
189 def add_tasks(request, tid): |
195 def add_tasks(request, tid): |