Add edit and view chapters views.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Tue, 01 Feb 2011 04:34:05 +0530
changeset 543 57b0f8f80ebf
parent 542 23bf9b4611cb
child 544 17c60a9c2439
Add edit and view chapters views.
pytask/taskapp/urls.py
pytask/taskapp/views/task.py
pytask/taskapp/views/textbook.py
pytask/templates/task/view.html
--- a/pytask/taskapp/urls.py	Tue Feb 01 04:33:35 2011 +0530
+++ b/pytask/taskapp/urls.py	Tue Feb 01 04:34:05 2011 +0530
@@ -39,4 +39,10 @@
       name='browse_textbooks'),
   url(r'^textbook/chapter/create/(?P<book_id>\d+)$', 'create_chapter',
       name='create_chapter'),
+  url(r'^textbook/chapter/edit/(?P<book_id>\d+)/(?P<chapter_id>\d+)$',
+      'edit_chapter',
+      name='edit_chapter'),
+  url(r'^textbook/chapter/view/(?P<book_id>\d+)/(?P<chapter_id>\d+)$',
+      'view_chapter',
+      name='view_chapter'),
 )
--- a/pytask/taskapp/views/task.py	Tue Feb 01 04:33:35 2011 +0530
+++ b/pytask/taskapp/views/task.py	Tue Feb 01 04:34:05 2011 +0530
@@ -25,6 +25,7 @@
 
 from tagging.models import Tag
 
+from pytask.helpers import exceptions
 from pytask.views import show_msg
 
 from pytask.profile import models as profile_models
@@ -34,12 +35,14 @@
 
 
 DONT_CLAIM_TASK_MSG = ugettext(
-  "Please don't submit any claims for the tasks until the workshop is "
-  "over. During the workshop we will introduce you to the work-flow of "
-  "this entire project. Also please be warned that the task claim work-"
+  "Please don't submit any claims for the tasks until you get an email "
+  "to start claiming tasks. Please be warned that the task claim work-"
   "flow may change. So all the claims submitted before the workshop may "
   "not be valid.")
 
+NO_EDIT_RIGHT = ugettext(
+  "You are not authorized to edit this page.")
+
 
 @login_required
 def create_task(request):
@@ -130,13 +133,17 @@
                                         RequestContext(request, context))
 
 
-def view_task(request, task_id):
-    """ get the task depending on its task_id and display accordingly if it is a get.
-    check for authentication and add a comment if it is a post request.
+def view_task(request, task_id, **kwargs):
+    """View to get the requested.
+
+    Checks for authentication and add a comment if it is a post request.
     """
 
     context = {}
 
+    if 'context' in kwargs:
+        context.update(kwargs['context'])
+
     # TODO(disable): Disable once the tasks can be claimed
     context['uberbar_message'] = DONT_CLAIM_TASK_MSG
 
@@ -199,7 +206,8 @@
         context['can_approve'] = False
 
     if ((is_creator or user_role != profile_models.ROLES_CHOICES[3][0])
-      and task.status == taskapp_models.TASK_STATUS_CHOICES[0][0]):
+      and task.status in [taskapp_models.TASK_STATUS_CHOICES[0][0],
+      taskapp_models.TASK_STATUS_CHOICES[1][0]]):
         context['can_edit'] = True
     else:
         context['can_edit'] = False
@@ -272,9 +280,16 @@
     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
 
     is_creator = True if user == task.created_by else False
-    can_edit = True if task.status == taskapp_models.TASK_STATUS_CHOICES[0][0] and is_creator else False
+
+    if ((is_creator or profile.role != profile_models.ROLES_CHOICES[3][0])
+      and task.status in [taskapp_models.TASK_STATUS_CHOICES[0][0],
+      taskapp_models.TASK_STATUS_CHOICES[1][0]]):
+        can_edit = True 
+    else:
+        can_edit = False
+
     if not can_edit:
-        raise http.Http404
+        raise exceptions.UnauthorizedAccess(NO_EDIT_RIGHT)
 
     context = {"user": user,
                "profile": profile,
--- a/pytask/taskapp/views/textbook.py	Tue Feb 01 04:33:35 2011 +0530
+++ b/pytask/taskapp/views/textbook.py	Tue Feb 01 04:34:05 2011 +0530
@@ -21,25 +21,29 @@
 
 from tagging.managers import TaggedItem
 
-from pytask.helpers.exceptions import UnauthorizedAccess
+from pytask.helpers import exceptions
 
 from pytask.profile import models as profile_models
 
 from pytask.taskapp import forms as taskapp_forms
 from pytask.taskapp import models as taskapp_models
+from pytask.taskapp.views import task as task_view
 from pytask.taskapp.views.utils import get_intial_tags_for_chapter
 
 
 DONT_CLAIM_TASK_MSG = ugettext(
-  "Please don't submit any claims for the tasks until the workshop is "
-  "over. During the workshop we will introduce you to the work-flow of "
-  "this entire project. Also please be warned that the task claim work-"
+  "Please don't submit any claims for the tasks until you get an email "
+  "to start claiming tasks. Please be warned that the task claim work-"
   "flow may change. So all the claims submitted before the workshop may "
   "not be valid.")
 
 NO_EDIT_RIGHT = ugettext(
   "You are not authorized to edit this page.")
 
+NOT_A_PARENT_FOR_CHAPTER = ugettext(
+  "There is an error in your request. The chapter you are requesting is "
+  "does not belong to the textbook you have requested.")
+
 
 @login_required
 def create_textbook(request):
@@ -210,7 +214,7 @@
         can_edit = False
 
     if not can_edit:
-        raise UnauthorizedAccess(NO_EDIT_RIGHT)
+        raise exceptions.UnauthorizedAccess(NO_EDIT_RIGHT)
 
     context = {
       'user': user,
@@ -295,6 +299,41 @@
           template, RequestContext(request, context))
 
 @login_required
+def edit_chapter(request, book_id, chapter_id,
+                 template='task/chapter_edit.html'):
+    """View function that lets edit chapters from textbooks.
+    """
+    chapter = shortcuts.get_object_or_404(taskapp_models.Task, pk=chapter_id)
+
+    if chapter.parent.id != int(book_id):
+        raise exceptions.PyTaskException(NOT_A_PARENT_FOR_CHAPTER)
+
+    return task_view.edit_task(request, chapter_id)
+
+
+def view_chapter(request, book_id, chapter_id,
+                 template='task/chapter_edit.html'):
+    """View that displays the chapter of the textbook.
+
+    Args:
+        book_id: the id of the book to which this chapter belongs.
+        chapter_id: id of the chapter that must be displayed.
+    """
+
+    chapter = shortcuts.get_object_or_404(taskapp_models.Task, pk=chapter_id)
+
+    if chapter.parent.id != int(book_id):
+        raise exceptions.PyTaskException(NOT_A_PARENT_FOR_CHAPTER)
+
+    context = {
+      'edit_url': reverse('edit_chapter', kwargs={
+        'book_id': book_id, 'chapter_id': chapter_id})
+      }
+    kwargs = {'context': context}
+
+    return task_view.view_task(request, chapter_id, **kwargs)
+
+@login_required
 def approve_textbook(request, task_id):
 
     user = request.user
--- a/pytask/templates/task/view.html	Tue Feb 01 04:33:35 2011 +0530
+++ b/pytask/templates/task/view.html	Tue Feb 01 04:34:05 2011 +0530
@@ -15,7 +15,13 @@
   <h3>{{ task.title }}</h3>
 
   {% if can_edit %}
-    <a href="{% url edit_task task.id %}">Edit task</a>
+    {% if edit_url %}
+      <a href="{{ edit_url }}">
+    {% else %}
+      <a href="{% url edit_task task.id %}">
+    {% endif %}
+      Edit task
+    </a>
   {% endif %}
 
   {% if can_approve %}