adding untested json endpoint for searching schools
authorSeth Woodworth <seth@sethish.com>
Sat, 26 Jan 2013 00:23:00 +0000 (19:23 -0500)
committerSeth Woodworth <seth@sethish.com>
Sat, 26 Jan 2013 00:23:00 +0000 (19:23 -0500)
karmaworld/apps/courses/views.py
karmaworld/urls.py

index a9dd145b3716370b3aa3176a73fa44c40ada0eaa..5172bb5b1504e2f3cec4bb0a458cfc8bc7fa74a0 100644 (file)
@@ -2,6 +2,8 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2012  FinalsClub Foundation
 
+import json
+
 from django.views.generic import DetailView
 from django.views.generic import TemplateView
 
@@ -25,3 +27,16 @@ class AboutView(TemplateView):
         context = { 'params': kwargs, \
                     'schools': School.objects.all()[:3] }
         return context
+
+
+def school_list(request):
+    """ Return JSON describing Schools that match q query on name """
+    if request.method == 'GET' and request.is_ajax() \
+                        and request.GET.has_key('q'):
+        # if an ajax get request with a 'q' name query
+        # get the list of schools that match and return
+        schools = School.objects.filter(name__icontains=request.GET['q'])[:4]
+        return json.dump({'status':'success', 'schools': schools})
+    else:
+        # else return that the api call failed
+        return json.dump({'status':'fail'})
index 7b597d93c66764c544c5757e5257f3ad856923ac..9f96a5611534132c5d74a7bd8d70503572f97026 100644 (file)
@@ -11,6 +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 school_list
 from karmaworld.apps.notes.views import NoteView
 from karmaworld.apps.notes.views import raw_file
 
@@ -38,8 +39,12 @@ urlpatterns = patterns('',
     url(r'^(?P<school_slug>[^/]+)/(?P<course_slug>[^/]+)/(?P<slug>[^/]+)$', \
         NoteView.as_view(), name='note_detail'),
 
-    # uploading
-    url(r'ajax-upload$', ajax_uploader, name='ajax_upload'),
+    # ---- JSON views ----#
+    # uploading files
+    url(r'^ajax-upload$', ajax_uploader, name='ajax_upload'),
+    # return json list of schools
+    url(r'^j/school/list$', school_list, name='json_school_list'),
+    # ---- end JSON views ----#
 
     url(r'^$', ListView.as_view(model=Course), name='home'),
 )