#!/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
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() \
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")
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
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'),
)