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
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
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")
$('#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
$('#add-course-btn').hide();
// Scroll the user's page to here
$('#add-course-divider').ScrollTo();
+ // Put focus in first input field
$('#str_school').focus();
});
$("#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);
},
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
+ });
+
+
});
{% load url from future %}
<script>
- var json_course_list = "{% url 'json_school_list' %}"
+ var json_school_list = "{% url 'json_school_list' %}"
+ var json_school_course_list = "{% url 'json_school_course_list' %}"
var csrf_token = "{{ csrf_token }}";
</script>
<script src="{{ STATIC_URL }}js/add-course.js"></script>
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
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'),