implemented edit_task functionality.
authornishanth
Tue, 02 Mar 2010 02:12:57 +0530
changeset 165 8ea5bcf3bd01
parent 164 bfe621e64a16
child 166 ac72d641046e
implemented edit_task functionality.
taskapp/forms/task.py
taskapp/models.py
taskapp/views/task.py
--- a/taskapp/forms/task.py	Tue Mar 02 01:05:24 2010 +0530
+++ b/taskapp/forms/task.py	Tue Mar 02 02:12:57 2010 +0530
@@ -5,7 +5,20 @@
     class Meta:
         model = Task
         fields = ['title', 'desc', 'tags_field', 'credits']
-    publish = forms.BooleanField(required=False)
+    #publish = forms.BooleanField(required=False)
+
+def EditTaskForm(task, instance=None):
+    class myForm(forms.ModelForm):
+        class Meta:
+            model = Task
+            fields = ['title', 'desc', 'tags_field', 'credits']
+    data = {
+        'title': task.title,
+        'desc': task.desc,
+        'tags_field': task.tags_field,
+        'credits': task.credits,
+    }
+    return myForm(instance) if instance else myForm(data)
 
 def AddMentorForm(choices,instance=None):
     """ return a form object with appropriate choices """
--- a/taskapp/models.py	Tue Mar 02 01:05:24 2010 +0530
+++ b/taskapp/models.py	Tue Mar 02 02:12:57 2010 +0530
@@ -92,7 +92,7 @@
     
     prim_key = models.AutoField(primary_key = True)
     id = models.CharField(max_length = 10, unique = True)
-    title = models.CharField(max_length = 100, unique = True, verbose_name = u"Title", help_text = u"Keep it simple and below 100 chars.")
+    title = models.CharField(max_length = 100, verbose_name = u"Title", help_text = u"Keep it simple and below 100 chars.")
     desc = models.TextField(verbose_name = u"Description")
     status = models.CharField(max_length = 2, choices = STATUS_CHOICES, default = "UP")
     tags_field = TagField()
--- a/taskapp/views/task.py	Tue Mar 02 01:05:24 2010 +0530
+++ b/taskapp/views/task.py	Tue Mar 02 02:12:57 2010 +0530
@@ -5,7 +5,7 @@
 
 from pytask.taskapp.models import User, Task, Comment, Claim, Request, Notification
 from pytask.taskapp.utilities.task import getTask
-from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm
+from pytask.taskapp.forms.task import TaskCreateForm, AddMentorForm, AddTaskForm, ChoiceForm, AssignCreditForm, RemoveUserForm, EditTaskForm
 from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask, addMentor
 from pytask.taskapp.views.user import show_msg
 from pytask.taskapp.utilities.user import get_user
@@ -137,15 +137,15 @@
                     
                     if not task:
                         error_msg = "Another task with the same title exists"
-                        return render_to_response('task/create.html',{'form':form, 'error_msg':error_msg})
+                        return render_to_response('task/create.html',{'user':user, 'form':form, 'error_msg':error_msg})
                     
                     addMentor(task, user)
                     updateTask(task,tags_field=data['tags_field'])
-                    if publish: publishTask(task)    
+                    # if publish: publishTask(task)    
                     task_url = '/task/view/tid=%s'%task.id
                     return redirect(task_url)
                 else:
-                    return render_to_response('task/create.html',{'form':form})
+                    return render_to_response('task/create.html',{'user':user, 'form':form})
             else:
                 form = TaskCreateForm()
                 return render_to_response('task/create.html',{'form':form})
@@ -521,9 +521,44 @@
     and then give the user fields accordingly.
     """
     
-    task = Task.objects.get(id=tid) 
+    task = Task.objects.get(id=tid)
+    task_url = "/task/view/tid=%s"%tid
     user = get_user(request.user) if request.user.is_authenticated() else request.user
 
+    is_mentor = True if user in task.mentors.all() else False
+    can_edit = True if is_mentor and task.status == "UP" else False
+
+    if can_edit:
+        form = EditTaskForm(task)
+        if request.method=="POST":
+            data = request.POST
+            form = EditTaskForm(task, data)
+            if form.is_valid():
+                data = form.cleaned_data
+                title = data['title']
+                try:
+                    prev_task = Task.objects.get(title=title)
+                    if prev_task != task:
+                        error_msg = "Another task exists with the same title"
+                        return render_to_response('task/edittask.html',{'user':user, 'form':form, 'error_msg':error_msg})
+                except Task.DoesNotExist:
+                    pass
+                task.title = title
+                task.desc = data['desc']
+                task.tags_field = data['tags_field']
+                task.credits = data['credits']
+                task.save()
+                return redirect(task_url)
+            else:
+                return render_to_response('task/edittask.html',{'user':user, 'form':form})
+        else:
+            return render_to_response('task/edittask.html',{'user':user, 'form':form})
+    else:
+        return show_msg(user, "You cannot edit the task at this stage", task_url, "view the task")
+
+
+
+
 def complete_task(request, tid):
     """ call the event called complete task.
     and also pass it the current user to know who marked it as complete.