added capability to remove an assigned user.
authornishanth
Thu, 25 Feb 2010 16:17:56 +0530
changeset 94 d1f59bbc2685
parent 93 1a1e712e60fd
child 95 e4034904f82e
added capability to remove an assigned user.
taskapp/events/task.py
taskapp/forms/task.py
taskapp/views/task.py
templates/task/remove_user.html
templates/task/view.html
urls.py
--- a/taskapp/events/task.py	Thu Feb 25 06:07:55 2010 +0530
+++ b/taskapp/events/task.py	Thu Feb 25 16:17:56 2010 +0530
@@ -175,3 +175,10 @@
     mapobj = Map.objects.get(main=main_task)
     mapobj.subs.remove(sub_task)
     mapobj.save()
+
+def removeUser(main_task, rem_user):
+    """ right now, just remove the user from the list of assigned_users.
+    """
+
+    main_task.assigned_users.remove(rem_user)
+    main_task.save()
--- a/taskapp/forms/task.py	Thu Feb 25 06:07:55 2010 +0530
+++ b/taskapp/forms/task.py	Thu Feb 25 16:17:56 2010 +0530
@@ -48,3 +48,11 @@
         user = forms.ChoiceField(choices=choices, required=True)
         points = forms.IntegerField(min_value=0,required=True)
     return myForm(instance) if instance else myForm()
+
+def RemoveUserForm(choices, instance=None):
+
+    class myForm(forms.Form):
+        user = forms.ChoiceField(choices=choices, required=True)
+        reason = forms.CharField(min_length=1, required=True)
+    return myForm(instance) if instance else myForm()
+
--- a/taskapp/views/task.py	Thu Feb 25 06:07:55 2010 +0530
+++ b/taskapp/views/task.py	Thu Feb 25 16:17:56 2010 +0530
@@ -4,8 +4,8 @@
 from django.shortcuts import render_to_response, redirect
 
 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.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm
+from pytask.taskapp.events.task import createTask, addMentor, publishTask, addSubTask, addDep, addClaim, assignTask, getTask, updateTask, removeTask, removeUser
 from pytask.taskapp.views.user import show_msg
 
 ## everywhere if there is no task, django should display 500 message.. but take care of that in sensitive views like add mentor and all
@@ -301,7 +301,50 @@
     else:
         return show_msg('You are not logged in to view claims for this task', task_url, 'view the task')
     
+def rem_user(request, tid):
+    """ show a list of working users and ask for a message/reason for removing user.
+    """
     
+    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 user in task.mentors.all() else False
+
+    if (not is_guest) and is_mentor:
+
+        assigned_users = task.assigned_users.all()
+        choices = [ (_.id,_.username) for _ in assigned_users ]
+        context = {
+            'user':user,
+            'task':task,
+        }
+
+        if assigned_users:
+            form = RemoveUserForm(choices)
+            context['form'] = form
+            if request.method == "POST":
+                data = request.POST
+                form = RemoveUserForm(choices, data)
+                if form.is_valid():
+                    data = form.cleaned_data
+                    uid = data['user']
+                    rem_user = User.objects.get(id=uid)
+                    removeUser(task, rem_user)
+                    print data['reason']
+                    return redirect(task_url)
+                else:
+                    context['form'] = form
+                    return render_to_response('task/remove_user.html', context)
+            else:
+                return render_to_response('task/remove_user.html',context)
+        else:
+            return show_msg("There is no one working on this task to be kicked off", task_url, "view the task")
+    else:
+        return show_msg("You are not authorised to do this", task_url, "view the task")
+
 def assign_task(request, tid):
     """ first get the status of the task and then assign it to one of claimed users
     generate list of claimed users by passing it as an argument to a function.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/task/remove_user.html	Thu Feb 25 16:17:56 2010 +0530
@@ -0,0 +1,11 @@
+{% extends 'base.html' %}
+{% block title %}
+    Remove users for {{task.title}}
+{% endblock %}
+{% block content %}
+    <a href="/task/view/tid={{task.id}}">Click here</a> to return to {{task.title}}
+    <form action="" method="post">
+        {{form.as_p}}
+    <input value="Submit" type="submit">
+    </form>
+{% endblock %}
--- a/templates/task/view.html	Thu Feb 25 06:07:55 2010 +0530
+++ b/templates/task/view.html	Thu Feb 25 16:17:56 2010 +0530
@@ -66,9 +66,11 @@
                 <a href="/user/view/uid={{user.id}}">{{user.username}}</a>|
             {% endfor %}
             {% if is_mentor %}
-                <a href="/task/user/remove/">Remove an existing user</a>
+                <a href="/task/remuser/tid={{task.id}}">Remove an existing user</a>
             <br />
             {% endif %}
+        {% else %}
+            There are no users currently working on this task.<br />
         {% endif %}
         {% if can_assign_credits %}
             <a href="/task/assigncredits/tid={{task.id}}">Assign credits</a>
--- a/urls.py	Thu Feb 25 06:07:55 2010 +0530
+++ b/urls.py	Thu Feb 25 16:17:56 2010 +0530
@@ -31,6 +31,7 @@
     (r'^task/edit/tid=(\d+)$', taskViews.edit_task),
     (r'^task/claim/tid=(\d+)$', taskViews.claim_task),
     (r'^task/assign/tid=(\d+)$', taskViews.assign_task),
+    (r'^task/remuser/tid=(\d+)$', taskViews.rem_user),
     (r'^task/addtask/tid=(\d+)$', taskViews.add_tasks),
     (r'^task/remtask/tid=(\d+)$', taskViews.remove_task),
     (r'^task/assigncredits/tid=(\d+)$', taskViews.assign_credits),