From be87ad49a8e8af0467bdc8fefc0892f4c535773a Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Wed, 23 Jan 2013 19:08:13 -0500 Subject: [PATCH] refactor note views for Note object to handle get and post http methods to save note filemeta form --- karmaworld/apps/notes/views.py | 49 +++++++++++++++++++++++++++++++++- karmaworld/urls.py | 14 +++++----- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index c226f00..c2b83ae 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -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' diff --git a/karmaworld/urls.py b/karmaworld/urls.py index eda60d9..b215e09 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -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\d+)$', raw_file, name='note_raw'), url(r'^(?P[^/]+)/(?P[^/]+)$', \ CourseDetailView.as_view(), name='course_detail'), - # note file as id + + # note file as id, for notes without titles yet url(r'^(?P[^/]+)/(?P[^/]+)/(?P[\d^/]+)$', \ - NoteDetailView.as_view(), name='note_detail'), + NoteView.as_view(), name='note_detail_pk'), # note file by note.slug url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$', \ - NoteDetailView.as_view(), name='note_detail'), + NoteView.as_view(), name='note_detail'), # uploading url(r'ajax-upload$', ajax_uploader, name='ajax_upload'), -- 2.25.1