instructor_name = models.CharField(max_length=255, blank=True, null=True)
instructor_email = models.EmailField(blank=True, null=True)
- # Save school name redundantly to speed filtering
- school_name = models.CharField(max_length=255, blank=True, null=True)
-
updated_at = models.DateTimeField(default=datetime.datetime.utcnow)
created_at = models.DateTimeField(auto_now_add=True)
print "\n\n"
-class CourseAjaxList(BaseListView):
-
- def get_queryset(self):
- """ generate a contexual query based on arguments """
-
- req = self.request # HTTPrequest is stored in the class
-
- # Handle empty argument sSearch
- if req.GET['sSearch'] == '':
- return []
-
- search_query = req.GET['sSearch']
-
- search_strings = search_query.strip().split(' ')
- queries = [
- (
- Q(name__icontains=s) |
- Q(instructor_name__icontains=s) |
- Q(school_name__icontains=s)
- )
- for s in search_strings
- ]
-
- full_query = queries.pop()
- for q in queries:
- full_query &= q
-
- object_list = Course.objects.filter(full_query).values_list(
- 'school__name',
- 'name',
- 'instructor_name',
- 'file_count',
- 'updated_at')
-
- # this works too
- # return map(lambda row: [row[:3], row[4].strftime("%I%p // %a %b %d %Y")], object_list)
- return [[a, b, c, d, e.strftime("%I%p // %a %b %d %Y")] for a, b, c, d, e in object_list]
-
- def render_to_response(self, context, **response_kwargs):
- """ Returns a JSON response, transforming 'context' to make the payload """
- response_dict = {
- 'aaData': self.object_list,
- 'iTotalRecords': len(self.object_list),
- 'iTotalDisplayRecords': len(self.object_list),
- 'sEcho': int(self.request.GET['sEcho'])
- }
- return HttpResponse(
- # replace convert_context_to_json with our whatever function
- json.dumps(response_dict),
- content_type = 'application/json'
- )
-
-
def school_list(request):
""" Return JSON describing Schools that match q query on name """
if request.method == 'POST' and request.is_ajax() \
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
# 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 ----#