our task model does not meet out needs. so modified it and added a model called map. made the changes in views accordingly.phew!!!. this one took the hell out of me :( .
authornishanth
Thu, 25 Feb 2010 03:47:23 +0530
changeset 90 b2426897ff18
parent 89 1cc03941ed5d
child 91 1b5ad4b7c40e
our task model does not meet out needs. so modified it and added a model called map. made the changes in views accordingly.phew!!!. this one took the hell out of me :( .
taskapp/events/task.py
taskapp/forms/task.py
taskapp/models.py
taskapp/views/task.py
--- a/taskapp/events/task.py	Thu Feb 25 02:11:49 2010 +0530
+++ b/taskapp/events/task.py	Thu Feb 25 03:47:23 2010 +0530
@@ -1,12 +1,15 @@
 from datetime import datetime
-from pytask.taskapp.models import Profile, Task, Comment, Credit, Claim
+from pytask.taskapp.models import Profile, Task, Comment, Credit, Claim, Map
 
 def publishTask(task):
     """ set the task status to open """
-    
-    sub_tasks = task.subs.all()
-    dependencies = task.deps.all()
-    if sub_tasks or any(map(lambda t:t.status!="CM",dependencies)):
+
+    if task.sub_type == 'D':
+        deps, subs = task.map_subs.all(), []
+    else:
+        subs, deps = task.map_subs.all(), []
+   
+    if subs or any(map(lambda t:t.status!="CM",deps)):
         task.status = "LO"
     else:
         task.status = "OP"
@@ -20,8 +23,20 @@
 
     ## Shall modify after talking to pr about subtasks
     ## I think i might even remove the concept of subtasks
-    main_task.subs.add(sub_task)
-    sub_tasks = main_task.subs.all()
+
+    main_task.sub_type = "S"
+    main_task.save()
+
+    try:
+        mapobj = Map.objects.get(main=main_task)
+    except Map.DoesNotExist:
+        mapobj = Map()
+        mapobj.main = main_task
+        mapobj.save()
+    mapobj.subs.add(sub_task)
+    mapobj.save()
+
+    sub_tasks = getTask(main_task.id).subs
     if main_task.status == "OP":
         if any(map(lambda t:t.status!="CM",sub_tasks)):
             main_task.status = "LO"
@@ -36,8 +51,21 @@
     And also if the task doesn't have any subs.
     """
 
-    main_task.deps.add(dependency)
-    deps = main_task.deps.all()
+    main_task.sub_type = "D"
+    main_task.save()
+
+    try:
+        mapobj = Map.objects.get(main=main_task)
+    except Map.DoesNotExist:
+        mapobj = Map()
+        mapobj.main = main_task
+        mapobj.save()
+
+    mapobj.subs.add(dependency)
+    mapobj.save()
+
+    deps = getTask(main_task.id).deps
+
     if main_task.status in ["OP", "LO"]: 
         if all(map(lambda t:t.status=="CM",deps)):
             main_task.status = "OP"
@@ -99,9 +127,21 @@
     """
 
     task = Task.objects.get(id=tid)
-    deps = task.deps.all()
-    subs = task.subs.all()
+    try:
+        mapobj = Map.objects.get(main=task)
+    except Map.DoesNotExist:
+        mapobj = Map()
+        mapobj.main = task
+        mapobj.save()
+        
+    task_subs = mapobj.subs.all()
 
+    if task.sub_type == "D":
+        task.deps, task.subs = task_subs, []
+    elif task.sub_type == "S":
+        task.subs, task.deps = task_subs, []
+
+    deps, subs = task.deps, task.subs
     if deps and task.status in ["OP", "LO"]:
         task.status = "OP" if all(map(lambda t:t.status=="CM",deps)) else "LO"
     if subs and task.status in ["OP", "LO", "CM"]:
--- a/taskapp/forms/task.py	Thu Feb 25 02:11:49 2010 +0530
+++ b/taskapp/forms/task.py	Thu Feb 25 03:47:23 2010 +0530
@@ -39,5 +39,5 @@
             type_choices = [('S','Subtasks'),('D','Dependencies')]
             type = forms.ChoiceField(type_choices, widget=forms.RadioSelect)
 
-        task = forms.MultipleChoiceField(choices=task_choices)
+        task = forms.ChoiceField(choices=task_choices)
     return myForm()
--- a/taskapp/models.py	Thu Feb 25 02:11:49 2010 +0530
+++ b/taskapp/models.py	Thu Feb 25 03:47:23 2010 +0530
@@ -75,9 +75,6 @@
 #    tags = models.CharField(max_length = 200, blank = True)
     tags_field = TagField()
     
-    subs = models.ManyToManyField('self', blank = True, related_name = "%(class)s_parents")
-    deps = models.ManyToManyField('self', blank = True, related_name = "%(class)s_deps")
-    
     credits = models.PositiveSmallIntegerField()
     progress = models.PositiveSmallIntegerField(default = 0)
         
@@ -87,7 +84,7 @@
     assigned_users = models.ManyToManyField(User, blank = True, related_name = "%(class)s_assigned_users")
     
     creation_datetime = models.DateTimeField()
-    
+    sub_type = models.CharField(max_length=1, choices = (('D','Dependency'),('S','Subtask')), default = 'D')   
     #is_claimable = models.BooleanField()
     
     ## not yet decided if attribs after this are to be included
@@ -97,6 +94,12 @@
     def __unicode__(self):
         return unicode(self.title)
 
+class Map(models.Model):
+
+    main = models.ForeignKey('Task', related_name = "%(class)s_main")
+    subs = models.ManyToManyField('Task', blank = True, null = True, related_name = "%(class)s_subs")
+
+
 class Comment(models.Model):
     
     task = models.ForeignKey('Task')
--- a/taskapp/views/task.py	Thu Feb 25 02:11:49 2010 +0530
+++ b/taskapp/views/task.py	Thu Feb 25 03:47:23 2010 +0530
@@ -33,8 +33,9 @@
     task = getTask(tid)
     comments = Comment.objects.filter(task=task)
     mentors = task.mentors.all()
-    subs = task.subs.all()
-    deps = task.deps.all()
+
+    deps, subs = task.deps, task.subs
+    
     errors = []
     
     is_guest = True if not user.is_authenticated() else False
@@ -62,7 +63,6 @@
     if request.method == 'POST':
         if not is_guest:
             data = request.POST["data"]
-            task = getTask(tid)
             new_comment = Comment(task=task, data=data, created_by=user, creation_datetime=datetime.now())
             new_comment.save()
             return redirect(task_url)
@@ -162,15 +162,13 @@
     user = request.user
     task = getTask(tid)
 
-    deps = task.deps.all()
-    subs = task.subs.all()
-
+    deps, subs = task.deps, task.subs
     is_plain = False if deps or subs else True
 
     ## again a brute force method
     valid_tasks = []
     for a_task in Task.objects.all():
-        if not ( a_task.status in deps or a_task in subs or a_task.status=="CD" or a_task==task ):
+        if not ( a_task in deps or a_task in subs or a_task.status=="CD" or a_task==task ):
             valid_tasks.append(a_task)
 
     task_choices = [ (_.id,_.title) for _ in valid_tasks ]
@@ -185,7 +183,6 @@
                 ## first decide if adding subs and deps can be in same page
                 ## only exclude tasks with status deleted
                 data = request.POST
-                print data
                 if is_plain and not data.get('type', None): errors.append('Please choose which type of task(s) do you want to add.')
                 if not data.get('task', None): errors.append('Please choose a one task')
 
@@ -202,7 +199,6 @@
                     ## we might iterate over a task list later on
                     task_id = data['task']
                     sub_or_dep = getTask(task_id)
-                    print task_id, sub_or_dep
                     update_method(task, sub_or_dep)
 
                     return redirect(task_url)