add textbook works fine
authorNishanth Amuluru <nishanth@fossee.in>
Sun, 09 Jan 2011 20:26:11 +0530
changeset 130 a4fa11b2cb5c
parent 129 13e171f09941
child 131 602a909e9e16
add textbook works fine
pytask/taskapp/forms.py
pytask/taskapp/models.py
pytask/taskapp/urls.py
pytask/taskapp/views.py
pytask/templates/task/create_textbook.html
--- a/pytask/taskapp/forms.py	Sun Jan 09 19:45:42 2011 +0530
+++ b/pytask/taskapp/forms.py	Sun Jan 09 20:26:11 2011 +0530
@@ -1,5 +1,6 @@
 from django import forms
-from pytask.taskapp.models import Task, WorkReport, TaskComment, TaskClaim
+from pytask.taskapp.models import Task, WorkReport, TaskComment, TaskClaim, \
+                                  TextBook
 
 class CreateTaskForm(forms.ModelForm):
     class Meta:
@@ -85,6 +86,12 @@
     form = myform(data) if data else myform()
     return form
 
+class CreateTextbookForm(forms.ModelForm):
+
+    class Meta:
+        model = TextBook
+        fields = ['name', 'chapters', 'tags_field']
+
 def AddReviewerForm(choices,instance=None):
     """ return a form object with appropriate choices """
     
--- a/pytask/taskapp/models.py	Sun Jan 09 19:45:42 2011 +0530
+++ b/pytask/taskapp/models.py	Sun Jan 09 20:26:11 2011 +0530
@@ -136,7 +136,8 @@
 class TextBook(models.Model):
 
     uniq_key = models.CharField(max_length = 10, unique = True)
-    chapters = models.ManyToManyField(Task, related_name="%(class)s_set")
+    name = models.CharField(max_length = 100)
+    chapters = models.ManyToManyField(Task, related_name="textbooks")
     tags_field = TagField(verbose_name="Tags")
 
     created_by = models.ForeignKey(User, related_name = "created_textbooks")
--- a/pytask/taskapp/urls.py	Sun Jan 09 19:45:42 2011 +0530
+++ b/pytask/taskapp/urls.py	Sun Jan 09 20:26:11 2011 +0530
@@ -1,7 +1,7 @@
 from django.conf.urls.defaults import *
 
 from pytask.taskapp.views import create_task, view_task, claim_task, \
-        select_user, edit_task
+        select_user, edit_task, create_textbook
 
 urlpatterns = patterns('',
 
@@ -10,5 +10,7 @@
             (r'^view/tid=(\w+)$', view_task),
             (r'^claim/tid=(\w+)$', claim_task),
             (r'^select/tid=(\w+)$', select_user),
+
+            (r'^textbook/create/$', create_textbook)
 )
 
--- a/pytask/taskapp/views.py	Sun Jan 09 19:45:42 2011 +0530
+++ b/pytask/taskapp/views.py	Sun Jan 09 20:26:11 2011 +0530
@@ -12,10 +12,10 @@
 from pytask.utils import make_key
 from pytask.views import show_msg
 
-from pytask.taskapp.models import Task, TaskComment, TaskClaim
+from pytask.taskapp.models import Task, TaskComment, TaskClaim, TextBook
 from pytask.taskapp.forms import CreateTaskForm, EditTaskForm, \
                                  TaskCommentForm, ClaimTaskForm, \
-                                 ChoiceForm, EditTaskForm
+                                 ChoiceForm, EditTaskForm, CreateTextbookForm
 from pytask.taskapp.utils import getTask
 from pytask.profile.utils import get_notification
 
@@ -179,8 +179,44 @@
         context.update({"form": form})
         return render_to_response("task/edit.html", context)
 
+@login_required
+def create_textbook(request):
 
+    user = request.user
+    profile = user.get_profile()
 
+    can_create = True if profile.rights != "CT" else False
+    if not can_create:
+        raise Http404
+
+    context = {"user": user,
+               "profile": profile,
+              }
+
+    context.update(csrf(request))
+
+    if request.method == "POST":
+        form = CreateTextbookForm(request.POST)
+        if form.is_valid():
+            data = form.cleaned_data.copy()
+            data.update({"uniq_key": make_key(TextBook),
+                         "created_by": user,
+                         "creation_datetime": datetime.now()})
+            del data['chapters']
+            new_textbook = TextBook(**data)
+            new_textbook.save()
+
+            new_textbook.chapters = form.cleaned_data['chapters']
+
+            textbook_url = "/task/textbook/tid=%s"%new_textbook.uniq_key
+            return redirect(textbook_url)
+        else:
+            context.update({"form": form})
+            return render_to_response("task/create_textbook.html", context)
+    else:
+        form = CreateTextbookForm()
+        context.update({"form": form})
+        return render_to_response("task/create_textbook.html", context)
 
 @login_required
 def claim_task(request, tid):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/task/create_textbook.html	Sun Jan 09 20:26:11 2011 +0530
@@ -0,0 +1,8 @@
+{% extends 'base.html' %}
+{% block content %}
+<form action="" method="post">
+{% csrf_token %}
+{{form.as_p}}
+<input type="submit" value="Create">
+</form>
+{% endblock %}