edit textbook works fine
authorNishanth Amuluru <nishanth@fossee.in>
Tue, 11 Jan 2011 00:34:23 +0530
changeset 373 014d812e625e
parent 372 563fe356947d
child 374 cdd8026ee60e
edit textbook works fine
pytask/taskapp/forms.py
pytask/taskapp/urls.py
pytask/taskapp/views.py
pytask/templates/task/edit_textbook.html
--- a/pytask/taskapp/forms.py	Tue Jan 11 00:23:47 2011 +0530
+++ b/pytask/taskapp/forms.py	Tue Jan 11 00:34:23 2011 +0530
@@ -92,6 +92,12 @@
         model = TextBook
         fields = ['name', 'chapters', 'tags_field']
 
+class EditTextbookForm(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/urls.py	Tue Jan 11 00:23:47 2011 +0530
+++ b/pytask/taskapp/urls.py	Tue Jan 11 00:34:23 2011 +0530
@@ -2,7 +2,7 @@
 
 from pytask.taskapp.views import create_task, view_task, claim_task, \
         select_user, edit_task, create_textbook, view_textbook, \
-        browse_textbooks
+        browse_textbooks, edit_textbook
 
 from pytask.views import under_construction
 
@@ -17,6 +17,7 @@
 
             (r'^textbook/create/$', create_textbook),
             (r'^textbook/view/tid=(\w+)/$', view_textbook),
+            (r'^textbook/edit/tid=(\w+)/$', edit_textbook),
             (r'^textbook/browse/$', browse_textbooks),
 )
 
--- a/pytask/taskapp/views.py	Tue Jan 11 00:23:47 2011 +0530
+++ b/pytask/taskapp/views.py	Tue Jan 11 00:34:23 2011 +0530
@@ -15,7 +15,8 @@
 from pytask.taskapp.models import Task, TaskComment, TaskClaim, TextBook
 from pytask.taskapp.forms import CreateTaskForm, EditTaskForm, \
                                  TaskCommentForm, ClaimTaskForm, \
-                                 ChoiceForm, EditTaskForm, CreateTextbookForm
+                                 ChoiceForm, EditTaskForm, CreateTextbookForm,\
+                                 EditTextbookForm
 from pytask.taskapp.utils import getTask, getTextBook
 from pytask.profile.utils import get_notification
 
@@ -270,6 +271,41 @@
     return render_to_response("task/browse_textbooks.html", context)
 
 @login_required
+def edit_textbook(request, tid):
+
+    user = request.user
+    profile = user.get_profile()
+
+    textbook = getTextBook(tid)
+    textbook_url = "/task/textbook/view/tid=%s"%textbook.uniq_key
+
+    can_edit = True if user == textbook.created_by and textbook.status == "UP"\
+                       else False
+
+    if not can_edit:
+        raise Http404
+
+    context = {"user": user,
+               "profile": profile,
+               "textbook": textbook,
+              }
+
+    context.update(csrf(request))
+
+    if request.method == "POST":
+        form = EditTextbookForm(request.POST, instance=textbook)
+        if form.is_valid():
+            form.save()
+            return redirect(textbook_url)
+        else:
+            context.update({"form": form})
+            return render_to_response("task/edit_textbook.html", context)
+    else:
+        form = EditTextbookForm(instance=textbook)
+        context.update({"form": form})
+        return render_to_response("task/edit_textbook.html", context)
+
+@login_required
 def claim_task(request, tid):
 
     task_url = "/task/view/tid=%s"%tid
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/task/edit_textbook.html	Tue Jan 11 00:34:23 2011 +0530
@@ -0,0 +1,8 @@
+{% extends 'base.html' %}
+{% block content %}
+    <form action="" method="post">
+    {% csrf_token %}
+    {{form.as_p}}
+    <input type="submit" value="Update">
+    </form>
+{% endblock %}