pytask/taskapp/views/task.py
author nishanth
Mon, 01 Feb 2010 15:00:40 +0530
changeset 18 293692eb8f06
parent 15 c6038cbf8a39
permissions -rw-r--r--
added the functionality to assign a task to one of the claimed users.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
     1
from datetime import datetime
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
     2
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
     3
from django.http import HttpResponse
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
     4
from django.shortcuts import render_to_response, redirect
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
     5
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
     6
from pytask.taskapp.models import User, Task, Comment, Claim
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
     7
from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AssignTaskForm
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
     8
from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addClaim, assignTask
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
     9
from pytask.taskapp.views.user import show_msg
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    10
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
    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
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
    12
## do not create su user thro syncdb
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
    13
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    14
def browse_tasks(request):
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    15
    """ display all the tasks """
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    16
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    17
    user = request.user
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    18
    task_list = Task.objects.order_by('id').reverse()
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    19
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    20
    context = {'user':user,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    21
               'task_list':task_list,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    22
               }
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    23
    return render_to_response('task/browse.html', context)
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    24
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    25
def view_task(request, tid):
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    26
    """ get the task depending on its tid and display accordingly if it is a get.
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    27
    check for authentication and add a comment if it is a post request.
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    28
    """
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    29
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    30
    task_url = "/task/view/tid=%s"%tid
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    31
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    32
    user = request.user
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    33
    task = Task.objects.get(id=tid)
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    34
    comments = Comment.objects.filter(task=task)
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
    35
    mentors = task.mentors.all()
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    36
    errors = []
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    37
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    38
    is_guest = True if not user.is_authenticated() else False
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    39
    is_mentor = True if user in task.mentors.all() else False
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    40
    
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
    41
    task_claimable = True if task.status in ["OP", "RE"] else False
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
    42
    
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    43
    context = {'user':user,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    44
               'task':task,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    45
               'comments':comments,
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
    46
               'mentors':mentors,
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    47
               'is_guest':is_guest,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    48
               'is_mentor':is_mentor,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    49
               'errors':errors,
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    50
               }
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
    51
               
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
    52
    if task.status == "AS":
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
    53
        context['assigned_user'] = task.assigned_users.all()[0]
5
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    54
    
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    55
    if request.method == 'POST':
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    56
        if not is_guest:
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    57
            data = request.POST["data"]
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    58
            task = Task.objects.get(id=tid)
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    59
            new_comment = Comment(task=task, data=data, created_by=user, creation_datetime=datetime.now())
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    60
            new_comment.save()
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    61
            return redirect(task_url)
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    62
        else:
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    63
            errors.append("You must be logged in to post a comment")
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    64
            return render_to_response('task/view.html', context)
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    65
    else:
aea7e764c033 created views and templates for homepage,browse_task and added actions.
nishanth
parents: 2
diff changeset
    66
        return render_to_response('task/view.html', context)
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    67
        
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    68
def create_task(request):
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    69
    """ check for rights and create a task if applicable.
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    70
    if user cannot create a task, redirect to homepage.
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    71
    """
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    72
    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    73
    user = request.user
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    74
    is_guest = True if not user.is_authenticated() else False
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    75
    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    76
    if not is_guest:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    77
        user_profile = user.get_profile()
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    78
        can_create_task = False if user_profile.rights == "CT" else True
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    79
        if can_create_task:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    80
            if request.method == "POST":
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    81
                form = TaskCreateForm(request.POST)
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    82
                if form.is_valid():
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    83
                    data = form.cleaned_data
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    84
                    title = data['title']
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    85
                    desc = data['desc']
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    86
                    credits = data['credits']
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    87
                    publish = data['publish']
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    88
                    task = createTask(title,desc,user,credits)
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    89
                    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    90
                    if not task:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    91
                        error_msg = "Another task with the same title exists"
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    92
                        return render_to_response('task/create.html',{'form':form, 'error_msg':error_msg})
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    93
                    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    94
                    addMentor(task, user)
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    95
                    if publish: publishTask(task)    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    96
                    task_url = '/task/view/tid=%s'%task.id
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    97
                    return redirect(task_url)
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    98
                else:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
    99
                    return render_to_response('task/create.html',{'form':form})
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   100
            else:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   101
                form = TaskCreateForm()
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   102
                return render_to_response('task/create.html',{'form':form})
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   103
        else:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   104
            return show_msg('You are not authorised to create a task.')
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   105
    else:
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   106
        return show_msg('You are not authorised to create a task.')
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   107
        
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   108
def add_mentor(request, tid):
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   109
    """ check if the current user has the rights to edit the task and add him.
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   110
    if user is not authenticated, redirect him to concerned page. """
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   111
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   112
    task_url = "/task/view/tid=%s"%tid
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   113
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   114
    user = request.user
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   115
    task = Task.objects.get(id=tid)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   116
    errors = []
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   117
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   118
    is_guest = True if not user.is_authenticated() else False
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   119
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   120
    if (not is_guest) and user in task.mentors.all():
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   121
        
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   122
        ## now iam going for a brute force method
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   123
        user_list = list(User.objects.all())
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   124
        for mentor in task.mentors.all():
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   125
            user_list.remove(mentor)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   126
        non_mentors = ((_.id,_.username) for _ in user_list)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   127
        
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   128
        form = AddMentorForm(non_mentors)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   129
        if request.method == "POST":
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   130
            uid = request.POST['mentor']
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   131
            new_mentor = User.objects.get(id=uid)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   132
            addMentor(task, new_mentor)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   133
            return redirect(task_url)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   134
        else:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   135
            return render_to_response('task/addmentor.html', {'form':form, 'errors':errors})
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   136
        
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   137
    else:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   138
        return show_msg('You are not authorised to add mentors for this task', task_url, 'view the task')
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   139
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   140
def add_tasks(request, tid):
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   141
    """ first display tasks which can be subtasks for the task and do the rest.
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   142
    """
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   143
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   144
    task_url = "/task/view/tid=%s"%tid
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   145
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   146
    user = request.user
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   147
    task = Task.objects.get(id=tid)
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   148
    errors = []
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   149
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   150
    is_guest = True if not user.is_authenticated() else False
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   151
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   152
    if (not is_guest) and user in task.mentors.all():
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   153
        if task.status in ["OP", "LO"]:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   154
            if request.method == "POST":
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   155
                ## first decide if adding subs and deps can be in same page
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   156
                ## only exclude tasks with status deleted
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   157
                pass
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   158
            else:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   159
                ## write a form just like add mentor and get the form here
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   160
                pass
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   161
        else:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   162
            errors = ["The task cannot be added subtasks or dependencies in this state"]
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   163
#            return render_to_response('task/add.html', {'form':form, 'errors':errors})
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   164
            return show_msg('The task cannot be added subtasks or dependencies now', task_url, 'view the task')
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   165
    else:
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   166
        return show_msg('You are not authorised to add subtasks or dependencies for this task', task_url, 'view the task')
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   167
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   168
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   169
def claim_task(request, tid):
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   170
    """ display a list of claims for get and display submit only if claimable """
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   171
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   172
    ## create claims model and create a new database with required tables for it
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   173
    ## see if that "one to n" or "n to one" relationship has a special field
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   174
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   175
    task_url = "/task/view/tid=%s"%tid
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   176
    claim_url = "/task/claim/tid=%s"%tid
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   177
    
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   178
    errors = []
14
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   179
    
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   180
    user = request.user
f2623fb8041a implemented add another mentor functionality to a task.
nishanth
parents: 11
diff changeset
   181
    task = Task.objects.get(id=tid)
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   182
    claims = Claim.objects.filter(task=task)
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   183
    
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   184
    is_guest = True if not user.is_authenticated() else False
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   185
    if user in task.mentors.all():
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   186
        is_mentor = True 
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   187
    else:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   188
         is_mentor = False
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   189
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   190
    task_claimable = True if task.status in ["OP", "RE", "CL"] else False
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   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
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   192
    
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   193
    context = {'is_mentor':is_mentor,
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   194
               'task':task,
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   195
               'claims':claims,
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   196
               'user_can_claim':user_can_claim,
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   197
               'task_claimable':task_claimable,
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   198
               'errors':errors}
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   199
    
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   200
    if not is_guest:
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   201
        if request.method == "POST":
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   202
            claim_proposal = request.POST['message']
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   203
            if claim_proposal:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   204
                addClaim(task, claim_proposal, user)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   205
                return redirect(claim_url)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   206
            else:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   207
                errors.append('Please fill up proposal in the field below')
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   208
                return render_to_response('task/claim.html', context)
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   209
        else:
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   210
            return render_to_response('task/claim.html', context)
15
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   211
    else:
c6038cbf8a39 addded link to view claims in 'view task' page .
nishanth
parents: 14
diff changeset
   212
        return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   213
    
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   214
    
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   215
def assign_task(request, tid):
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   216
    """ first get the status of the task and then assign it to one of claimed users
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   217
    generate list of claimed users by passing it as an argument to a function.
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   218
    """
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   219
    
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   220
    task_url = "/task/view/tid=%s"%tid
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   221
    
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   222
    user = request.user
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   223
    task = Task.objects.get(id=tid)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   224
    
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   225
    is_guest = True if not user.is_authenticated() else False
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   226
    is_mentor = True if user in task.mentors.all() else False
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   227
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   228
    task_claimed = True if task.status == "CL" else False
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   229
    
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   230
    if (not is_guest) and is_mentor:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   231
        if task_claimed:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   232
            user_list = ((user.id,user.username) for user in task.claimed_users.all())
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   233
            form = AssignTaskForm(user_list)
11
d28fcc644fbb implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents: 10
diff changeset
   234
    
18
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   235
            if request.method == "POST":
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   236
                uid = request.POST['user']
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   237
                assigned_user = User.objects.get(id=uid)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   238
                assignTask(task, assigned_user)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   239
                return redirect(task_url)
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   240
            else:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   241
                return render_to_response('task/assign.html',{'form':form})
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   242
        else:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   243
            return show_msg('The task is already assigned', task_url, 'view the task')
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   244
    else:
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   245
        return show_msg('You are not authorised to perform this action', task_url, 'view the task')
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   246
        
293692eb8f06 added the functionality to assign a task to one of the claimed users.
nishanth
parents: 15
diff changeset
   247