3 # Copyright (C) 2012 FinalsClub Foundation
4 """ Views for the KarmaNotes Courses app """
8 from django.http import HttpResponse
9 from django.views.generic import DetailView
10 from django.views.generic import TemplateView
11 from django.views.generic.edit import ProcessFormView
12 from django.views.generic.edit import ModelFormMixin
13 from django.views.generic.list import ListView
15 from karmaworld.apps.courses.forms import CourseForm
16 from karmaworld.apps.courses.models import Course
17 from karmaworld.apps.courses.models import School
18 from karmaworld.apps.notes.models import Note
21 class CourseListView(ListView, ModelFormMixin, ProcessFormView):
22 """ Simple ListView for the front page that includes the CourseForm """
24 form_class = CourseForm
27 def get_context_data(self, **kwargs):
28 """ Add the CourseForm to ListView context """
29 # get the original context
30 context = super(CourseListView, self).get_context_data(**kwargs)
31 # get the total number of notes
32 context['note_count'] = Note.objects.count()
33 # get the course form for the form at the bottom of the homepage
34 context['course_form'] = kwargs.get('course_form', CourseForm())
35 if context['course_form'].errors:
36 # if there was an error in the form
37 context['jump_to_form'] = True
41 def get_success_url(self):
42 """ On form submission success, redirect to what url """
43 return u'/{school_slug}/{course_slug}'.format(
44 school_slug=self.object.school.slug,
45 course_slug=self.object.slug
48 def form_invalid(self, form, **kwargs):
49 """ override form_invalid to populate object_list on redirect """
50 kwargs['is_error'] = True
51 kwargs['course_form'] = form
52 self.object_list = self.get_queryset()
53 kwargs['object_list'] = self.object_list
54 return self.render_to_response(self.get_context_data(**kwargs))
58 class CourseDetailView(DetailView):
59 """ Class-based view for the course html page """
61 context_object_name = u"course" # name passed to template
64 class AboutView(TemplateView):
65 """ Display the About page with the Schools leaderboard """
66 template_name = "about.html"
68 def get_context_data(self, **kwargs):
69 """ get the list of schools with the most files for leaderboard """
70 context = { 'params': kwargs, \
71 'schools': School.objects.all()[:3] }
75 def school_list(request):
76 """ Return JSON describing Schools that match q query on name """
77 if request.method == 'POST' and request.is_ajax() \
78 and request.POST.has_key('q'):
79 # if an ajax get request with a 'q' name query
80 # get the schools as a id name dict,
81 _query = request.POST['q']
82 matching_school_aliases = list(School.objects.filter(alias__icontains=_query))
83 matching_school_names = list(School.objects.filter(name__icontains=_query)[:20])
84 _schools = matching_school_aliases[:2] + matching_school_names
85 schools = [{'id': s.id, 'name': s.name} for s in _schools]
88 return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
90 # else return that the api call failed
91 return HttpResponse(json.dumps({'status':'fail'}), mimetype="application/json")