implemented deleting of a task.
authornishanth
Tue, 02 Mar 2010 03:21:20 +0530
changeset 167 b61e45074ba1
parent 166 ac72d641046e
child 168 42aba20cfe97
implemented deleting of a task.
taskapp/events/task.py
taskapp/models.py
taskapp/utilities/notification.py
taskapp/views/task.py
taskapp/views/user.py
templates/task/view.html
urls.py
--- a/taskapp/events/task.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/taskapp/events/task.py	Tue Mar 02 03:21:20 2010 +0530
@@ -230,8 +230,6 @@
     for a_mentor in task.mentors.all():
         create_notification(role="CM", sent_to=a_mentor, sent_from=marked_by, task=task)
 
-
-
 def closeTask(task, closed_by, reason=None):
     """ set the status of task as CD.
     generate notifications accordingly.
@@ -254,4 +252,16 @@
     for a_mentor in task.mentors.all():
         create_notification(role="CD", sent_to=a_mentor, sent_from=closed_by, task=task, remarks=reason)
 
+def deleteTask(task, deleted_by, reason=None):
+    """ set the task status as DL
+    notify all its other viewers about the deleting of task.
+    """
 
+    task.status = "DL"
+    task.save()
+
+    pending_requests = task.request_task.filter(is_replied=False,is_valid=True)
+    pending_requests.update(is_valid=False)
+
+    for a_mentor in task.mentors.exclude(id=deleted_by.id):
+        create_notification("DL", sent_to=a_mentor, sent_from=deleted_by, task=task, remarks=reason)
--- a/taskapp/models.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/taskapp/models.py	Tue Mar 02 03:21:20 2010 +0530
@@ -43,11 +43,9 @@
     ("RU", "Remove user"), ## remove from working users list in task
 )
 
-
 IMAGES_DIR = "./images"
 UPLOADS_DIR = "./uploads"
 
-
 class CustomImageStorage(FileSystemStorage):
 
     def path(self, name):
@@ -87,7 +85,6 @@
     def __unicode__(self):
         return unicode(self.user.username)
 
-
 class Task(models.Model):
     
     prim_key = models.AutoField(primary_key = True)
@@ -117,7 +114,6 @@
     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')
@@ -169,7 +165,6 @@
     sent_from = models.ForeignKey(User, related_name = "%(class)s_sent_from", null = True, blank = True)
     task = models.ForeignKey(Task, related_name = "%(class)s_sent_for", null = True, blank = True)
 
-
     sub = models.CharField(max_length = 100)
     message = models.TextField()
     remarks = models.CharField(max_length = 100)
--- a/taskapp/utilities/notification.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/taskapp/utilities/notification.py	Tue Mar 02 03:21:20 2010 +0530
@@ -169,6 +169,7 @@
     elif role == "AU":
 
         notification.task = task
+        notification.sent_from = sent_from
         added_user = sent_to
         mentor = sent_from
         assigned_by_url = '<a href="/user/view/uid=%s">%s</a>'%(mentor.id, mentor.username)
@@ -194,6 +195,7 @@
     elif role == "RU":
 
         notification.task = task
+        notification.sent_from = sent_from
         removed_user = sent_to
         mentor = sent_from
         removed_by_url = '<a href="/user/view/uid=%s">%s</a>'%(mentor.id, mentor.username)
@@ -207,6 +209,18 @@
             notification.remarks = remarks
             notification.message += "<b>Reason: </b>%s"%(remarks)
 
+    elif role == "DL":
+
+        notification.sent_from = sent_from
+        notification.task = task
+        deleted_by_url = '<a href="/user/view/uid=%s">%s</a>'%(sent_from.id, sent_from.username)
+
+        notification.sub = "Task deleted"
+        notification.message = 'The unpublished task "%s" viewable by you has been deleted by its creator %s.<br />'%(task.title, deleted_by_url)
+
+        if remarks:
+            notification.remarks = remarks
+            notification.message += "<b>Reason: </b>%s"%remarks
 
     notification.save()
 
--- a/taskapp/views/task.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/taskapp/views/task.py	Tue Mar 02 03:21:20 2010 +0530
@@ -6,7 +6,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, EditTaskForm
-from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask, addMentor
+from pytask.taskapp.events.task import createTask, reqMentor, publishTask, addSubTask, addDep, addClaim, assignTask, updateTask, removeTask, removeUser, assignCredits, completeTask, closeTask, addMentor, deleteTask
 from pytask.taskapp.views.user import show_msg
 from pytask.taskapp.utilities.user import get_user
 
@@ -85,8 +85,9 @@
     context['task_viewable'] = True if ( task.status != "UP" ) or is_mentor else False
 
     context['can_publish'] = True if task.status == "UP" and user == task.created_by else False
-    context['can_edit'] = True if ( not claimed_users ) and task.status in ["UP", "LO", "OP"] and is_mentor else False
+    context['can_edit'] = True if task.status == "UP" and is_mentor else False
     context['can_close'] = True if task.status not in ["UP", "CD", "CM"] and is_mentor else False
+    context['can_delete'] = True if task.status == "UP" and user == task.created_by else False
 
     context['can_mod_mentors'] = True if task.status in ["UP", "OP", "LO", "WR"] and is_mentor else False
     context['can_mod_tasks'] = True if task.status in ["UP", "OP", "LO"] and is_mentor else False
@@ -632,3 +633,30 @@
             return show_msg(user, "The task is either already closed or cannot be closed at this stage", task_url, "view the task")
     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.
+    """
+
+    task_url = "/task/view/tid=%s"%tid
+
+    task = getTask(tid)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
+
+    if task.status == "DL":
+        return show_msg(user, "This task no longer exists", '/task/browse/', "to browse other tasks")
+
+    can_delete = True if task.status == "UP" and task.created_by == user else False
+
+    if can_delete:
+        if request.method == "POST":
+            data = request.POST
+            reason = data.get('reason', None)
+            deleteTask(task, user, reason)
+            return show_msg(user, "The task is deleted", '/', "to return to home page")
+        else:
+            return render_to_response('task/delete.html',{'user':user,})
+    else:
+        return show_msg(user, "You are not authorised to do this at this stage", task_url, "view the task")
--- a/taskapp/views/user.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/taskapp/views/user.py	Tue Mar 02 03:21:20 2010 +0530
@@ -18,7 +18,7 @@
 
 about = {
     "addmentors":"about/addmentors.html",
-    "mentor":"about/mentor.html",
+    "mentor":"about/mentor.html", ## - include role in different stages and merge with mentorrights
     ## "claimtask":
     ## "edittask": - contains both about up and pub states
     ## "mentorrights":
--- a/templates/task/view.html	Tue Mar 02 02:25:28 2010 +0530
+++ b/templates/task/view.html	Tue Mar 02 03:21:20 2010 +0530
@@ -10,12 +10,16 @@
             <a href="/task/edit/tid={{task.id}}">Edit task</a>
         {% endif %}
         
+        {% if can_publish %}
+            <a href="/task/publish/tid={{task.id}}">Publish task</a>
+        {% endif %}
+        
         {% if can_close %}
             <a href="/task/close/tid={{task.id}}">Close this task</a>
         {% endif %}
-                    
-        {% if can_publish %}
-            <a href="/task/publish/tid={{task.id}}">Publish task</a>
+        
+        {% if can_delete %}
+            <a href="/task/delete/tid={{task.id}}">Delete task</a>
         {% endif %}
 
         <hr />created by <a href="/user/view/uid={{ task.created_by.id }}">{{ task.created_by.username }}</a>
--- a/urls.py	Tue Mar 02 02:25:28 2010 +0530
+++ b/urls.py	Tue Mar 02 03:21:20 2010 +0530
@@ -26,7 +26,7 @@
     (r'^task/browse/$', taskViews.browse_tasks),
     (r'^task/view/tid=(\w+)$', taskViews.view_task),
     (r'^task/create/$', taskViews.create_task),
-    (r'task/publish/tid=(\w+)/$', taskViews.publish_task),
+    (r'^task/publish/tid=(\w+)/$', taskViews.publish_task),
     (r'^task/addmentor/tid=(\w+)$', taskViews.add_mentor),
     (r'^task/edit/tid=(\w+)$', taskViews.edit_task),
     (r'^task/claim/tid=(\w+)$', taskViews.claim_task),
@@ -37,6 +37,7 @@
     (r'^task/assigncredits/tid=(\w+)$', taskViews.assign_credits),
     (r'^task/complete/tid=(\w+)$', taskViews.complete_task),
     (r'^task/close/tid=(\w+)$', taskViews.close_task),
+    (r'^task/delete/tid=(\w+)$', taskViews.delete_task),
     
     (r'^admin/', include(admin.site.urls)),