3 # Copyright (C) 2012 FinalsClub Foundation
7 from django.db.models import Q
8 from django.http import HttpResponse
9 from django.views.generic import DetailView
10 from django.views.generic import FormView
11 from django.views.generic import CreateView
12 from django.views.generic import TemplateView
13 from django.views.generic.base import View
14 from django.views.generic.detail import SingleObjectMixin
15 from django.views.generic.edit import ProcessFormView
16 from django.views.generic.edit import ModelFormMixin
17 from django.views.generic.list import BaseListView
19 from karmaworld.apps.courses.forms import CourseForm
20 from karmaworld.apps.courses.models import Course
21 from karmaworld.apps.courses.models import School
24 class CourseDetailView(DetailView):
25 """ Class-based view for the course html page """
27 context_object_name = u"course" # name passed to template
30 class AboutView(TemplateView):
31 """ Display the About page with the Schools leaderboard """
32 template_name = "about.html"
34 def get_context_data(self, **kwargs):
35 """ get the list of schools with the most files for leaderboard """
36 context = { 'params': kwargs, \
37 'schools': School.objects.all()[:3] }
41 class CourseSaveView(ModelFormMixin, ProcessFormView):
42 """ Save a course form and then view that course page """
43 # TODO: make this not use a genericview
44 form_class = CourseForm
46 template_name = 'course/course_detail.html'
49 def get_success_url(self):
50 """ On form submission success, redirect to what url """
51 return u'/{school_slug}/{course_slug}'.format(
52 school_slug=self.object.school.slug,
53 course_slug=self.object.slug
56 def form_invalid(self, form):
57 # TODO: create stand-alone version of form with errors
64 def school_list(request):
65 """ Return JSON describing Schools that match q query on name """
66 if request.method == 'POST' and request.is_ajax() \
67 and request.POST.has_key('q'):
68 # if an ajax get request with a 'q' name query
69 # get the schools as a id name dict,
70 schools = [{'id': s.id, 'name': s.name} for s in \
71 School.objects.filter(name__icontains=request.POST['q'])[:4]]
73 return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
75 # else return that the api call failed
76 return HttpResponse(json.dump({'status':'fail'}), mimetype="application/json")