From 7c6787751541c2fedf20c625f2ae92267779d2fa Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Wed, 27 Mar 2013 17:50:24 -0400 Subject: [PATCH] combine home page ListView with the CourseSaveView and fixes form_invalid handling --- karmaworld/apps/courses/views.py | 71 ++++++++++++++++++-------------- karmaworld/urls.py | 6 +-- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index d65330c..f40382e 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -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") diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 60d06e6..ef0788d 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -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'), ) -- 2.25.1