# -*- 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 """
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'
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()
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'),