Commits

Nattaphoom Ch. committed 2a665cd

Edited UserHandler more smart with filters for lecturer, staff, or
student.

  • Participants
  • Parent commits 750b0f0

Comments (0)

Files changed (1)

File api/handlers.py

 # -*- encoding: utf-8 -*-
 from ans_gcm.gcm_sender import send_json_gcm_message
-from ans_gcm.models import ANSUser, Agenda, ApiKey, Device, Message,\
-        Group, Membership
+from ans_gcm.models import ANSUser, Agenda, ApiKey, Device, Message
+from ans_gcm.models import Group, Membership
 
 from django.http import HttpResponse
 from django.shortcuts import get_object_or_404
 class UserHandler(BaseHandler):
     """
     Entry point for API key authentication.
-    access via /api/user/
+    Access via /api/user/
     """
     model = ANSUser
     anonymous = 'AnonymousUserHandler'
         """
         Returns all ANS users except user `asterisk`.
         """
+        query = ANSUser.objects.filter(~Q(username='asterisk'),
+                ~Q(username=request.user)).order_by('first_name', 'last_name')
+        if 'filter_lecturer' in request.GET:
+            return query.filter(user_type=0)
+            return ANSUser.objects.filter(~Q(username='asterisk'),
+                    ~Q(username=request.user), user_type=0).order_by(
+                            'first_name', 'last_name')
+        if 'filter_staff' in request.GET:
+            return query.filter(user_type=1)
+        if 'filter_student' in request.GET:
+            return query.filter(user_type=2)
+        return query
         return ANSUser.objects.filter(~Q(username='asterisk'),
                 ~Q(username=request.user)).order_by('first_name', 'last_name')
 
             message.save()
             # check whether message have agenda or not?
             if request.POST['agenda'] == 'true':
-                if 'date_value' in request.POST and 'time_value' in request.POST:
+                if 'date_value' in request.POST and\
+                        'time_value' in request.POST:
                     date = request.POST['date_value'] + ' ' + request.POST[
                             'time_value']
                 else:
                     # then check for username
                     # student case
                     try:
-                        sender_user = ANSUser.objects.get(username=asterisk_user)
+                        sender_user = ANSUser.objects.get(
+                                username=asterisk_user)
                     except ANSUser.DoesNotExist:
                         # worst case
-                        # no asterisk_username and username found in ANS database
+                        # asterisk_username and username was not
+                        # found in ANS database
                         # change sender user to asterisk server
                         sender_user = ANSUser.objects.get(username='asterisk')