From 4f2d6d5d6592b0af4006dc403363da21e4c977a0 Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Fri, 25 Jan 2013 19:23:00 -0500 Subject: [PATCH] adding untested json endpoint for searching schools --- karmaworld/apps/courses/views.py | 15 +++++++++++++++ karmaworld/urls.py | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index a9dd145..5172bb5 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -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'}) diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 7b597d9..9f96a56 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -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[^/]+)/(?P[^/]+)/(?P[^/]+)$', \ 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'), ) -- 2.25.1