refactor note views for Note object to handle get and post http methods to save note...
authorSeth Woodworth <seth@sethish.com>
Thu, 24 Jan 2013 00:08:13 +0000 (19:08 -0500)
committerSeth Woodworth <seth@sethish.com>
Thu, 24 Jan 2013 00:08:13 +0000 (19:08 -0500)
karmaworld/apps/notes/views.py
karmaworld/urls.py

index c226f001cc30c8f33e4b4cc463789ea2ac29888f..c2b83ae3e7f1135141d9122a688877544bbb5e7f 100644 (file)
@@ -2,10 +2,15 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2012  FinalsClub Foundation
 
+from django.http import HttpResponse
 from django.views.generic import DetailView
+from django.views.generic import FormView
+from django.views.generic import View
+from django.views.generic.detail import SingleObjectMixin
 from django.shortcuts import get_object_or_404
-from django.http import HttpResponse
+
 from karmaworld.apps.notes.models import Note
+from karmaworld.apps.notes.forms import NoteForm
 
 class NoteDetailView(DetailView):
     """ Class-based view for the note html page """
@@ -13,6 +18,48 @@ class NoteDetailView(DetailView):
     context_object_name = u"note"
     model = Note
 
+
+class NoteSaveView(FormView, SingleObjectMixin):
+    template_name = 'notes/note_detail.html'
+    form_class = NoteForm
+    model = Note
+
+    def get_context_data(self, **kwargs):
+        context = {
+            'object': self.get_object(),
+        }
+        return super(NoteSaveView, self).get_context_data(**context)
+
+    def get_success_url(self):
+        return u'/{school_slug}/{course_slug}/{pk}'.format(
+                school_slug=self.object.course.school.slug,
+                course_slug=self.object.course.slug,
+                pk=self.object.pk
+            )
+
+    def form_valid(self, form):
+        self.object = self.get_object()
+        self.object.name = form.cleaned_data['name']
+        self.object.desc = form.cleaned_data['desc']
+        # use *arg expansion to pass tags a list of tags
+        self.object.tags.add(*form.cleaned_data['tags'])
+        self.object.save()
+        return super(NoteSaveView, self).form_valid(form)
+
+
+class NoteView(View):
+    """ Notes superclass that wraps http methods """
+
+    def get(self, request, *args, **kwargs):
+        view = NoteDetailView.as_view()
+        return view(request, *args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        view = NoteSaveView.as_view()
+        return view(request, *args, **kwargs)
+
+
+
 class RawNoteDetailView(DetailView):
     """ Class-based view for the raw note html for iframes """
     template_name = u'notes/note_raw.html'
index eda60d97f20e13124165144ed39f27e74d20a2bd..b215e0981f86c08eebf482086d27842fb8a0e50c 100644 (file)
@@ -4,13 +4,14 @@
 
 from django.contrib import admin
 from django.conf.urls.defaults import patterns, include, url
-from django.views.generic import ListView, DetailView
+from django.views.generic import ListView
 from django.views.generic.simple import direct_to_template
 
 from karmaworld.apps.ajaxuploader.views import ajax_uploader
 from karmaworld.apps.courses.models import Course
 from karmaworld.apps.courses.views import CourseDetailView
-from karmaworld.apps.notes.views import NoteDetailView, RawNoteDetailView, raw_file
+from karmaworld.apps.notes.views import NoteView
+from karmaworld.apps.notes.views import raw_file
 
 # See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf
 admin.autodiscover()
@@ -23,17 +24,16 @@ urlpatterns = patterns('',
 
     url(r'^about/$', direct_to_template, { 'template': 'about.html' }, name='about'),
 
-    # the raw route must come before routes with a capture group after the
-    # first / of the url
     url(r'^raw/(?P<pk>\d+)$', raw_file, name='note_raw'),
     url(r'^(?P<school_slug>[^/]+)/(?P<slug>[^/]+)$', \
         CourseDetailView.as_view(), name='course_detail'),
-    # note file as id
+
+    # note file as id, for notes without titles yet
     url(r'^(?P<school_slug>[^/]+)/(?P<course_slug>[^/]+)/(?P<pk>[\d^/]+)$', \
-        NoteDetailView.as_view(), name='note_detail'),
+        NoteView.as_view(), name='note_detail_pk'),
     # note file by note.slug
     url(r'^(?P<school_slug>[^/]+)/(?P<course_slug>[^/]+)/(?P<slug>[^/]+)$', \
-        NoteDetailView.as_view(), name='note_detail'),
+        NoteView.as_view(), name='note_detail'),
 
     # uploading
     url(r'ajax-upload$', ajax_uploader, name='ajax_upload'),