now editing of tasks is done in elegant way.
authornishanth
Tue, 02 Mar 2010 16:55:33 +0530
changeset 181 be8ad7e26176
parent 180 972745147e3f
child 182 1b4253350a3c
now editing of tasks is done in elegant way.
taskapp/forms/task.py
taskapp/views/task.py
--- a/taskapp/forms/task.py	Tue Mar 02 16:20:28 2010 +0530
+++ b/taskapp/forms/task.py	Tue Mar 02 16:55:33 2010 +0530
@@ -22,37 +22,28 @@
 
         return data
 
-def EditTaskForm(task, instance=None):
-    class myForm(forms.ModelForm):
-        class Meta:
-            model = Task
-            fields = ['title', 'desc', 'tags_field', 'credits']
+class EditTaskForm(forms.ModelForm):
+    class Meta:
+        model = Task
+        fields = ['title', 'desc', 'tags_field', 'credits']
 
-        def clean_desc(self):
-            data = self.cleaned_data['desc'].strip()
-            if not data:
-                raise forms.ValidationError("Enter some description for the task")
-
-            return data
+    def clean_desc(self):
+        data = self.cleaned_data['desc'].strip()
+        if not data:
+            raise forms.ValidationError("Enter some description for the task")
 
-        def clean_title(self):
-            data = self.cleaned_data['title'].strip()
-            try:
-                prev_task = Task.objects.exclude(status="DL").get(title__iexact=data)
-                if prev_task != task:
-                    raise forms.ValidationError("Another task with same title exists")
-                else:
-                    return data
-            except:
+        return data
+
+    def clean_title(self):
+        data = self.cleaned_data['title'].strip()
+        try:
+            prev_task = Task.objects.exclude(status="DL").get(title__iexact=data)
+            if prev_task.id != self.instance.id:
+                raise forms.ValidationError("Another task with same title exists")
+            else:
                 return data
-
-    data = {
-        'title': task.title,
-        'desc': task.desc,
-        'tags_field': task.tags_field,
-        'credits': task.credits,
-    }
-    return myForm(instance) if instance else myForm(data)
+        except Task.DoesNotExist:
+            return data
 
 def AddMentorForm(choices,instance=None):
     """ return a form object with appropriate choices """
--- a/taskapp/views/task.py	Tue Mar 02 16:20:28 2010 +0530
+++ b/taskapp/views/task.py	Tue Mar 02 16:55:33 2010 +0530
@@ -530,25 +530,12 @@
     can_edit = True if is_mentor and task.status == "UP" else False
 
     if can_edit:
-        form = EditTaskForm(task)
+        form = EditTaskForm(instance=task)
         if request.method=="POST":
             data = request.POST
-            form = EditTaskForm(task, data)
+            form = EditTaskForm(data, instance=task)
             if form.is_valid():
-                data = form.cleaned_data
-                title = data['title']
-                try:
-                    prev_task = Task.objects.exclude(status="DL").get(title__iexact=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()
+                form.save()
                 return redirect(task_url)
             else:
                 return render_to_response('task/edittask.html',{'user':user, 'form':form})
@@ -634,7 +621,6 @@
     else:
         return show_msg(user, "You are not authorised to do this", task_url, "view the task")
 
-
 def delete_task(request, tid):
     """ mark the task status as DL.
     take a reason from the user and pass on to all the other mentors.