pytask/taskapp/views/task.py
changeset 18 293692eb8f06
parent 15 c6038cbf8a39
equal deleted inserted replaced
15:c6038cbf8a39 18:293692eb8f06
     2 
     2 
     3 from django.http import HttpResponse
     3 from django.http import HttpResponse
     4 from django.shortcuts import render_to_response, redirect
     4 from django.shortcuts import render_to_response, redirect
     5 
     5 
     6 from pytask.taskapp.models import User, Task, Comment, Claim
     6 from pytask.taskapp.models import User, Task, Comment, Claim
     7 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm 
     7 from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AssignTaskForm
     8 from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask
     8 from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addClaim, assignTask
     9 from pytask.taskapp.views.user import show_msg
     9 from pytask.taskapp.views.user import show_msg
    10 
    10 
    11 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
    11 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
    12 ## do not create su user thro syncdb
    12 ## do not create su user thro syncdb
    13 
    13 
    37     
    37     
    38     is_guest = True if not user.is_authenticated() else False
    38     is_guest = True if not user.is_authenticated() else False
    39     is_mentor = True if user in task.mentors.all() else False
    39     is_mentor = True if user in task.mentors.all() else False
    40     
    40     
    41     task_claimable = True if task.status in ["OP", "RE"] else False
    41     task_claimable = True if task.status in ["OP", "RE"] else False
    42     user_can_view_claim = True if ( task_claimable and not ( is_guest or is_mentor ) ) else False
       
    43     
    42     
    44     context = {'user':user,
    43     context = {'user':user,
    45                'task':task,
    44                'task':task,
    46                'comments':comments,
    45                'comments':comments,
    47                'mentors':mentors,
    46                'mentors':mentors,
    48                'is_guest':is_guest,
    47                'is_guest':is_guest,
    49                'is_mentor':is_mentor,
    48                'is_mentor':is_mentor,
    50                'user_can_view_claim':user_can_view_claim,
       
    51                'errors':errors,
    49                'errors':errors,
    52                }
    50                }
       
    51                
       
    52     if task.status == "AS":
       
    53         context['assigned_user'] = task.assigned_users.all()[0]
    53     
    54     
    54     if request.method == 'POST':
    55     if request.method == 'POST':
    55         if not is_guest:
    56         if not is_guest:
    56             data = request.POST["data"]
    57             data = request.POST["data"]
    57             task = Task.objects.get(id=tid)
    58             task = Task.objects.get(id=tid)
   170 
   171 
   171     ## create claims model and create a new database with required tables for it
   172     ## create claims model and create a new database with required tables for it
   172     ## see if that "one to n" or "n to one" relationship has a special field
   173     ## see if that "one to n" or "n to one" relationship has a special field
   173     
   174     
   174     task_url = "/task/view/tid=%s"%tid
   175     task_url = "/task/view/tid=%s"%tid
       
   176     claim_url = "/task/claim/tid=%s"%tid
       
   177     
       
   178     errors = []
   175     
   179     
   176     user = request.user
   180     user = request.user
   177     task = Task.objects.get(id=tid)
   181     task = Task.objects.get(id=tid)
   178     claims = Claim.objects.filter(task=task)
   182     claims = Claim.objects.filter(task=task)
   179     
   183     
   180     is_guest = True if not user.is_authenticated() else False
   184     is_guest = True if not user.is_authenticated() else False
   181 
   185     if user in task.mentors.all():
   182     task_claimable = True if task.status in ["OP", "RE"] else False
   186         is_mentor = True 
   183     user_can_claim = True if ( task_claimable and not ( is_guest or is_mentor ) and ( user not in task.claimed_users.all() ) ) else False
   187     else:
       
   188          is_mentor = False
       
   189 
       
   190     task_claimable = True if task.status in ["OP", "RE", "CL"] else False
       
   191     user_can_claim = True if  task_claimable and not ( is_guest or is_mentor ) and ( user not in task.claimed_users.all() )  else False
       
   192     
       
   193     context = {'is_mentor':is_mentor,
       
   194                'task':task,
       
   195                'claims':claims,
       
   196                'user_can_claim':user_can_claim,
       
   197                'task_claimable':task_claimable,
       
   198                'errors':errors}
   184     
   199     
   185     if not is_guest:
   200     if not is_guest:
   186         if task_claimable:
   201         if request.method == "POST":
   187             pass
   202             claim_proposal = request.POST['message']
   188         else:
   203             if claim_proposal:
   189             return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
   204                 addClaim(task, claim_proposal, user)
       
   205                 return redirect(claim_url)
       
   206             else:
       
   207                 errors.append('Please fill up proposal in the field below')
       
   208                 return render_to_response('task/claim.html', context)
       
   209         else:
       
   210             return render_to_response('task/claim.html', context)
   190     else:
   211     else:
   191         return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
   212         return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
   192     
   213     
   193     
   214     
   194     
   215 def assign_task(request, tid):
   195     
   216     """ first get the status of the task and then assign it to one of claimed users
   196     
   217     generate list of claimed users by passing it as an argument to a function.
       
   218     """
       
   219     
       
   220     task_url = "/task/view/tid=%s"%tid
       
   221     
       
   222     user = request.user
       
   223     task = Task.objects.get(id=tid)
       
   224     
       
   225     is_guest = True if not user.is_authenticated() else False
       
   226     is_mentor = True if user in task.mentors.all() else False
       
   227 
       
   228     task_claimed = True if task.status == "CL" else False
       
   229     
       
   230     if (not is_guest) and is_mentor:
       
   231         if task_claimed:
       
   232             user_list = ((user.id,user.username) for user in task.claimed_users.all())
       
   233             form = AssignTaskForm(user_list)
       
   234     
       
   235             if request.method == "POST":
       
   236                 uid = request.POST['user']
       
   237                 assigned_user = User.objects.get(id=uid)
       
   238                 assignTask(task, assigned_user)
       
   239                 return redirect(task_url)
       
   240             else:
       
   241                 return render_to_response('task/assign.html',{'form':form})
       
   242         else:
       
   243             return show_msg('The task is already assigned', task_url, 'view the task')
       
   244     else:
       
   245         return show_msg('You are not authorised to perform this action', task_url, 'view the task')
       
   246         
       
   247