From 2abd93d0cfd62209ca31aa7d8fdc9063e18d66f7 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Fri, 13 Dec 2013 17:01:40 -0500 Subject: [PATCH] Autocomplete course names --- karmaworld/apps/courses/views.py | 35 ++++++++++++++++++-- karmaworld/assets/js/add-course.js | 33 +++++++++++++++++- karmaworld/templates/partial/add_course.html | 3 +- karmaworld/urls.py | 3 ++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 717e245..ab2c57e 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -5,7 +5,10 @@ import json -from django.http import HttpResponse +from django.core.exceptions import MultipleObjectsReturned +from django.core.exceptions import ObjectDoesNotExist + +from django.http import HttpResponse, HttpResponseNotFound from django.views.generic import DetailView from django.views.generic import TemplateView from django.views.generic.edit import ProcessFormView @@ -17,7 +20,6 @@ from karmaworld.apps.courses.models import Course from karmaworld.apps.courses.models import School from karmaworld.apps.notes.models import Note - class CourseListView(ListView, ModelFormMixin, ProcessFormView): """ Simple ListView for the front page that includes the CourseForm """ model = Course @@ -102,3 +104,32 @@ def school_list(request): else: # else return that the api call failed return HttpResponse(json.dumps({'status':'fail'}), mimetype="application/json") + + +def school_course_list(request): + """Return JSON describing courses we know of at the given school + that match the query """ + if request.method == 'POST' and request.is_ajax() \ + and request.POST.has_key('q')\ + and request.POST.has_key('school_id'): + + _query = request.POST['q'] + try: + _school_id = int(request.POST['school_id']) + except: + return HttpResponseNotFound(json.dumps({'status': 'fail', 'message':'could not convert school id to integer'}), mimetype="application/json") + + # Look up the school + try: + school = School.objects.get(id__exact=_school_id) + except (MultipleObjectsReturned, ObjectDoesNotExist): + return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'school id did not match exactly one school'}), mimetype="application/json") + + _courses = Course.objects.filter(school__exact=school.id, name__icontains=_query) + courses = [{'name': c.name} for c in _courses] + + # return as json + return HttpResponse(json.dumps({'status':'success', 'courses': courses}), mimetype="application/json") + else: + # else return that the api call failed + return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'query parameters missing'}), mimetype="application/json") diff --git a/karmaworld/assets/js/add-course.js b/karmaworld/assets/js/add-course.js index 5aec010..f9f0e07 100644 --- a/karmaworld/assets/js/add-course.js +++ b/karmaworld/assets/js/add-course.js @@ -9,6 +9,8 @@ $(function() { $('#add-course-form').show(); // Hide the add a course button $('#add-course-btn').hide(); + // Put focus in first input field + $('#str_school').focus(); }); // Set up the "Add Course" button in the @@ -20,6 +22,7 @@ $(function() { $('#add-course-btn').hide(); // Scroll the user's page to here $('#add-course-divider').ScrollTo(); + // Put focus in first input field $('#str_school').focus(); }); @@ -46,7 +49,7 @@ $(function() { $("#str_school").autocomplete({ source: function(request, response){ $.ajax({ - url: json_course_list, + url: json_school_list, data: {q: request.term}, success: function(data) { console.log(data); @@ -83,4 +86,32 @@ $(function() { }, minLength: 3 }); + + $("#id_name").autocomplete({ + source: function(request, response){ + var school_id = $('#id_school').val() + $.ajax({ + url: json_school_course_list, + data: {q: request.term, school_id: school_id}, + success: function(data) { + console.log(data); + if (data['status'] === 'success') { + response($.map(data['courses'], function(item) { + return { + value: item.name, + label: item.name, + }; + })); + } else { + // FIXME: do something? + } + }, + dataType: "json", + type: 'POST' + }); + }, + minLength: 3 + }); + + }); diff --git a/karmaworld/templates/partial/add_course.html b/karmaworld/templates/partial/add_course.html index 99af04d..ea0c366 100644 --- a/karmaworld/templates/partial/add_course.html +++ b/karmaworld/templates/partial/add_course.html @@ -1,6 +1,7 @@ {% load url from future %} diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 6e0af64..f69edd0 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -14,6 +14,7 @@ from karmaworld.apps.courses.views import AboutView from karmaworld.apps.courses.views import CourseDetailView from karmaworld.apps.courses.views import CourseListView from karmaworld.apps.courses.views import school_list +from karmaworld.apps.courses.views import school_course_list from karmaworld.apps.notes.views import NoteView from karmaworld.apps.notes.views import RawNoteDetailView from karmaworld.apps.notes.views import PDFView @@ -88,6 +89,8 @@ urlpatterns = patterns('', url(r'^ajax-upload$', ajax_uploader, name='ajax_upload'), # return json list of schools url(r'^school/list/$', school_list, name='json_school_list'), + # return json list of courses for a given school + url(r'^school/course/list/$', school_course_list, name='json_school_course_list'), # ---- end JSON views ----# url(r'^$', CourseListView.as_view(model=Course), name='home'), -- 2.25.1