Exemplo n.º 1
0
 def get(self, request, *args, **kwargs):
     '''
     Return the permission level of the user.
     '''
     if not request.user.is_authenticated():
         return jr({'level': 0})
     else:
         return jr({'level': getattr(request.user, 'level', 0)})
Exemplo n.º 2
0
 def _get_assignments_and_students_enrolled_and_not_enrolled(self, request, *args, **kwargs):
     """
     Retrieves the given assignment and returns a separate list of all of
     the students with their name and their id. If they are assigned to
     the assignment, they will be in the assigned_to attribute, and the
     to_assign will the others go.
     """
     try:
         assigned = Assignment.objects.prefetch_related().get(id=kwargs['id'])
     except ObjectDoesNotExist:
         return err("Assignment with id {} does not exist."
                         .format(kwargs['id'])
                    )
     students = User.objects.exclude(Q(id__in=assigned.users.all()) | Q(level__gt=0))
     
     respDict = {
                 "assignment" : {
                                 "description" : assigned.description,
                                 "due_date" : assigned.due_date
                                 },
                 "assigned_to" : objs_to_dict(User, 
                                              assigned.users.all(),
                                              ("id", "lname", "fname")
                                              ),
                 "to_assign" : objs_to_dict(User, 
                                              students,
                                              ("id", "lname", "fname")
                                              )
                 }
     return jr(respDict)
Exemplo n.º 3
0
 def options(self, request, *args, **kwargs):
     """
     Return the permission levels available.
     """
     data = {
         "levels": get_user_levels(),
         "level_by_name": get_user_level_names()
     }
     return jr(data)
Exemplo n.º 4
0
def err(msg, status=400):
    '''
    Send an error response.
    
    @param msg: the reason for the error
    @param status: the status code of the error
    @return the HttpResponse object
    '''
    resp = jr(d({"err": "{}".format(msg)}), status=status)
    resp.reason_phrase = msg
    return resp
Exemplo n.º 5
0
 def get(self, request, *args, **kwargs):
     """
     Returns the json object of discovery endpoints. Looks as follows:
     
         {
             "urls" : {
                 "<url_path>" : "<regex of path>"
             }
         }
     """
     return jr({"urls": self.discovery})
Exemplo n.º 6
0
 def post(self, request, *args, **kwargs):
     '''
     Creates a new poster. The poster object should be of the following json 
     type:
     
         {
             "email" : "<email>",
             "password" : "<password>"
         }
     '''
     j = read(request)
     try:
         p = USER().objects.create_user(**j)
         resp = convert_to_dicts([p], p.field_names, p)[0]
         return jr(resp)
     except IntegrityError as ie:
         return err(ie)