taskapp/views/task.py
changeset 219 f04a1ec7a07f
parent 218 59107ce0a618
child 220 807e4b701a20
equal deleted inserted replaced
218:59107ce0a618 219:f04a1ec7a07f
     3 from django.http import HttpResponse, Http404
     3 from django.http import HttpResponse, Http404
     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, Request, Notification
     6 from pytask.taskapp.models import User, Task, Comment, Request, Notification
     7 from pytask.taskapp.utilities.task import getTask
     7 from pytask.taskapp.utilities.task import getTask
     8 from pytask.taskapp.forms.task import TaskCreateForm, AddReviewerForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm, EditTaskForm, ClaimTaskForm
     8 from pytask.taskapp.forms.task import TaskCreateForm, AddReviewerForm, AddTaskForm, ChoiceForm, AssignPyntForm, RemoveUserForm, EditTaskForm, ClaimTaskForm
     9 from pytask.taskapp.events.task import createTask, reqReviewer, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask, addReviewer, deleteTask
     9 from pytask.taskapp.events.task import createTask, reqReviewer, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignPynts, completeTask, closeTask, addReviewer, deleteTask
    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
    12 
    12 
    13 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add reviewer and all
    13 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add reviewer and all
    14 ## do not create su user thro syncdb
    14 ## do not create su user thro syncdb
   111     context['can_delete'] = True if task.status == "UP" and user == task.created_by else False
   111     context['can_delete'] = True if task.status == "UP" and user == task.created_by else False
   112 
   112 
   113     context['can_mod_reviewers'] = True if task.status in ["UP", "OP", "LO", "WR"] and is_reviewer else False
   113     context['can_mod_reviewers'] = True if task.status in ["UP", "OP", "LO", "WR"] and is_reviewer else False
   114     context['can_mod_tasks'] = True if task.status in ["UP", "OP", "LO"] and is_reviewer else False
   114     context['can_mod_tasks'] = True if task.status in ["UP", "OP", "LO"] and is_reviewer else False
   115 
   115 
   116     context['can_assign_credits'] = True if task.status in ["OP", "WR"] and is_reviewer else False
   116     context['can_assign_pynts'] = True if task.status in ["OP", "WR"] and is_reviewer else False
   117     context['task_claimable'] = True if task.status in ["OP", "WR"] and not is_guest else False
   117     context['task_claimable'] = True if task.status in ["OP", "WR"] and not is_guest else False
   118 
   118 
   119     if task.status == "CD":
   119     if task.status == "CD":
   120         context['closing_notification'] =  Notification.objects.filter(task=task,role="CD")[0]
   120         context['closing_notification'] =  Notification.objects.filter(task=task,role="CD")[0]
   121     elif task.status == "CM":
   121     elif task.status == "CM":
   154                 form = TaskCreateForm(request.POST)
   154                 form = TaskCreateForm(request.POST)
   155                 if form.is_valid():
   155                 if form.is_valid():
   156                     data = form.cleaned_data
   156                     data = form.cleaned_data
   157                     title = data['title']
   157                     title = data['title']
   158                     desc = data['desc']
   158                     desc = data['desc']
   159                     credits = data['credits']
   159                     pynts = data['pynts']
   160                     #publish = data['publish'] # just in case if we have to show the option
   160                     #publish = data['publish'] # just in case if we have to show the option
   161                     task = createTask(title,desc,user,credits)
   161                     task = createTask(title,desc,user,pynts)
   162                     
   162                     
   163                     addReviewer(task, user)
   163                     addReviewer(task, user)
   164                     updateTask(task,tags_field=data['tags_field'])
   164                     updateTask(task,tags_field=data['tags_field'])
   165                     # if publish: publishTask(task)    
   165                     # if publish: publishTask(task)    
   166                     task_url = '/task/view/tid=%s'%task.id
   166                     task_url = '/task/view/tid=%s'%task.id
   484         else: 
   484         else: 
   485             return show_msg(user, "The task cannot be assigned to users at this stage", task_url, 'view the task')
   485             return show_msg(user, "The task cannot be assigned to users at this stage", task_url, 'view the task')
   486     else:
   486     else:
   487         return show_msg(user, 'You are not authorised to perform this action', task_url, 'view the task')
   487         return show_msg(user, 'You are not authorised to perform this action', task_url, 'view the task')
   488 
   488 
   489 def assign_credits(request, tid):
   489 def assign_pynts(request, tid):
   490     """ Check if the user is a reviewer and credits can be assigned.
   490     """ Check if the user is a reviewer and pynts can be assigned.
   491     Then display all the approved credits.
   491     Then display all the approved pynts.
   492     Then see if reviewer can assign credits to users also or only reviewers.
   492     Then see if reviewer can assign pynts to users also or only reviewers.
   493     Then put up a form for reviewer to assign credits accordingly.
   493     Then put up a form for reviewer to assign pynts accordingly.
   494     """
   494     """
   495     
   495     
   496     task_url = "/task/view/tid=%s"%tid
   496     task_url = "/task/view/tid=%s"%tid
   497     
   497     
   498     user = get_user(request.user) if request.user.is_authenticated() else request.user
   498     user = get_user(request.user) if request.user.is_authenticated() else request.user
   499     task = getTask(tid)
   499     task = getTask(tid)
   500 
   500 
   501     ## the moment we see that user had requested credits, it means he had worked and hence we change the status to WR
   501     ## the moment we see that user had requested pynts, it means he had worked and hence we change the status to WR
   502     ## we have to discuss on this since, credits may also be given to reviewer
   502     ## we have to discuss on this since, pynts may also be given to reviewer
   503     task.status = "WR"
   503     task.status = "WR"
   504     task.save()
   504     task.save()
   505 
   505 
   506     is_guest = True if not user.is_authenticated() else False
   506     is_guest = True if not user.is_authenticated() else False
   507     is_reviewer = True if (not is_guest) and user in task.reviewers.all() else False
   507     is_reviewer = True if (not is_guest) and user in task.reviewers.all() else False
   509     if is_reviewer:
   509     if is_reviewer:
   510         if task.status in ["OP", "WR"]:
   510         if task.status in ["OP", "WR"]:
   511             choices = [(_.id,_.username) for _ in task.reviewers.all()]
   511             choices = [(_.id,_.username) for _ in task.reviewers.all()]
   512             if task.status == "WR":
   512             if task.status == "WR":
   513                 choices.extend([(_.id, _.username) for _  in task.assigned_users.all() ])
   513                 choices.extend([(_.id, _.username) for _  in task.assigned_users.all() ])
   514             prev_credits = task.request_task.filter(role="PY",is_valid=True,is_replied=True,reply=True).count()
   514             prev_pynts = task.request_task.filter(role="PY",is_valid=True,is_replied=True,reply=True).count()
   515             credit_requests = task.request_task.filter(role="PY",is_valid=True).order_by('creation_date').reverse()
   515             pynt_requests = task.request_task.filter(role="PY",is_valid=True).order_by('creation_date').reverse()
   516             form = AssignCreditForm(choices)
   516             form = AssignPyntForm(choices)
   517 
   517 
   518             context = {
   518             context = {
   519                 'user':user,
   519                 'user':user,
   520                 'task':task,
   520                 'task':task,
   521                 'prev_credits':prev_credits,
   521                 'prev_pynts':prev_pynts,
   522                 'credit_requests':credit_requests,
   522                 'pynt_requests':pynt_requests,
   523                 'form':form,
   523                 'form':form,
   524             }
   524             }
   525 
   525 
   526             if request.method == "POST":
   526             if request.method == "POST":
   527                 data = request.POST
   527                 data = request.POST
   528                 form = AssignCreditForm(choices, data)
   528                 form = AssignPyntForm(choices, data)
   529                 if form.is_valid():
   529                 if form.is_valid():
   530                     data = form.cleaned_data
   530                     data = form.cleaned_data
   531                     uid = data['user']
   531                     uid = data['user']
   532                     points = data['pynts']
   532                     points = data['pynts']
   533                     given_to = User.objects.get(id=uid)
   533                     given_to = User.objects.get(id=uid)
   534                     assignCredits(task=task, given_by=user, given_to=given_to, points=points)
   534                     assignPynts(task=task, given_by=user, given_to=given_to, points=points)
   535                     return redirect('/task/assigncredits/tid=%s'%task.id)
   535                     return redirect('/task/assignpynts/tid=%s'%task.id)
   536                 else:
   536                 else:
   537                     context['form'] = form
   537                     context['form'] = form
   538                     return render_to_response('task/assigncredits.html', context)
   538                     return render_to_response('task/assignpynts.html', context)
   539             else:
   539             else:
   540                 return render_to_response('task/assigncredits.html', context)
   540                 return render_to_response('task/assignpynts.html', context)
   541         else:
   541         else:
   542             return show_msg(user, "Credits cannot be assigned at this stage", task_url, "view the task")
   542             return show_msg(user, "Pynts cannot be assigned at this stage", task_url, "view the task")
   543     else:
   543     else:
   544         return show_msg(user, "You are not authorised to perform this action", task_url, "view the task")
   544         return show_msg(user, "You are not authorised to perform this action", task_url, "view the task")
   545 
   545 
   546 def edit_task(request, tid):
   546 def edit_task(request, tid):
   547     """ see what are the attributes that can be edited depending on the current status
   547     """ see what are the attributes that can be edited depending on the current status
   585     is_reviewer = True if user in task.reviewers.all() else False
   585     is_reviewer = True if user in task.reviewers.all() else False
   586 
   586 
   587     claimed_users = task.claimed_users.all()
   587     claimed_users = task.claimed_users.all()
   588     assigned_users = task.assigned_users.all()
   588     assigned_users = task.assigned_users.all()
   589 
   589 
   590     assign_credits_url = '/task/assigncredits/tid=%s'%task.id
   590     assign_pynts_url = '/task/assignpynts/tid=%s'%task.id
   591     task_assigned_credits = task.credit_set.all()
   591     task_assigned_pynts = task.pynt_set.all()
   592 
   592 
   593 
   593 
   594     if is_reviewer:
   594     if is_reviewer:
   595         if task.status in ["OP", "WR"]:
   595         if task.status in ["OP", "WR"]:
   596 
   596 
   597             context = {
   597             context = {
   598                 'user':user,
   598                 'user':user,
   599                 'task':task,
   599                 'task':task,
   600             }
   600             }
   601 
   601 
   602             if task_assigned_credits:
   602             if task_assigned_pynts:
   603                 if request.method=="POST":
   603                 if request.method=="POST":
   604                     completeTask(task, user)
   604                     completeTask(task, user)
   605                     return redirect(task_url)
   605                     return redirect(task_url)
   606                 else:
   606                 else:
   607                     return render_to_response('task/complete.html', context)
   607                     return render_to_response('task/complete.html', context)
   608             else:
   608             else:
   609                 return show_msg(user, "Nobody has been credited for doing this task.", assign_credits_url, "assign credits")
   609                 return show_msg(user, "Nobody has been pynted for doing this task.", assign_pynts_url, "assign pynts")
   610         else:
   610         else:
   611             return show_msg(user, "The task cannot be marked as completed at this stage", task_url, "view the task")
   611             return show_msg(user, "The task cannot be marked as completed at this stage", task_url, "view the task")
   612     else:
   612     else:
   613         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
   613         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
   614 
   614