Merge branch 'master' of github.com:FinalsClub/karmaworld
[oweals/karmaworld.git] / karmaworld / apps / courses / views.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4
5 import json
6
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
18
19 from karmaworld.apps.courses.forms import CourseForm
20 from karmaworld.apps.courses.models import Course
21 from karmaworld.apps.courses.models import School
22
23
24 class CourseDetailView(DetailView):
25     """ Class-based view for the course html page """
26     model = Course
27     context_object_name = u"course" # name passed to template
28
29
30 class AboutView(TemplateView):
31     """ Display the About page with the Schools leaderboard """
32     template_name = "about.html"
33
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] }
38         return context
39
40
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
45     model = Course
46     template_name = 'course/course_detail.html'
47     object = Course()
48
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
54             )
55
56     def form_invalid(self, form):
57         # TODO: create stand-alone version of form with errors
58         print "form invalid"
59         print dir(form)
60         print form.errors
61         print "\n\n"
62
63
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]]
72         # return as json
73         return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
74     else:
75         # else return that the api call failed
76         return HttpResponse(json.dump({'status':'fail'}), mimetype="application/json")