pytask/taskapp/views.py
changeset 443 a13ea00c1a3f
parent 440 56ea209e559f
child 451 09a1ee04a3f4
equal deleted inserted replaced
442:7e6da295b90a 443:a13ea00c1a3f
    29               }
    29               }
    30 
    30 
    31     context.update(csrf(request))
    31     context.update(csrf(request))
    32 
    32 
    33     can_create_task = False if (
    33     can_create_task = False if (
    34       profile.role == profile_models.ROLE_CHOICES[3][0]) else True
    34       profile.role == profile_models.ROLES_CHOICES[3][0]) else True
    35     if can_create_task:
    35     if can_create_task:
    36         if request.method == "POST":
    36         if request.method == "POST":
    37             form = taskapp_forms.CreateTaskForm(request.POST)
    37             form = taskapp_forms.CreateTaskForm(request.POST)
    38             if form.is_valid():
    38             if form.is_valid():
    39                 data = form.cleaned_data.copy()
    39                 data = form.cleaned_data.copy()
    72     if not user.is_authenticated():
    72     if not user.is_authenticated():
    73         return shortcuts.render_to_response("task/browse.html")
    73         return shortcuts.render_to_response("task/browse.html")
    74 
    74 
    75     profile = user.get_profile()
    75     profile = user.get_profile()
    76 
    76 
    77     can_approve = True if profile.role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] else False
    77     can_approve = True if profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] else False
    78     unpub_tasks = taskapp_models.Task.objects.filter(status=taskapp_models.TASK_STATUS_CHOICES[0][0]).exclude(status=taskapp_models.TASK_STATUS_CHOICES[5][0])
    78     unpub_tasks = taskapp_models.Task.objects.filter(status=taskapp_models.TASK_STATUS_CHOICES[0][0]).exclude(status=taskapp_models.TASK_STATUS_CHOICES[5][0])
    79     if can_approve:
    79     if can_approve:
    80         context.update({"unpub_tasks": unpub_tasks})
    80         context.update({"unpub_tasks": unpub_tasks})
    81 
    81 
    82     context.update({"user": user,
    82     context.update({"user": user,
   110 
   110 
   111     if task.status == taskapp_models.TASK_STATUS_CHOICES[5][0]:
   111     if task.status == taskapp_models.TASK_STATUS_CHOICES[5][0]:
   112         return show_msg(user, 'This task no longer exists',
   112         return show_msg(user, 'This task no longer exists',
   113                         reverse('browse_tasks'), 'browse the tasks')
   113                         reverse('browse_tasks'), 'browse the tasks')
   114 
   114 
   115     task_viewable = True if ( task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] ) or profile.role != profile_models.ROLE_CHOICES[3][0] \
   115     task_viewable = True if ( task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] ) or profile.role != profile_models.ROLES_CHOICES[3][0] \
   116                          else False
   116                          else False
   117     if not task_viewable:
   117     if not task_viewable:
   118         return show_msg(user, "You are not authorised to view this task",
   118         return show_msg(user, "You are not authorised to view this task",
   119                         reverse('browse_tasks'), "browse the tasks")
   119                         reverse('browse_tasks'), "browse the tasks")
   120 
   120 
   133     is_creator = True if user == task.created_by else False
   133     is_creator = True if user == task.created_by else False
   134 
   134 
   135     context['selected_users'] = selected_users
   135     context['selected_users'] = selected_users
   136     context['is_selected'] = True if user in selected_users else False
   136     context['is_selected'] = True if user in selected_users else False
   137     context['can_approve'] = True if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and\
   137     context['can_approve'] = True if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and\
   138                                      profile.role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]]\
   138                                      profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]]\
   139                                      else False
   139                                      else False
   140     context['can_edit'] = True if is_creator and task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] else False
   140     context['can_edit'] = True if is_creator and task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] else False
   141     context['can_close'] = True if task.status not in [taskapp_models.TASK_STATUS_CHOICES[0][0], taskapp_models.TASK_STATUS_CHOICES[4][0], taskapp_models.TASK_STATUS_CHOICES[6][0]] and is_reviewer else False
   141     context['can_close'] = True if task.status not in [taskapp_models.TASK_STATUS_CHOICES[0][0], taskapp_models.TASK_STATUS_CHOICES[4][0], taskapp_models.TASK_STATUS_CHOICES[6][0]] and is_reviewer else False
   142     context['can_delete'] = True if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and is_creator else False
   142     context['can_delete'] = True if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and is_creator else False
   143 
   143 
   144     context['can_assign_pynts'] = True if task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]] and is_reviewer else False
   144     context['can_assign_pynts'] = True if task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]] and is_reviewer else False
   145     context['task_claimable'] = True if task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]] else False
   145     context['task_claimable'] = True if task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]] else False
   146 
   146 
   147     context['can_comment'] = True if task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] or\
   147     context['can_comment'] = True if task.status != taskapp_models.TASK_STATUS_CHOICES[0][0] or\
   148                                      profile.role != profile_models.ROLE_CHOICES[3][0] else False
   148                                      profile.role != profile_models.ROLES_CHOICES[3][0] else False
   149 
   149 
   150     context['can_mod_reviewers'] = True if profile.role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] else\
   150     context['can_mod_reviewers'] = True if profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] else\
   151                                    False
   151                                    False
   152 
   152 
   153     if request.method == 'POST':
   153     if request.method == 'POST':
   154         form = taskapp_forms.TaskCommentForm(request.POST)
   154         form = taskapp_forms.TaskCommentForm(request.POST)
   155         if form.is_valid():
   155         if form.is_valid():
   210     user = request.user
   210     user = request.user
   211     profile = user.get_profile()
   211     profile = user.get_profile()
   212 
   212 
   213     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   213     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   214 
   214 
   215     if profile.role not in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] or task.status != taskapp_models.TASK_STATUS_CHOICES[0][0]:
   215     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]:
   216         raise http.Http404
   216         raise http.Http404
   217 
   217 
   218     context = {"user": user,
   218     context = {"user": user,
   219                "profile": profile,
   219                "profile": profile,
   220                "task": task,
   220                "task": task,
   228     user = request.user
   228     user = request.user
   229     profile = user.get_profile()
   229     profile = user.get_profile()
   230 
   230 
   231     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   231     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   232 
   232 
   233     if profile.role not in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] or task.status != taskapp_models.TASK_STATUS_CHOICES[0][0]:
   233     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]:
   234         raise http.Http404
   234         raise http.Http404
   235 
   235 
   236     task.approved_by = user
   236     task.approved_by = user
   237     task.approval_datetime = datetime.now()
   237     task.approval_datetime = datetime.now()
   238     task.status = taskapp_models.TASK_STATUS_CHOICES[1][0]
   238     task.status = taskapp_models.TASK_STATUS_CHOICES[1][0]
   252     profile = user.get_profile()
   252     profile = user.get_profile()
   253 
   253 
   254     task_url = reverse('view_task', kwargs={'task_id': task_id})
   254     task_url = reverse('view_task', kwargs={'task_id': task_id})
   255     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   255     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
   256 
   256 
   257     can_mod_reviewers = True if profile.role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] else False
   257     can_mod_reviewers = True if profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] else False
   258     if not can_mod_reviewers:
   258     if not can_mod_reviewers:
   259         raise http.Http404
   259         raise http.Http404
   260 
   260 
   261     context = {"user": user,
   261     context = {"user": user,
   262                "profile": profile,
   262                "profile": profile,
   396 def create_textbook(request):
   396 def create_textbook(request):
   397 
   397 
   398     user = request.user
   398     user = request.user
   399     profile = user.get_profile()
   399     profile = user.get_profile()
   400 
   400 
   401     can_create = True if profile.role != profile_models.ROLE_CHOICES[3][0] else False
   401     can_create = True if profile.role != profile_models.ROLES_CHOICES[3][0] else False
   402     if not can_create:
   402     if not can_create:
   403         raise http.Http404
   403         raise http.Http404
   404 
   404 
   405     context = {"user": user,
   405     context = {"user": user,
   406                "profile": profile,
   406                "profile": profile,
   455     context.update(csrf(request))
   455     context.update(csrf(request))
   456 
   456 
   457     can_edit = True if user == textbook.created_by and textbook.status == taskapp_models.TB_STATUS_CHOICES[0][0]\
   457     can_edit = True if user == textbook.created_by and textbook.status == taskapp_models.TB_STATUS_CHOICES[0][0]\
   458                        else False
   458                        else False
   459 
   459 
   460     can_approve = True if profile.role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] and \
   460     can_approve = True if profile.role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] and \
   461                           textbook.status == taskapp_models.TB_STATUS_CHOICES[0][0] else False
   461                           textbook.status == taskapp_models.TB_STATUS_CHOICES[0][0] else False
   462 
   462 
   463     context.update({"can_edit": can_edit,
   463     context.update({"can_edit": can_edit,
   464                     "can_approve": can_approve})
   464                     "can_approve": can_approve})
   465     return shortcuts.render_to_response("task/view_textbook.html", context)
   465     return shortcuts.render_to_response("task/view_textbook.html", context)
   475     context = {"user": user,
   475     context = {"user": user,
   476                "open_textbooks": open_textbooks,
   476                "open_textbooks": open_textbooks,
   477                "comp_textbooks": comp_textbooks,
   477                "comp_textbooks": comp_textbooks,
   478               }
   478               }
   479 
   479 
   480     if user.is_authenticated() and user.get_profile().role in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]]:
   480     if user.is_authenticated() and user.get_profile().role in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]]:
   481         unpub_textbooks = taskapp_models.TextBook.objects.filter(status=taskapp_models.TB_STATUS_CHOICES[0][0])
   481         unpub_textbooks = taskapp_models.TextBook.objects.filter(status=taskapp_models.TB_STATUS_CHOICES[0][0])
   482 
   482 
   483         context.update({"unpub_textbooks": unpub_textbooks})
   483         context.update({"unpub_textbooks": unpub_textbooks})
   484 
   484 
   485     return shortcuts.render_to_response("task/browse_textbooks.html", context)
   485     return shortcuts.render_to_response("task/browse_textbooks.html", context)
   603     claimed_users = task.claimed_users.all()
   603     claimed_users = task.claimed_users.all()
   604     task_claimed = True if claimed_users else False
   604     task_claimed = True if claimed_users else False
   605     
   605     
   606     is_creator = True if user == task.created_by else False
   606     is_creator = True if user == task.created_by else False
   607 
   607 
   608     if (is_creator or profile.role in [profile_models.ROLE_CHOICES[1][0], profile_models.ROLE_CHOICES[2][0]]) and \
   608     if (is_creator or profile.role in [profile_models.ROLES_CHOICES[1][0], profile_models.ROLES_CHOICES[2][0]]) and \
   609        task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]]:
   609        task.status in [taskapp_models.TASK_STATUS_CHOICES[1][0], taskapp_models.TASK_STATUS_CHOICES[3][0]]:
   610 
   610 
   611         if task_claimed:
   611         if task_claimed:
   612 
   612 
   613             user_list = ((user.id,user.username) for user in claimed_users)
   613             user_list = ((user.id,user.username) for user in claimed_users)
   643     user = request.user
   643     user = request.user
   644     profile = user.get_profile()
   644     profile = user.get_profile()
   645 
   645 
   646     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
   646     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
   647 
   647 
   648     if profile.role not in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] or textbook.status != taskapp_models.TB_STATUS_CHOICES[0][0]:
   648     if profile.role not in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] or textbook.status != taskapp_models.TB_STATUS_CHOICES[0][0]:
   649         raise http.Http404
   649         raise http.Http404
   650 
   650 
   651     context = {"user": user,
   651     context = {"user": user,
   652                "profile": profile,
   652                "profile": profile,
   653                "textbook": textbook,
   653                "textbook": textbook,
   661     user = request.user
   661     user = request.user
   662     profile = user.get_profile()
   662     profile = user.get_profile()
   663 
   663 
   664     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
   664     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
   665 
   665 
   666     if profile.role not in [profile_models.ROLE_CHOICES[0][0], profile_models.ROLE_CHOICES[1][0]] or textbook.status != taskapp_models.TB_STATUS_CHOICES[0][0]:
   666     if profile.role not in [profile_models.ROLES_CHOICES[0][0], profile_models.ROLES_CHOICES[1][0]] or textbook.status != taskapp_models.TB_STATUS_CHOICES[0][0]:
   667         raise http.Http404
   667         raise http.Http404
   668 
   668 
   669     textbook.approved_by = user
   669     textbook.approved_by = user
   670     textbook.approval_datetime = datetime.now()
   670     textbook.approval_datetime = datetime.now()
   671     textbook.status = taskapp_models.TB_STATUS_CHOICES[1][0]
   671     textbook.status = taskapp_models.TB_STATUS_CHOICES[1][0]