initial broken serverside datatables config
authorSeth Woodworth <seth@sethish.com>
Wed, 27 Feb 2013 19:38:11 +0000 (14:38 -0500)
committerSeth Woodworth <seth@sethish.com>
Wed, 27 Feb 2013 19:38:11 +0000 (14:38 -0500)
karmaworld/apps/courses/views.py
karmaworld/templates/courses/course_list.html
karmaworld/urls.py

index 7e2b31647f614f8bbbe3681233fdcfdb2f833bd3..18ddbc9f3111042bab056d3305cc956309b5173a 100644 (file)
@@ -13,11 +13,29 @@ from django.views.generic.base import View
 from django.views.generic.detail import SingleObjectMixin
 from django.views.generic.edit import ProcessFormView
 from django.views.generic.edit import ModelFormMixin
+from django.views.generic.list import BaseListView
 
 from karmaworld.apps.courses.forms import CourseForm
 from karmaworld.apps.courses.models import Course
 from karmaworld.apps.courses.models import School
 
+
+class JSONCourseMixin(object):
+    """ JSON mixin for returning course lists """
+    response_class = HttpResponse
+
+    def render_to_response(self, context, **response_kwargs):
+        """
+        Returns a JSON response, transforming 'context' to make the payload.
+        """
+        response_kwargs['content_type'] = 'application/json'
+        return self.response_class(
+            # replace convert_context_to_json with our whatever function
+            self.convert_context_to_json(context),
+            **response_kwargs
+        )
+
+
 class CourseDetailView(DetailView):
     """ Class-based view for the course html page """
     model = Course
@@ -59,6 +77,20 @@ class CourseSaveView(ModelFormMixin, ProcessFormView):
         print "\n\n"
 
 
+class CourseAjaxList(BaseListView, JSONCourseMixin):
+
+
+    def get_queryset(self):
+        """ generate a contexual query based on arguments """
+        req = self.request # HTTPrequest is stored in the class
+        print "\nxxxXXXxxx datatables ajax arguments xxxXXXxxx\n"
+        print req
+        print '\n'
+        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
+        print "printing the request.GET dict"
+        print req.GET
+
+
 def school_list(request):
     """ Return JSON describing Schools that match q query on name """
     if request.method == 'POST' and request.is_ajax() \
index f825849452fd92698893066fdb9da1715925d548..14b9193d71aa1d4ff81e3e209f265d81f41d105f 100644 (file)
 <script src="{{ STATIC_URL }}js/jquery.dataTables.js"></script>
 <script>
 $(document).ready(function() {
-  $('#course_list').dataTable();
-} );
+  $('#course_list').dataTable({
+    'bServerSide': true,
+    'sAjaxSource': "{% url 'api_course_list' %}"
+  });
+});
 </script>
 {% endblock %}
 
index caddca9a313ae20e0acf96b2dbfe5cefb12d7311..0c8896905b897dcb922b6adfaba2057be97bbfb9 100644 (file)
@@ -12,6 +12,7 @@ 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 CourseSaveView
+from karmaworld.apps.courses.views import CourseAjaxList
 from karmaworld.apps.courses.views import school_list
 from karmaworld.apps.notes.views import NoteView
 from karmaworld.apps.notes.views import raw_file
@@ -21,17 +22,21 @@ admin.autodiscover()
 
 # See: https://docs.djangoproject.com/en/dev/topics/http/urls/
 urlpatterns = patterns('',
+    ## Administrative URLpatterns
     # Admin panel and documentation:
     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
     url(r'^admin/', include(admin.site.urls)),
     # Grappelli django-admin improvment suite
     url(r'^grappelli/', include('grappelli.urls')),
 
+    ## Single-serving page URLpatterns
     url(r'^terms/$', direct_to_template, { 'template': 'terms.html' }, name='terms'),
-
     url(r'^about/$', AboutView.as_view(), name='about'),
 
+    # VIEW for viewing a Note's gdrive generated html, used as iframe
     url(r'^raw/(?P<pk>\d+)$', raw_file, name='note_raw'),
+
+    # VIEW for displaying a single Course
     url(r'^(?P<school_slug>[^/]+)/(?P<slug>[-A-Za-z0-9_]+)$', \
         CourseDetailView.as_view(), name='course_detail'),
 
@@ -48,6 +53,8 @@ urlpatterns = patterns('',
     # return json list of schools
     url(r'^school/list/$', school_list, name='json_school_list'),
     url(r'^course/post/$', CourseSaveView.as_view(), name='api_course_post'),
+    url(r'^course/list/$', CourseAjaxList.as_view(), name='api_course_list'),
+
     # ---- end JSON views ----#
 
     url(r'^$', ListView.as_view(model=Course), name='home'),