pytask/taskapp/views/task.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 01 Feb 2011 04:34:05 +0530
changeset 543 57b0f8f80ebf
parent 538 478c7fc9a223
child 545 3a86d85333a3
permissions -rwxr-xr-x
Add edit and view chapters views.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
"""Module containing the views for all the task related activities.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
If there is something common to all other views. They sit here, here
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
even if there nothing relevant to tasks alone since task is the basis
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
for everything else.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
"""
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
__authors__ = [
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
    '"Nishanth Amuluru" <nishanth@fossee.in>',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
    '"Madhusudan.C.S" <madhusudancs@fossee.in>',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
    ]
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
from datetime import datetime
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
from django import shortcuts
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
from django import http
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
from django.contrib.auth.decorators import login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
from django.contrib.auth.models import User
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
from django.core.context_processors import csrf
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
from django.core.urlresolvers import reverse
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
from django.template import RequestContext
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
from django.utils import simplejson as json
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
from django.utils.translation import ugettext
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
from tagging.models import Tag
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    28
from pytask.helpers import exceptions
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
from pytask.views import show_msg
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
from pytask.profile import models as profile_models
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
from pytask.taskapp import forms as taskapp_forms
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
from pytask.taskapp import models as taskapp_models
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
DONT_CLAIM_TASK_MSG = ugettext(
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    38
  "Please don't submit any claims for the tasks until you get an email "
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    39
  "to start claiming tasks. Please be warned that the task claim work-"
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
  "flow may change. So all the claims submitted before the workshop may "
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
  "not be valid.")
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    43
NO_EDIT_RIGHT = ugettext(
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    44
  "You are not authorized to edit this page.")
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
    45
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
def create_task(request):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
    can_create_task = False if (
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
      profile.role == profile_models.ROLES_CHOICES[3][0]) else True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
    if can_create_task:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
        if request.method == "POST":
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
            form = taskapp_forms.CreateTaskForm(request.POST)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
            if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
                data = form.cleaned_data.copy()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
                data.update({"created_by": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
                             "creation_datetime": datetime.now(),
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
                            })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
                task = taskapp_models.Task(**data)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
                task.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
                task_url = reverse('view_task', kwargs={'task_id': task.id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
                return shortcuts.redirect(task_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
            else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
                context.update({'form':form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
                return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
                  'task/edit.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
            form = taskapp_forms.CreateTaskForm()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
            context.update({'form': form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
              'task/edit.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
        return show_msg(user, 'You are not authorised to create a task.')
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
def browse_tasks(request):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
    context = {}
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
    # TODO(disable): Disable once the tasks can be claimed
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
    context['uberbar_message'] = DONT_CLAIM_TASK_MSG
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
    open_tasks = taskapp_models.Task.objects.filter(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
      status=taskapp_models.TASK_STATUS_CHOICES[1][0])
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
    working_tasks = taskapp_models.Task.objects.filter(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
      status=taskapp_models.TASK_STATUS_CHOICES[3][0])
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
    comp_tasks = taskapp_models.Task.objects.filter(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
      status=taskapp_models.TASK_STATUS_CHOICES[6][0])
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
    context.update({
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
      'open_tasks': open_tasks,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
      'working_tasks': working_tasks,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
      'comp_tasks': comp_tasks,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
      })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
    if not user.is_authenticated():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
        return shortcuts.render_to_response('task/browse.html',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
                                            RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
    if profile.role in [profile_models.ROLES_CHOICES[0][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
                        profile_models.ROLES_CHOICES[1][0]]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
        can_approve = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
        can_approve = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
    unpub_tasks = taskapp_models.Task.objects.filter(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
      status=taskapp_models.TASK_STATUS_CHOICES[0][0]).exclude(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
        status=taskapp_models.TASK_STATUS_CHOICES[5][0])
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
    if can_approve:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
        context.update({'unpub_tasks': unpub_tasks})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   127
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
    context.update({'user': user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
                    'profile': profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
                   })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
    return shortcuts.render_to_response('task/browse.html',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
                                        RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   136
def view_task(request, task_id, **kwargs):
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   137
    """View to get the requested.
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   138
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   139
    Checks for authentication and add a comment if it is a post request.
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
    """
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
    context = {}
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   144
    if 'context' in kwargs:
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   145
        context.update(kwargs['context'])
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   146
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   147
    # TODO(disable): Disable once the tasks can be claimed
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   148
    context['uberbar_message'] = DONT_CLAIM_TASK_MSG
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
    task_url = reverse('view_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   154
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
    if not user.is_authenticated():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   156
        return shortcuts.render_to_response('task/view.html', {'task': task})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   157
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   159
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   160
    context.update({
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
      'user': user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   162
      'profile': profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
      'task': task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   164
      })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   165
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
    if task.status == taskapp_models.TASK_STATUS_CHOICES[5][0]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
        return show_msg(user, 'This task no longer exists',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   170
                        reverse('browse_tasks'), 'browse the tasks')
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   171
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   172
    if ((task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] )
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
      or profile.role != profile_models.ROLES_CHOICES[3][0]):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   174
        task_viewable = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   175
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
        task_viewable = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   177
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   178
    if not task_viewable:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
        return show_msg(user, 'You are not authorised to view this task',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   180
                        reverse('browse_tasks'), 'browse the tasks')
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   181
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   182
    reviewers = task.reviewers.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   183
    is_reviewer = True if user in task.reviewers.all() else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
    comments = task.comments.filter(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   185
      is_deleted=False).order_by('comment_datetime')
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   187
    context.update({'is_reviewer':is_reviewer,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   188
                    'comments':comments,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   189
                    'reviewers':reviewers,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   190
                   })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   191
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   192
    selected_users = task.selected_users.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   193
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   194
    user_role = user.get_profile().role
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   195
    is_creator = True if user == task.created_by else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   196
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   197
    context['selected_users'] = selected_users
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   198
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   199
    context['is_selected'] = True if user in selected_users else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   200
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
    if (task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   202
      profile.role in [profile_models.ROLES_CHOICES[0][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
      profile_models.ROLES_CHOICES[1][0]]):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
        context['can_approve'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   206
        context['can_approve'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   208
    if ((is_creator or user_role != profile_models.ROLES_CHOICES[3][0])
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   209
      and task.status in [taskapp_models.TASK_STATUS_CHOICES[0][0],
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   210
      taskapp_models.TASK_STATUS_CHOICES[1][0]]):
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   211
        context['can_edit'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   212
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   213
        context['can_edit'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   214
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   215
    if (task.status not in [taskapp_models.TASK_STATUS_CHOICES[0][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   216
      taskapp_models.TASK_STATUS_CHOICES[4][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   217
      taskapp_models.TASK_STATUS_CHOICES[6][0]] and is_reviewer):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   218
        context['can_close'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   219
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   220
        context['can_close'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   221
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   222
    if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and is_creator:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   223
        context['can_delete'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   224
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   225
        context['can_delete'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   226
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   227
    if (task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   228
      taskapp_models.TASK_STATUS_CHOICES[3][0]] and is_reviewer):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   229
        context['can_assign_pynts'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   230
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   231
        context['can_assign_pynts'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   232
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   233
    if (task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   234
      taskapp_models.TASK_STATUS_CHOICES[3][0]]):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   235
        context['task_claimable'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   236
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   237
        context['task_claimable'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   238
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   239
    if (task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] or\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   240
      profile.role != profile_models.ROLES_CHOICES[3][0]):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   241
        context['can_comment'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   242
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   243
        context['can_comment'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   244
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   245
    if (profile.role in [profile_models.ROLES_CHOICES[0][0],
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   246
      profile_models.ROLES_CHOICES[1][0]]):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   247
        context['can_mod_reviewers'] = True
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   248
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   249
        context['can_mod_reviewers'] = False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   250
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   251
    if request.method == 'POST':
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   252
        form = taskapp_forms.TaskCommentForm(request.POST)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   253
        if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   254
            data = form.cleaned_data['data']
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   255
            new_comment = taskapp_forms.TaskComment(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   256
              task=task, data=data, commented_by=user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   257
              comment_datetime=datetime.now())
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   258
            new_comment.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   259
            return shortcuts.redirect(task_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   260
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   261
            context['form'] = form
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   262
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   263
              'task/view.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   264
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   265
        form = taskapp_forms.TaskCommentForm()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   266
        context['form'] = form
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   267
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   268
          'task/view.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   269
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   270
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   271
def edit_task(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   272
    """ only creator gets to edit the task and that too only before it gets
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   273
    approved.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   274
    """
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   275
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   276
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   277
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   278
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   279
    task_url = reverse('view_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   280
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   281
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   282
    is_creator = True if user == task.created_by else False
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   283
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   284
    if ((is_creator or profile.role != profile_models.ROLES_CHOICES[3][0])
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   285
      and task.status in [taskapp_models.TASK_STATUS_CHOICES[0][0],
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   286
      taskapp_models.TASK_STATUS_CHOICES[1][0]]):
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   287
        can_edit = True 
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   288
    else:
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   289
        can_edit = False
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   290
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   291
    if not can_edit:
543
57b0f8f80ebf Add edit and view chapters views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 538
diff changeset
   292
        raise exceptions.UnauthorizedAccess(NO_EDIT_RIGHT)
538
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   293
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   294
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   295
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   296
               "task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   297
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   298
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   299
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   300
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   301
    if request.method == "POST":
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   302
        form = taskapp_forms.EditTaskForm(request.POST, instance=task)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   303
        if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   304
            form.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   305
            return shortcuts.redirect(task_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   306
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   307
            context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   308
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   309
              "task/edit.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   310
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   311
        form = taskapp_forms.EditTaskForm(instance=task)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   312
        context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   313
        return shortcuts.render_to_response("task/edit.html",
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   314
                                            RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   315
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   316
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   317
def approve_task(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   318
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   319
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   320
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   321
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   322
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   323
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   324
    if profile.role not in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] or task.status != taskapp_models.TASK_STATUS_CHOICES[0][0]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   325
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   326
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   327
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   328
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   329
               "task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   330
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   331
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   332
    return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   333
      "task/confirm_approval.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   334
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   335
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   336
def approved_task(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   337
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   338
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   339
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   340
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   341
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   342
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   343
    if profile.role not in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] or task.status != taskapp_models.TASK_STATUS_CHOICES[0][0]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   344
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   345
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   346
    task.approved_by = user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   347
    task.approval_datetime = datetime.now()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   348
    task.status = taskapp_models.TASK_STATUS_CHOICES[1][0]
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   349
    task.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   350
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   351
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   352
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   353
               "task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   354
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   355
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   356
    return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   357
      "task/approved_task.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   358
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   359
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   360
def addreviewer(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   361
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   362
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   363
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   364
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   365
    task_url = reverse('view_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   366
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   367
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   368
    can_mod_reviewers = True if profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   369
    if not can_mod_reviewers:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   370
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   371
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   372
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   373
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   374
               "task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   375
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   376
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   377
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   378
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   379
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   380
    # This part has to be made better
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   381
    reviewer_choices = User.objects.filter(is_active=True).\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   382
                                           exclude(reviewing_tasks__id=task_id).\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   383
                                           exclude(claimed_tasks__id=task_id).\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   384
                                           exclude(selected_tasks__id=task_id).\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   385
                                           exclude(created_tasks__id=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   386
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   387
    choices = ((a_user.id,a_user.username) for a_user in reviewer_choices)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   388
    label = "Reviewer"
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   389
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   390
    if request.method == "POST":
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   391
        form = taskapp_forms.ChoiceForm(choices, data=request.POST, label=label)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   392
        if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   393
            data = form.cleaned_data.copy()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   394
            uid = data['choice']
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   395
            reviewer = User.objects.get(id=uid)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   396
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   397
            task.reviewers.add(reviewer)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   398
            return shortcuts.redirect(task_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   399
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   400
            context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   401
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   402
              "task/addreviewer.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   403
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   404
        form = taskapp_forms.ChoiceForm(choices, label=label)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   405
        context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   406
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   407
          "task/addreviewer.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   408
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   409
def view_work(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   410
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   411
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   412
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   413
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   414
    old_reports = task.reports.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   415
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   416
    context = {
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   417
      'task': task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   418
      'old_reports': old_reports,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   419
      }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   420
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   421
    if not user.is_authenticated():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   422
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   423
          "task/view_work.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   424
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   425
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   426
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   427
    context.update({"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   428
                    "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   429
                   })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   430
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   431
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   432
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   433
    working_users = task.selected_users.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   434
    is_working = True if user in working_users else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   435
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   436
    context.update({"is_working": is_working})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   437
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   438
    return shortcuts.render_to_response("task/view_work.html",
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   439
                                        RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   440
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   441
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   442
def view_report(request, report_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   443
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   444
    report = shortcuts.get_object_or_404(taskapp_models.WorkReport,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   445
                                         pk=report_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   446
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   447
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   448
    context = {"report": report,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   449
               "user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   450
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   451
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   452
    if not user.is_authenticated():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   453
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   454
          "task/view_report.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   455
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   456
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   457
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   458
    context.update({"profile": profile})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   459
    return shortcuts.render_to_response("task/view_report.html",
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   460
                                        RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   461
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   462
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   463
def submit_report(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   464
    """ Check if the work is in WR state and the user is in assigned_users.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   465
    """
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   466
    task_url = reverse('view_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   467
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   468
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   469
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   470
    old_reports = task.reports.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   471
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   472
    if not task.status == taskapp_models.TASK_STATUS_CHOICES[3][0]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   473
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   474
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   475
    can_upload = True if user in task.selected_users.all() else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   476
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   477
    context = {
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   478
        'user': user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   479
        'task': task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   480
        'can_upload': can_upload,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   481
    }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   482
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   483
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   484
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   485
    if request.method == "POST":
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   486
        if not can_upload:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   487
            return show_msg(user, "You are not authorised to upload data to this task", task_url, "view the task")
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   488
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   489
        form = taskapp_forms.WorkReportForm(request.POST, request.FILES)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   490
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   491
        if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   492
            data = form.cleaned_data.copy()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   493
            data.update({"task":task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   494
                         "revision": old_reports.count(),
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   495
                         "submitted_by": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   496
                         "submitted_at": datetime.now(),
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   497
                        })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   498
            r = taskapp_models.WorkReport(**data)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   499
            r.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   500
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   501
            report_url = reverse('view_report', kwargs={'report_id': r.id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   502
            return shortcuts.redirect(report_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   503
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   504
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   505
            context.update({"form":form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   506
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   507
              'task/submit_report.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   508
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   509
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   510
        form = taskapp_forms.WorkReportForm()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   511
        context.update({"form":form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   512
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   513
          'task/submit_report.html', RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   514
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   515
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   516
def claim_task(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   517
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   518
    context = {}
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   519
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   520
    # TODO(disable): Disable once the tasks can be claimed
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   521
    context['uberbar_message'] = DONT_CLAIM_TASK_MSG
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   522
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   523
    claim_url = reverse('claim_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   524
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   525
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   526
    if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   527
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   528
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   529
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   530
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   531
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   532
    context.update({
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   533
      'user': user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   534
      'profile': profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   535
      'task': task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   536
      })
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   537
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   538
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   539
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   540
    reviewers = task.reviewers.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   541
    claimed_users = task.claimed_users.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   542
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   543
    is_creator = True if user == task.created_by else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   544
    is_reviewer = True if user in reviewers else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   545
    has_claimed = True if user in claimed_users else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   546
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   547
    task_claimable = True if task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]] else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   548
    can_claim = True if task_claimable and ( not has_claimed )\
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   549
                        and ( not is_reviewer ) and (not is_creator ) \
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   550
                        else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   551
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   552
    old_claims = task.claims.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   553
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   554
    context.update({"is_creator": is_creator,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   555
                    "task_claimable": task_claimable,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   556
                    "can_claim": can_claim,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   557
                    "old_claims": old_claims})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   558
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   559
    if request.method == "POST" and can_claim:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   560
        form = taskapp_forms.ClaimTaskForm(request.POST)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   561
        if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   562
            data = form.cleaned_data.copy()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   563
            data.update({"task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   564
                         "claim_datetime": datetime.now(),
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   565
                         "claimed_by": user,})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   566
            new_claim = taskapp_models.TaskClaim(**data)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   567
            new_claim.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   568
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   569
            task.claimed_users.add(user)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   570
            task.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   571
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   572
            return shortcuts.redirect(claim_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   573
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   574
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   575
            context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   576
            return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   577
              "task/claim.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   578
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   579
        form = taskapp_forms.ClaimTaskForm()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   580
        context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   581
        return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   582
          "task/claim.html", RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   583
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   584
@login_required
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   585
def select_user(request, task_id):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   586
    """ first get the status of the task and then select one of claimed users
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   587
    generate list of claimed users by passing it as an argument to a function.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   588
    """
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   589
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   590
    task_url = reverse('view_task', kwargs={'task_id': task_id})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   591
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   592
    user = request.user
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   593
    profile = user.get_profile()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   594
    task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   595
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   596
    context = {"user": user,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   597
               "profile": profile,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   598
               "task": task,
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   599
              }
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   600
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   601
    context.update(csrf(request))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   602
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   603
    claimed_users = task.claimed_users.all()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   604
    task_claimed = True if claimed_users else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   605
    
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   606
    is_creator = True if user == task.created_by else False
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   607
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   608
    if (is_creator or profile.role in [profile_models.ROLES_CHOICES[1][0], profile_models.ROLES_CHOICES[2][0]]) and \
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   609
       task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]]:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   610
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   611
        if task_claimed:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   612
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   613
            user_list = ((user.id,user.username) for user in claimed_users)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   614
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   615
            if request.method == "POST":
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   616
                form = taskapp_forms.ChoiceForm(user_list, request.POST)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   617
                if form.is_valid():
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   618
                    uid = form.cleaned_data['choice']
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   619
                    selected_user = User.objects.get(id=uid)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   620
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   621
                    task.selected_users.add(selected_user)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   622
                    task.claimed_users.remove(selected_user)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   623
                    task.status = taskapp_models.TASK_STATUS_CHOICES[3][0]
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   624
                    task.save()
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   625
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   626
                    return shortcuts.redirect(task_url)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   627
                else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   628
                    context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   629
                    return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   630
                      'task/select_user.html',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   631
                      RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   632
            else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   633
                form = taskapp_forms.ChoiceForm(user_list)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   634
                context.update({"form": form})
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   635
                return shortcuts.render_to_response(
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   636
                  'task/select_user.html',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   637
                  RequestContext(request, context))
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   638
        else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   639
            return show_msg(user, 'There are no pending claims for this task',
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   640
                            task_url, 'view the task')
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   641
    else:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   642
        raise http.Http404
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   643
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   644
def suggest_task_tags(request):
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   645
    """Returns the tags matching the query for the AJAXy autocomplete
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   646
    to get tags related to tasks.
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   647
    """
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   648
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   649
    term = request.GET.get('term', None)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   650
    response = []
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   651
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   652
    if term:
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   653
      tag_entities = Tag.objects.filter(name__icontains=term)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   654
      response = [tag.name for tag in tag_entities]
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   655
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   656
    json_response = json.dumps(response)
478c7fc9a223 Create a package for taskapp views and break the views into task and textbook.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   657
    return http.HttpResponse(json_response)