Add a view, form and related URL for chapter creation under textbook.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Sun, 30 Jan 2011 18:59:23 +0530
changeset 526 9c0c88d129dd
parent 525 d6ad9c1a571b
child 527 c894de293216
Add a view, form and related URL for chapter creation under textbook.
pytask/taskapp/forms.py
pytask/taskapp/urls.py
pytask/taskapp/views.py
pytask/templates/task/chapter_edit.html
--- a/pytask/taskapp/forms.py	Sun Jan 30 18:55:15 2011 +0530
+++ b/pytask/taskapp/forms.py	Sun Jan 30 18:59:23 2011 +0530
@@ -92,6 +92,12 @@
         model = Task
         fields = ['name', 'chapters', 'tags_field']
 
+class CreateChapterForm(forms.ModelForm):
+
+    class Meta:
+        model = Task
+        fields = ['title', 'desc' , 'pynts', 'tags_field']
+
 class EditTextbookForm(forms.ModelForm):
 
     class Meta:
--- a/pytask/taskapp/urls.py	Sun Jan 30 18:55:15 2011 +0530
+++ b/pytask/taskapp/urls.py	Sun Jan 30 18:59:23 2011 +0530
@@ -37,4 +37,6 @@
       name='approved_textbook'),
   url(r'^textbook/browse/$', 'browse_textbooks',
       name='browse_textbooks'),
+  url(r'^textbook/chapter/create/(?P<book_id>\d+)$', 'create_chapter',
+      name='create_chapter'),
 )
--- a/pytask/taskapp/views.py	Sun Jan 30 18:55:15 2011 +0530
+++ b/pytask/taskapp/views.py	Sun Jan 30 18:59:23 2011 +0530
@@ -658,6 +658,67 @@
                                             RequestContext(request, context))
 
 @login_required
+def create_chapter(request, book_id, template='task/chapter_edit.html'):
+    """View function to let Coordinators and TAs (Mentor in
+    PyTask terminology) create chapters out of textbooks.
+
+    Args:
+      book_id: ID of the text book to which this chapter belongs to
+    """
+
+    user = request.user
+    profile = user.get_profile()
+
+    if profile.role != profile_models.ROLES_CHOICES[3][0]:
+        can_create = True
+    else:
+        can_create= False
+
+    if not can_create:
+        raise http.HttpResponseForbidden
+
+    context = {
+      'user': user,
+      'profile': profile,
+      }
+
+    context.update(csrf(request))
+
+    textbook = shortcuts.get_object_or_404(taskapp_models.Task, pk=book_id)
+    initial_tags = ', '.join([textbook.tags_field] + ['Chapter'])
+
+    if request.method == 'POST':
+        form = taskapp_forms.CreateChapterForm(request.POST)
+        if form.is_valid():
+            data = form.cleaned_data.copy()
+
+            data.update({
+              'created_by': user,
+              'creation_datetime': datetime.now(),
+              'parent': textbook,
+              })
+
+            # TODO: remove hard coded default publish for chapters
+            data['status'] = 'Open'
+            new_chapter = taskapp_models.Task(**data)
+            new_chapter.save()
+
+            textbook_url = reverse(
+              'view_textbook', kwargs={'task_id': textbook.id})
+            return shortcuts.redirect(textbook_url)
+        else:
+            context.update({"form": form})
+            return shortcuts.render_to_response(
+              template, RequestContext(request, context))
+    else:
+        form = taskapp_forms.CreateChapterForm(
+          initial={'tags_field': initial_tags})
+        context.update({'form': form})
+        return shortcuts.render_to_response(
+          template, RequestContext(request, context))
+
+
+@login_required
 def claim_task(request, task_id):
 
     context = {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/task/chapter_edit.html	Sun Jan 30 18:59:23 2011 +0530
@@ -0,0 +1,24 @@
+{% extends "base.html" %}
+
+{% load form_helpers %}
+
+{% block css %}
+  <style>
+    .ui-autocomplete-loading {
+      background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
+    }
+  </style>
+{% endblock %}
+
+{% block js %}
+    <script type="text/javascript"
+        src="/pytask/static/js/create_task_form.js">
+    </script>
+    <script type="text/javascript">
+      create_form("{% url suggest_task_tags %}");
+    </script>
+{% endblock %}
+
+{% block content %}
+  {% as_div_form form "Create Chapter Form" csrf_token "Submit" %}
+{% endblock %}