finished assign_credits view. hav to integrate it with requests now.
authornishanth
Thu, 25 Feb 2010 06:07:55 +0530
changeset 93 1a1e712e60fd
parent 92 c99f09bebe56
child 94 d1f59bbc2685
child 97 3699550991c6
finished assign_credits view. hav to integrate it with requests now.
taskapp/forms/task.py
taskapp/views/task.py
templates/task/assigncredits.html
urls.py
--- a/taskapp/forms/task.py	Thu Feb 25 04:38:50 2010 +0530
+++ b/taskapp/forms/task.py	Thu Feb 25 06:07:55 2010 +0530
@@ -41,3 +41,10 @@
 
         task = forms.ChoiceField(choices=task_choices)
     return myForm()
+
+def AssignCreditForm(choices, instance=None):
+    
+    class myForm(forms.Form):
+        user = forms.ChoiceField(choices=choices, required=True)
+        points = forms.IntegerField(min_value=0,required=True)
+    return myForm(instance) if instance else myForm()
--- a/taskapp/views/task.py	Thu Feb 25 04:38:50 2010 +0530
+++ b/taskapp/views/task.py	Thu Feb 25 06:07:55 2010 +0530
@@ -3,8 +3,8 @@
 from django.http import HttpResponse
 from django.shortcuts import render_to_response, redirect
 
-from pytask.taskapp.models import User, Task, Comment, Claim
-from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm
+from pytask.taskapp.models import User, Task, Comment, Claim, Credit
+from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm
 from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addDep, addClaim, assignTask, getTask, updateTask, removeTask
 from pytask.taskapp.views.user import show_msg
 
@@ -338,7 +338,60 @@
             return show_msg('Wait for ppl to claim dude... slow and steady wins the race :)', task_url, 'view the task')
     else:
         return show_msg('You are not authorised to perform this action', task_url, 'view the task')
-        
+
+def assign_credits(request, tid):
+    """ Check if the user is a mentor and credits can be assigned.
+    Then display all the approved credits.
+    Then see if mentor can assign credits to users also or only mentors.
+    Then put up a form for mentor to assign credits accordingly.
+    """
+    
+    task_url = "/task/view/tid=%s"%tid
+    
+    user = request.user
+    task = getTask(tid)
+
+    is_guest = True if not user.is_authenticated() else False
+    is_mentor = True if (not is_guest) and user in task.mentors.all() else False
+
+    if is_mentor:
+        if task.status in ["OP", "WR"]:
+            choices = [(_.id,_.username) for _ in task.mentors.all()]
+            if task.status == "WR":
+                choices.extend([(_.id, _.username) for _  in task.assigned_users.all() ])
+            prev_credits = task.credit_set.all()
+            ## here we can ditchax credits model and use the request model
+            form = AssignCreditForm(choices)
+
+            context = {
+                'user':user,
+                'prev_credits':prev_credits,
+                'form':form,
+            }
+
+            if request.method == "POST":
+                data = request.POST
+                form = AssignCreditForm(choices, data)
+                if form.is_valid():
+                    data = form.cleaned_data
+                    uid = data['user']
+                    points = data['points']
+                    given_to = User.objects.get(id=uid)
+                    given_time = datetime.now()
+                    creditobj = Credit(task=task, given_by=user, given_to=given_to,points=points,given_time=given_time)
+                    ## remove the next line and add a request here
+                    creditobj.save()
+                    return redirect('/task/assigncredits/tid=%s'%task.id)
+                else:
+                    context['form'] = form
+                    return render_to_response('task/assigncredits.html', context)
+            else:
+                return render_to_response('task/assigncredits.html', context)
+        else:
+            return show_msg("Credits cannot be assigned at this stage", task_url, "view the task")
+    else:
+        return show_msg("You are not authorised to perform this action", task_url, "view the task")
+
 def edit_task(request, tid):
     """ see what are the attributes that can be edited depending on the current status
     and then give the user fields accordingly.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/task/assigncredits.html	Thu Feb 25 06:07:55 2010 +0530
@@ -0,0 +1,20 @@
+{% extends 'base.html' %}
+{% block title %}
+    {{task.title}}
+{% endblock %}
+{% block content %}
+        
+        {% if prev_credits %}
+            <hr />
+            <br/>Previous credits:<br />
+            {% for credit in prev_credits %}
+                {{credit.points}} pynts were given by <a href="/user/view/uid={{credit.given_by.id}}">{{credit.given_by.username}}</a> to
+                <a href="/user/view/uid={{credit.given_to.id}}">{{credit.given_to.username}}</a> at {{credit.given_time.ctime}}<br />
+            {% endfor %}
+        {% endif %}
+
+        <form action="" method="post">
+        {{form.as_p}}
+        <input type="submit" value="Submit">
+        </form>
+{% endblock %}
--- a/urls.py	Thu Feb 25 04:38:50 2010 +0530
+++ b/urls.py	Thu Feb 25 06:07:55 2010 +0530
@@ -33,6 +33,7 @@
     (r'^task/assign/tid=(\d+)$', taskViews.assign_task),
     (r'^task/addtask/tid=(\d+)$', taskViews.add_tasks),
     (r'^task/remtask/tid=(\d+)$', taskViews.remove_task),
+    (r'^task/assigncredits/tid=(\d+)$', taskViews.assign_credits),
     
     (r'^admin/', include(admin.site.urls)),