taskapp/views/task.py
changeset 93 1a1e712e60fd
parent 92 c99f09bebe56
child 94 d1f59bbc2685
equal deleted inserted replaced
92:c99f09bebe56 93:1a1e712e60fd
     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
     6 from pytask.taskapp.models import User, Task, Comment, Claim, Credit
     7 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm
     7 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm
     8 from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addDep, addClaim, assignTask, getTask, updateTask, removeTask
     8 from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addDep, addClaim, assignTask, getTask, updateTask, removeTask
     9 from pytask.taskapp.views.user import show_msg
     9 from pytask.taskapp.views.user import show_msg
    10 
    10 
    11 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
    11 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
    12 ## do not create su user thro syncdb
    12 ## do not create su user thro syncdb
   336             return show_msg('When the no of users you need for the task is more than the no of users willing to do the task, I\'d say please re consider the task :P',task_url, 'view the task')
   336             return show_msg('When the no of users you need for the task is more than the no of users willing to do the task, I\'d say please re consider the task :P',task_url, 'view the task')
   337         else:
   337         else:
   338             return show_msg('Wait for ppl to claim dude... slow and steady wins the race :)', task_url, 'view the task')
   338             return show_msg('Wait for ppl to claim dude... slow and steady wins the race :)', task_url, 'view the task')
   339     else:
   339     else:
   340         return show_msg('You are not authorised to perform this action', task_url, 'view the task')
   340         return show_msg('You are not authorised to perform this action', task_url, 'view the task')
   341         
   341 
       
   342 def assign_credits(request, tid):
       
   343     """ Check if the user is a mentor and credits can be assigned.
       
   344     Then display all the approved credits.
       
   345     Then see if mentor can assign credits to users also or only mentors.
       
   346     Then put up a form for mentor to assign credits accordingly.
       
   347     """
       
   348     
       
   349     task_url = "/task/view/tid=%s"%tid
       
   350     
       
   351     user = request.user
       
   352     task = getTask(tid)
       
   353 
       
   354     is_guest = True if not user.is_authenticated() else False
       
   355     is_mentor = True if (not is_guest) and user in task.mentors.all() else False
       
   356 
       
   357     if is_mentor:
       
   358         if task.status in ["OP", "WR"]:
       
   359             choices = [(_.id,_.username) for _ in task.mentors.all()]
       
   360             if task.status == "WR":
       
   361                 choices.extend([(_.id, _.username) for _  in task.assigned_users.all() ])
       
   362             prev_credits = task.credit_set.all()
       
   363             ## here we can ditchax credits model and use the request model
       
   364             form = AssignCreditForm(choices)
       
   365 
       
   366             context = {
       
   367                 'user':user,
       
   368                 'prev_credits':prev_credits,
       
   369                 'form':form,
       
   370             }
       
   371 
       
   372             if request.method == "POST":
       
   373                 data = request.POST
       
   374                 form = AssignCreditForm(choices, data)
       
   375                 if form.is_valid():
       
   376                     data = form.cleaned_data
       
   377                     uid = data['user']
       
   378                     points = data['points']
       
   379                     given_to = User.objects.get(id=uid)
       
   380                     given_time = datetime.now()
       
   381                     creditobj = Credit(task=task, given_by=user, given_to=given_to,points=points,given_time=given_time)
       
   382                     ## remove the next line and add a request here
       
   383                     creditobj.save()
       
   384                     return redirect('/task/assigncredits/tid=%s'%task.id)
       
   385                 else:
       
   386                     context['form'] = form
       
   387                     return render_to_response('task/assigncredits.html', context)
       
   388             else:
       
   389                 return render_to_response('task/assigncredits.html', context)
       
   390         else:
       
   391             return show_msg("Credits cannot be assigned at this stage", task_url, "view the task")
       
   392     else:
       
   393         return show_msg("You are not authorised to perform this action", task_url, "view the task")
       
   394 
   342 def edit_task(request, tid):
   395 def edit_task(request, tid):
   343     """ see what are the attributes that can be edited depending on the current status
   396     """ see what are the attributes that can be edited depending on the current status
   344     and then give the user fields accordingly.
   397     and then give the user fields accordingly.
   345     """
   398     """
   346     
   399