combine home page ListView with the CourseSaveView and fixes form_invalid handling
authorSeth Woodworth <seth@sethish.com>
Wed, 27 Mar 2013 21:50:24 +0000 (17:50 -0400)
committerSeth Woodworth <seth@sethish.com>
Wed, 27 Mar 2013 21:50:24 +0000 (17:50 -0400)
karmaworld/apps/courses/views.py
karmaworld/urls.py

index d65330cb3278da9aafcad9a2284779f207f033fc..f40382eac92b9fb2579a94a08f298e4dcf94da98 100644 (file)
@@ -1,26 +1,60 @@
 #!/usr/bin/env python
 # -*- coding:utf8 -*-
 # Copyright (C) 2012  FinalsClub Foundation
+""" Views for the KarmaNotes Courses app """
 
 import json
 
-from django.db.models import Q
 from django.http import HttpResponse
 from django.views.generic import DetailView
-from django.views.generic import FormView
-from django.views.generic import CreateView
 from django.views.generic import TemplateView
-from django.views.generic.base import View
-from django.views.generic.detail import SingleObjectMixin
 from django.views.generic.edit import ProcessFormView
 from django.views.generic.edit import ModelFormMixin
-from django.views.generic.list import BaseListView
+from django.views.generic.list import ListView
 
 from karmaworld.apps.courses.forms import CourseForm
 from karmaworld.apps.courses.models import Course
 from karmaworld.apps.courses.models import School
 
 
+class CourseListView(ListView, ModelFormMixin, ProcessFormView):
+    """ Simple ListView for the front page that includes the CourseForm """
+    model = Course
+    form_class = CourseForm
+    object = Course()
+
+    def get_context_data(self, **kwargs):
+        """ Add the CourseForm to ListView context """
+        print "CourseListView context data getting"
+        # self.get() populates the object_list, fix this for form POST and redirect
+        # get the original context
+        context = super(CourseListView, self).get_context_data(**kwargs)
+        print "returned context and getting course_form"
+        context['course_form'] = kwargs.get('course_form', CourseForm())
+        if context['course_form'].errors:
+            # if there was an error in the form
+            context['jump_to_form'] = True
+
+        return context
+
+
+    def get_success_url(self):
+        """ On form submission success, redirect to what url """
+        return u'/{school_slug}/{course_slug}'.format(
+                school_slug=self.object.school.slug,
+                course_slug=self.object.slug
+            )
+
+    def form_invalid(self, form, **kwargs):
+        """ override form_invalid to populate object_list on redirect """
+        kwargs['is_error'] = True
+        kwargs['course_form'] = form
+        self.object_list = self.get_queryset()
+        kwargs['object_list'] = self.object_list
+        return self.render_to_response(self.get_context_data(**kwargs))
+
+
+
 class CourseDetailView(DetailView):
     """ Class-based view for the course html page """
     model = Course
@@ -38,29 +72,6 @@ class AboutView(TemplateView):
         return context
 
 
-class CourseSaveView(ModelFormMixin, ProcessFormView):
-    """ Save a course form and then view that course page """
-    # TODO: make this not use a genericview
-    form_class = CourseForm
-    model = Course
-    template_name = 'course/course_detail.html'
-    object = Course()
-
-    def get_success_url(self):
-        """ On form submission success, redirect to what url """
-        return u'/{school_slug}/{course_slug}'.format(
-                school_slug=self.object.school.slug,
-                course_slug=self.object.slug
-            )
-
-    def form_invalid(self, form):
-        # TODO: create stand-alone version of form with errors
-        print "form invalid"
-        print dir(form)
-        print form.errors
-        print "\n\n"
-
-
 def school_list(request):
     """ Return JSON describing Schools that match q query on name """
     if request.method == 'POST' and request.is_ajax() \
@@ -73,4 +84,4 @@ def school_list(request):
         return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
     else:
         # else return that the api call failed
-        return HttpResponse(json.dump({'status':'fail'}), mimetype="application/json")
+        return HttpResponse(json.dumps({'status':'fail'}), mimetype="application/json")
index 60d06e63cff8dbde7147ba62033aa95828e2ecd5..ef0788d39f51544900bba00f58a7752fae46ec54 100644 (file)
@@ -11,7 +11,7 @@ from karmaworld.apps.ajaxuploader.views import ajax_uploader
 from karmaworld.apps.courses.models import Course
 from karmaworld.apps.courses.views import AboutView
 from karmaworld.apps.courses.views import CourseDetailView
-from karmaworld.apps.courses.views import CourseSaveView
+from karmaworld.apps.courses.views import CourseListView
 from karmaworld.apps.courses.views import school_list
 from karmaworld.apps.notes.views import NoteView
 from karmaworld.apps.notes.views import raw_file
@@ -51,9 +51,7 @@ urlpatterns = patterns('',
     url(r'^ajax-upload$', ajax_uploader, name='ajax_upload'),
     # return json list of schools
     url(r'^school/list/$', school_list, name='json_school_list'),
-    url(r'^course/post/$', CourseSaveView.as_view(), name='api_course_post'),
-
     # ---- end JSON views ----#
 
-    url(r'^$', ListView.as_view(model=Course), name='home'),
+    url(r'^$', CourseListView.as_view(model=Course), name='home'),
 )