taskapp/views/task.py
branchbuildout
changeset 227 3c8f3b0e5b00
parent 214 679c7e237052
child 228 81994e525e69
equal deleted inserted replaced
214:679c7e237052 227:3c8f3b0e5b00
     1 from datetime import datetime
       
     2 
       
     3 from django.http import HttpResponse, Http404
       
     4 from django.shortcuts import render_to_response, redirect
       
     5 
       
     6 from pytask.taskapp.models import User, Task, Comment, Request, Notification
       
     7 from pytask.taskapp.utilities.task import getTask
       
     8 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm, EditTaskForm, ClaimTaskForm
       
     9 from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask, addMentor, deleteTask
       
    10 from pytask.taskapp.views.user import show_msg
       
    11 from pytask.taskapp.utilities.user import get_user
       
    12 
       
    13 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
       
    14 ## do not create su user thro syncdb
       
    15 
       
    16 def browse_tasks(request):
       
    17     """ display all the tasks """
       
    18     
       
    19     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
    20     task_list = Task.objects.exclude(status="UP").exclude(status="DL").order_by('published_datetime').reverse()
       
    21     
       
    22     context = {'user':user,
       
    23                'task_list':task_list,
       
    24                }
       
    25     return render_to_response('task/browse.html', context)
       
    26 
       
    27 def publish_task(request, tid):
       
    28     """ check if user is the mentor and also if the task status is UP.
       
    29     """
       
    30 
       
    31     task_url = "/task/view/tid=%s"%tid
       
    32     
       
    33     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
    34     task = getTask(tid)
       
    35 
       
    36     is_guest = True if not user.is_authenticated() else False
       
    37     is_mentor = True if user in task.mentors.all() else False
       
    38 
       
    39     if user == task.created_by:
       
    40         context = {
       
    41             'user':user,
       
    42         }
       
    43         if task.status == "UP":
       
    44             if request.method == "POST":
       
    45                 publishTask(task)
       
    46                 return show_msg(user, "The task has been published", task_url, "view the task")
       
    47             else:
       
    48                 return render_to_response('task/publish.html', context)
       
    49         else:
       
    50             return show_msg(user, "The task is already published", task_url, "view the task")
       
    51     else:
       
    52         return show_msg(user, "You are not authorised to do this", '/task/browse/', "browse tasks")
       
    53 
       
    54 def view_task(request, tid):
       
    55     """ get the task depending on its tid and display accordingly if it is a get.
       
    56     check for authentication and add a comment if it is a post request.
       
    57     """
       
    58     
       
    59     task_url = "/task/view/tid=%s"%tid
       
    60     
       
    61     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
    62     task = getTask(tid)
       
    63 
       
    64     if task.status == "DL":
       
    65         return show_msg(user, 'This task no longer exists', '/task/browse/','browse the tasks')
       
    66     comments = task.comment_set.filter(is_deleted=False).order_by('creation_datetime')
       
    67     mentors = task.mentors.all()
       
    68 
       
    69     deps, subs = task.deps, task.subs
       
    70     
       
    71     is_guest = True if not user.is_authenticated() else False
       
    72     is_mentor = True if user in task.mentors.all() else False
       
    73     context = {'user':user,
       
    74                'task':task,
       
    75                'comments':comments,
       
    76                'mentors':mentors,
       
    77                'subs':subs,
       
    78                'deps':deps,
       
    79                'is_guest':is_guest,
       
    80                'is_mentor':is_mentor,
       
    81               }
       
    82 
       
    83     claimed_users = task.claimed_users.all()
       
    84 
       
    85 
       
    86     is_requested_mentor = True if user.is_authenticated() and user.request_sent_to.filter(is_valid=True,is_replied=False,role="MT",task=task) else False
       
    87     task_viewable = True if ( task.status != "UP" ) or is_mentor or is_requested_mentor else False
       
    88     if not task_viewable:
       
    89         return show_msg(user, "You are not authorised to view this task", "/task/browse/", "browse the tasks")
       
    90 
       
    91     context['is_requested_mentor'] = is_requested_mentor
       
    92 
       
    93     context['can_publish'] = True if task.status == "UP" and user == task.created_by else False
       
    94     context['can_edit'] = True if task.status == "UP" and is_mentor else False
       
    95     context['can_close'] = True if task.status not in ["UP", "CD", "CM"] and is_mentor else False
       
    96     context['can_delete'] = True if task.status == "UP" and user == task.created_by else False
       
    97 
       
    98     context['can_mod_mentors'] = True if task.status in ["UP", "OP", "LO", "WR"] and is_mentor else False
       
    99     context['can_mod_tasks'] = True if task.status in ["UP", "OP", "LO"] and is_mentor else False
       
   100 
       
   101     context['can_assign_credits'] = True if task.status in ["OP", "WR"] and is_mentor else False
       
   102     context['task_claimable'] = True if task.status in ["OP", "WR"] and not is_guest else False
       
   103 
       
   104     if task.status == "CD":
       
   105         context['closing_notification'] =  Notification.objects.filter(task=task,role="CD")[0]
       
   106     elif task.status == "CM":
       
   107         context['completed_notification'] =  Notifications.objects.filter(task=task,role="CM")[0]
       
   108     elif task.status == "WR":
       
   109         context['assigned_users'] = task.assigned_users.all()
       
   110    
       
   111     if request.method == 'POST':
       
   112         if not is_guest:
       
   113             data = request.POST.get("data", "").strip()
       
   114             if not data:
       
   115                 context['error_msg'] = "Enter some message to comment"
       
   116                 return render_to_response('task/view.html', context)
       
   117             new_comment = Comment(task=task, data=data, created_by=user, creation_datetime=datetime.now())
       
   118             new_comment.save()
       
   119             return redirect(task_url)
       
   120         else:
       
   121             errors.append("You must be logged in to post a comment")
       
   122             return render_to_response('task/view.html', context)
       
   123     else:
       
   124         return render_to_response('task/view.html', context)
       
   125         
       
   126 def create_task(request):
       
   127     """ check for rights and create a task if applicable.
       
   128     if user cannot create a task, redirect to homepage.
       
   129     """
       
   130     
       
   131     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   132     is_guest = True if not user.is_authenticated() else False
       
   133     
       
   134     if not is_guest:
       
   135         user_profile = user.get_profile()
       
   136         can_create_task = False if user_profile.rights == "CT" else True
       
   137         if can_create_task:
       
   138             if request.method == "POST":
       
   139                 form = TaskCreateForm(request.POST)
       
   140                 if form.is_valid():
       
   141                     data = form.cleaned_data
       
   142                     title = data['title']
       
   143                     desc = data['desc']
       
   144                     credits = data['credits']
       
   145                     #publish = data['publish'] # just in case if we have to show the option
       
   146                     task = createTask(title,desc,user,credits)
       
   147                     
       
   148                     addMentor(task, user)
       
   149                     updateTask(task,tags_field=data['tags_field'])
       
   150                     # if publish: publishTask(task)    
       
   151                     task_url = '/task/view/tid=%s'%task.id
       
   152                     return redirect(task_url)
       
   153                 else:
       
   154                     return render_to_response('task/create.html',{'user':user, 'form':form})
       
   155             else:
       
   156                 form = TaskCreateForm()
       
   157                 return render_to_response('task/create.html',{'user':user, 'form':form})
       
   158         else:
       
   159             return show_msg(user, 'You are not authorised to create a task.')
       
   160     else:
       
   161         return show_msg(user, 'You are not authorised to create a task.', "/", "home page")
       
   162         
       
   163 def add_mentor(request, tid):
       
   164     """ check if the current user has the rights to edit the task and add him.
       
   165     if user is not authenticated, redirect him to concerned page. """
       
   166     
       
   167     task_url = "/task/view/tid=%s"%tid
       
   168     
       
   169     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   170     task = getTask(tid)
       
   171     errors = []
       
   172     
       
   173     is_guest = True if not user.is_authenticated() else False
       
   174     
       
   175     if (not is_guest) and user in task.mentors.all():
       
   176 
       
   177         pending_requests = Request.objects.filter(is_replied=False,is_valid=True,role="MT",task=task).order_by('creation_date').reverse()
       
   178         user_pending_requests = pending_requests.filter(sent_by=user)
       
   179 
       
   180         ## now iam going for a brute force method
       
   181         user_list = list(User.objects.filter(is_active=True))
       
   182         for mentor in task.mentors.all():
       
   183             user_list.remove(mentor)
       
   184             
       
   185         for a_user in task.claimed_users.all():
       
   186             user_list.remove(a_user)
       
   187 
       
   188         for a_user in task.assigned_users.all():
       
   189             user_list.remove(a_user)
       
   190 
       
   191         for req in user_pending_requests:
       
   192             user_list.remove(req.sent_to.all()[0])
       
   193             
       
   194         non_mentors = ((_.id, _.username) for _ in user_list)
       
   195         non_mentor_ids = [ str(a_user.id) for a_user in user_list ]
       
   196         ## code till must be made elegant and not brute force like above
       
   197 
       
   198         form = AddMentorForm(non_mentors)
       
   199 
       
   200         context = {
       
   201             'user':user,
       
   202             'task':task,
       
   203             'pending_requests':pending_requests,
       
   204             'form':form,
       
   205         }
       
   206 
       
   207         if request.method == "POST":
       
   208             data = request.POST
       
   209             uid = data.get('mentor', None)
       
   210             if uid in non_mentor_ids:
       
   211                 new_mentor = User.objects.get(id=int(uid))
       
   212                 reqMentor(task, new_mentor, user)
       
   213                 return redirect('/task/addmentor/tid=%s'%task.id)
       
   214             else:
       
   215                 ## bogus post request
       
   216                 raise Http404
       
   217         else:
       
   218             return render_to_response('task/addmentor.html', context)
       
   219     else:
       
   220         return show_msg(user, 'You are not authorised to add mentors for this task', task_url, 'view the task')
       
   221     
       
   222 def add_tasks(request, tid):
       
   223     """ first display tasks which can be subtasks for the task and do the rest.
       
   224     """
       
   225     
       
   226     task_url = "/task/view/tid=%s"%tid
       
   227     
       
   228     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   229     task = getTask(tid)
       
   230 
       
   231     deps, subs = task.deps, task.subs
       
   232     is_plain = False if deps or subs else True
       
   233 
       
   234     ## again a brute force method
       
   235     valid_tasks = []
       
   236     for a_task in Task.objects.all():
       
   237         if not ( a_task in deps or a_task in subs or a_task.status=="CD" or a_task==task ):
       
   238             valid_tasks.append(a_task)
       
   239 
       
   240     task_choices = [ (_.id,_.title) for _ in valid_tasks ]
       
   241     errors = []
       
   242     
       
   243     is_guest = True if not user.is_authenticated() else False
       
   244     
       
   245     if (not is_guest) and user in task.mentors.all():
       
   246         if task.status in ["UP", "OP", "LO"]:
       
   247             form = AddTaskForm(task_choices, is_plain)
       
   248             if request.method == "POST":
       
   249                 ## first decide if adding subs and deps can be in same page
       
   250                 ## only exclude tasks with status deleted
       
   251                 data = request.POST
       
   252                 if is_plain and not data.get('type', None): errors.append('Please choose which type of task(s) do you want to add.')
       
   253                 if not data.get('task', None): errors.append('Please choose a one task')
       
   254 
       
   255                 if not errors:
       
   256                     if is_plain:
       
   257                         update_method = addDep if data['type'] == "D" else addSubTask
       
   258                     elif deps:
       
   259                         update_method = addDep
       
   260                     elif subs:
       
   261                         update_method = addSubTask
       
   262                     else:
       
   263                         print "Screw you"
       
   264 
       
   265                     ## we might iterate over a task list later on
       
   266                     task_id = data['task']
       
   267                     sub_or_dep = getTask(task_id)
       
   268                     update_method(task, sub_or_dep)
       
   269 
       
   270                     return redirect(task_url)
       
   271                 else:
       
   272                     return render_to_response('task/addtask.html', {'user':user, 'form':form, 'errors':errors})
       
   273             else:
       
   274                 return render_to_response('task/addtask.html', {'user':user, 'form':form, 'errors':errors})
       
   275         else:
       
   276             errors = ["The task cannot be added subtasks or dependencies in this state"]
       
   277 #            return render_to_response('task/add.html', {'form':form, 'errors':errors})
       
   278             return show_msg(user, 'The task cannot be added subtasks or dependencies now', task_url, 'view the task')
       
   279     else:
       
   280         return show_msg(user, 'You are not authorised to add subtasks or dependencies for this task', task_url, 'view the task')
       
   281     
       
   282 def remove_task(request, tid):
       
   283     """ display a list of tasks and remove the selectes ones.
       
   284     """
       
   285 
       
   286     task_url = "/task/view/tid=%s"%tid
       
   287     
       
   288     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   289     task = getTask(tid)
       
   290 
       
   291     is_guest = True if not user.is_authenticated() else False
       
   292     if (not is_guest) and user in task.mentors.all():
       
   293 
       
   294         if task.status in ["UP", "LO", "OP"]:
       
   295             
       
   296             deps, subs = task.deps, task.subs
       
   297             task_list = deps if task.sub_type == "D" else subs
       
   298 
       
   299             if task_list:
       
   300                 choices = [(_.id,_.title) for _ in task_list ]
       
   301                 form = ChoiceForm(choices)
       
   302 
       
   303                 errors = []
       
   304                 
       
   305                 context = {
       
   306                     'user':user,
       
   307                     'task':task,
       
   308                     'form':form,
       
   309                 }
       
   310 
       
   311                 if request.method == "POST":
       
   312                     data = request.POST
       
   313                     if not data.get('choice', None):
       
   314                         errors.append("Please choose a task to remove.")
       
   315                         context['errors'] = errors
       
   316                     if not errors:
       
   317                         tid = data['choice']
       
   318                         sub_task = getTask(tid)
       
   319                         removeTask(task, sub_task)
       
   320                         return redirect(task_url)
       
   321                     else:
       
   322                         return render_to_response('task/removetask.html', context)
       
   323                 else:
       
   324                     return render_to_response('task/removetask.html', context)
       
   325             else:
       
   326                 return show_msg(user, "The task has no subtasks/dependencies to be removed", task_url, "view the task")
       
   327         else:
       
   328             return show_msg(user, "subtasks/dependencies cannot be removed at this stage", task_url, "view the task")
       
   329     else:
       
   330         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
       
   331 
       
   332 def claim_task(request, tid):
       
   333     """ display a list of claims for get and display submit only if claimable """
       
   334 
       
   335     ## create claims model and create a new database with required tables for it
       
   336     ## see if that "one to n" or "n to one" relationship has a special field
       
   337     
       
   338     task_url = "/task/view/tid=%s"%tid
       
   339     claim_url = "/task/claim/tid=%s"%tid
       
   340     
       
   341     errors = []
       
   342     
       
   343     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   344     task = getTask(tid)
       
   345     
       
   346     #claims = task.notifications_task.filter(role="CL",sent_to=task.created_by) 
       
   347     # this is what the next line should be when i re sync the db
       
   348     claims = Notification.objects.filter(task=task, sent_to=task.created_by, role="CL")
       
   349 
       
   350     mentors = task.mentors.all()
       
   351     claimed_users = task.claimed_users.all()
       
   352     assigned_users = task.assigned_users.all()
       
   353     
       
   354     is_guest = True if not user.is_authenticated() else False
       
   355     is_mentor = True if user in mentors else False
       
   356 
       
   357     task_claimable = True if task.status in ["OP", "WR"] else False
       
   358     user_can_claim = True if  task_claimable and not ( is_guest or is_mentor ) and ( user not in claimed_users ) and ( user not in assigned_users )  else False
       
   359     task_claimed = True if claimed_users else False
       
   360     
       
   361     context = {'user':user,
       
   362                'is_mentor':is_mentor,
       
   363                'task':task,
       
   364                'claims':claims,
       
   365                'user_can_claim':user_can_claim,
       
   366                'task_claimable':task_claimable,
       
   367                'task_claimed':task_claimed,
       
   368                'errors':errors}
       
   369     
       
   370     if not is_guest:
       
   371         form = ClaimTaskForm()
       
   372         context['form'] = form
       
   373         if request.method == "POST":
       
   374             form = ClaimTaskForm(request.POST)
       
   375             context['form'] = form
       
   376             if form.is_valid():
       
   377                 claim_proposal = form.cleaned_data['message']
       
   378                 addClaim(task, claim_proposal, user)
       
   379                 return redirect(claim_url)
       
   380             else:
       
   381                 return render_to_response('task/claim.html', context)
       
   382         else:
       
   383             return render_to_response('task/claim.html', context)
       
   384     else:
       
   385         return show_msg(user, 'You are not logged in to view claims for this task', task_url, 'view the task')
       
   386     
       
   387 def rem_user(request, tid):
       
   388     """ show a list of working users and ask for a message/reason for removing user.
       
   389     """
       
   390     
       
   391     task_url = "/task/view/tid=%s"%tid
       
   392     
       
   393     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   394     task = getTask(tid)
       
   395     
       
   396     is_guest = True if not user.is_authenticated() else False
       
   397     is_mentor = True if user in task.mentors.all() else False
       
   398 
       
   399     if (not is_guest) and is_mentor:
       
   400 
       
   401         assigned_users = task.assigned_users.all()
       
   402         choices = [ (_.id,_.username) for _ in assigned_users ]
       
   403         context = {
       
   404             'user':user,
       
   405             'task':task,
       
   406         }
       
   407         
       
   408         if task.status in ["WR"]:
       
   409             if assigned_users:
       
   410                 form = RemoveUserForm(choices)
       
   411                 context['form'] = form
       
   412                 if request.method == "POST":
       
   413                     data = request.POST
       
   414                     form = RemoveUserForm(choices, data)
       
   415                     if form.is_valid():
       
   416                         data = form.cleaned_data
       
   417                         uid = data['user']
       
   418                         reason = data['reason']
       
   419                         rem_user = User.objects.get(id=uid)
       
   420                         removeUser(task, rem_user, user, reason)
       
   421                         return redirect(task_url)
       
   422                     else:
       
   423                         context['form'] = form
       
   424                         return render_to_response('task/remove_user.html', context)
       
   425                 else:
       
   426                     return render_to_response('task/remove_user.html',context)
       
   427             else:
       
   428                 return show_msg(user, "There is no one working on this task to be kicked off", task_url, "view the task")
       
   429         else:
       
   430             return show_msg(user, "This is not the stage to remove users", task_url, "view the task")
       
   431     else:
       
   432         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
       
   433 
       
   434 def assign_task(request, tid):
       
   435     """ first get the status of the task and then assign it to one of claimed users
       
   436     generate list of claimed users by passing it as an argument to a function.
       
   437     """
       
   438     
       
   439     task_url = "/task/view/tid=%s"%tid
       
   440     
       
   441     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   442     task = getTask(tid)
       
   443     
       
   444     is_guest = True if not user.is_authenticated() else False
       
   445     is_mentor = True if user in task.mentors.all() else False
       
   446 
       
   447     claimed_users = task.claimed_users.all()
       
   448     assigned_users = task.assigned_users.all()
       
   449 
       
   450     task_claimed = True if claimed_users else False
       
   451     
       
   452     if (not is_guest) and is_mentor:
       
   453         if task.status in ["OP", "WR"]:
       
   454             if task_claimed:
       
   455                 user_list = ((user.id,user.username) for user in claimed_users)
       
   456                 form = ChoiceForm(user_list)
       
   457         
       
   458                 if request.method == "POST":
       
   459                     uid = request.POST['choice']
       
   460                     assigned_user = User.objects.get(id=uid)
       
   461                     assignTask(task, assigned_user, user)
       
   462                     return redirect(task_url)
       
   463                 else:
       
   464                     return render_to_response('task/assign.html',{'user':user, 'task':task,'form':form})
       
   465             elif assigned_users:
       
   466                 return show_msg(user, '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')
       
   467             else:
       
   468                 return show_msg(user, 'Wait for ppl to claim dude... slow and steady wins the race :)', task_url, 'view the task')
       
   469         else: 
       
   470             return show_msg(user, "The task cannot be assigned to users at this stage", task_url, 'view the task')
       
   471     else:
       
   472         return show_msg(user, 'You are not authorised to perform this action', task_url, 'view the task')
       
   473 
       
   474 def assign_credits(request, tid):
       
   475     """ Check if the user is a mentor and credits can be assigned.
       
   476     Then display all the approved credits.
       
   477     Then see if mentor can assign credits to users also or only mentors.
       
   478     Then put up a form for mentor to assign credits accordingly.
       
   479     """
       
   480     
       
   481     task_url = "/task/view/tid=%s"%tid
       
   482     
       
   483     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   484     task = getTask(tid)
       
   485 
       
   486     ## the moment we see that user had requested credits, it means he had worked and hence we change the status to WR
       
   487     ## we have to discuss on this since, credits may also be given to mentor
       
   488     task.status = "WR"
       
   489     task.save()
       
   490 
       
   491     is_guest = True if not user.is_authenticated() else False
       
   492     is_mentor = True if (not is_guest) and user in task.mentors.all() else False
       
   493 
       
   494     if is_mentor:
       
   495         if task.status in ["OP", "WR"]:
       
   496             choices = [(_.id,_.username) for _ in task.mentors.all()]
       
   497             if task.status == "WR":
       
   498                 choices.extend([(_.id, _.username) for _  in task.assigned_users.all() ])
       
   499             prev_credits = task.request_task.filter(role="PY",is_valid=True,is_replied=True,reply=True).count()
       
   500             credit_requests = task.request_task.filter(role="PY",is_valid=True).order_by('creation_date').reverse()
       
   501             form = AssignCreditForm(choices)
       
   502 
       
   503             context = {
       
   504                 'user':user,
       
   505                 'task':task,
       
   506                 'prev_credits':prev_credits,
       
   507                 'credit_requests':credit_requests,
       
   508                 'form':form,
       
   509             }
       
   510 
       
   511             if request.method == "POST":
       
   512                 data = request.POST
       
   513                 form = AssignCreditForm(choices, data)
       
   514                 if form.is_valid():
       
   515                     data = form.cleaned_data
       
   516                     uid = data['user']
       
   517                     points = data['pynts']
       
   518                     given_to = User.objects.get(id=uid)
       
   519                     assignCredits(task=task, given_by=user, given_to=given_to, points=points)
       
   520                     return redirect('/task/assigncredits/tid=%s'%task.id)
       
   521                 else:
       
   522                     context['form'] = form
       
   523                     return render_to_response('task/assigncredits.html', context)
       
   524             else:
       
   525                 return render_to_response('task/assigncredits.html', context)
       
   526         else:
       
   527             return show_msg(user, "Credits cannot be assigned at this stage", task_url, "view the task")
       
   528     else:
       
   529         return show_msg(user, "You are not authorised to perform this action", task_url, "view the task")
       
   530 
       
   531 def edit_task(request, tid):
       
   532     """ see what are the attributes that can be edited depending on the current status
       
   533     and then give the user fields accordingly.
       
   534     """
       
   535     
       
   536     task = getTask(tid)
       
   537 
       
   538     task_url = "/task/view/tid=%s"%tid
       
   539     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   540 
       
   541     is_mentor = True if user in task.mentors.all() else False
       
   542     can_edit = True if is_mentor and task.status == "UP" else False
       
   543 
       
   544     if can_edit:
       
   545         form = EditTaskForm(instance=task)
       
   546         if request.method=="POST":
       
   547             data = request.POST
       
   548             form = EditTaskForm(data, instance=task)
       
   549             if form.is_valid():
       
   550                 form.save()
       
   551                 return redirect(task_url)
       
   552             else:
       
   553                 return render_to_response('task/edittask.html',{'user':user, 'form':form})
       
   554         else:
       
   555             return render_to_response('task/edittask.html',{'user':user, 'form':form})
       
   556     else:
       
   557         return show_msg(user, "You cannot edit the task at this stage", task_url, "view the task")
       
   558 
       
   559 def complete_task(request, tid):
       
   560     """ call the event called complete task.
       
   561     and also pass it the current user to know who marked it as complete. 
       
   562     """
       
   563 
       
   564     task_url = "/task/view/tid=%s"%tid
       
   565     
       
   566     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   567     task = getTask(tid)
       
   568     
       
   569     is_guest = True if not user.is_authenticated() else False
       
   570     is_mentor = True if user in task.mentors.all() else False
       
   571 
       
   572     claimed_users = task.claimed_users.all()
       
   573     assigned_users = task.assigned_users.all()
       
   574 
       
   575     assign_credits_url = '/task/assigncredits/tid=%s'%task.id
       
   576     task_assigned_credits = task.credit_set.all()
       
   577 
       
   578 
       
   579     if is_mentor:
       
   580         if task.status in ["OP", "WR"]:
       
   581 
       
   582             context = {
       
   583                 'user':user,
       
   584                 'task':task,
       
   585             }
       
   586 
       
   587             if task_assigned_credits:
       
   588                 if request.method=="POST":
       
   589                     completeTask(task, user)
       
   590                     return redirect(task_url)
       
   591                 else:
       
   592                     return render_to_response('task/complete.html', context)
       
   593             else:
       
   594                 return show_msg(user, "Nobody has been credited for doing this task.", assign_credits_url, "assign credits")
       
   595         else:
       
   596             return show_msg(user, "The task cannot be marked as completed at this stage", task_url, "view the task")
       
   597     else:
       
   598         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
       
   599 
       
   600 def close_task(request, tid):
       
   601     """ task can be closed only if task is published.
       
   602     call the event close task if everything is fine.
       
   603     """
       
   604 
       
   605     task_url = "/task/view/tid=%s"%tid
       
   606     
       
   607     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   608     task = getTask(tid)
       
   609     
       
   610     is_guest = True if not user.is_authenticated() else False
       
   611     is_mentor = True if user in task.mentors.all() else False
       
   612 
       
   613     if is_mentor:
       
   614 
       
   615         context = {
       
   616             'user':user,
       
   617             'task':task,
       
   618         }
       
   619 
       
   620         if not task.status in ["UP", "CD", "DL", "CM"]:
       
   621             if request.method == "POST":
       
   622                 data = request.POST
       
   623                 if not data.get("reason", None):
       
   624                     context["error"] = "Please enter a reason for closing the task"
       
   625                     return render_to_response('task/close.html', context)
       
   626                 else:
       
   627                     closeTask(task, user, data['reason'])
       
   628                     return show_msg(user, "The task has been closed.", task_url, "view the task.")
       
   629             else:
       
   630                 return render_to_response('task/close.html', context)
       
   631         else:
       
   632             return show_msg(user, "The task is either already closed or cannot be closed at this stage", task_url, "view the task")
       
   633     else:
       
   634         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
       
   635 
       
   636 def delete_task(request, tid):
       
   637     """ mark the task status as DL.
       
   638     take a reason from the user and pass on to all the other mentors.
       
   639     """
       
   640 
       
   641     task_url = "/task/view/tid=%s"%tid
       
   642 
       
   643     task = getTask(tid)
       
   644     user = get_user(request.user) if request.user.is_authenticated() else request.user
       
   645 
       
   646     if task.status == "DL":
       
   647         return show_msg(user, "This task no longer exists", '/task/browse/', "to browse other tasks")
       
   648 
       
   649     can_delete = True if task.status == "UP" and task.created_by == user else False
       
   650 
       
   651     if can_delete:
       
   652         if request.method == "POST":
       
   653             data = request.POST
       
   654             reason = data.get('reason', None)
       
   655             deleteTask(task, user, reason)
       
   656             return show_msg(user, "The task is deleted", '/', "to return to home page")
       
   657         else:
       
   658             return render_to_response('task/delete.html',{'user':user,})
       
   659     else:
       
   660         return show_msg(user, "You are not authorised to do this at this stage", task_url, "view the task")