2016-02-11 22:58:32 +01:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Assignment and AssignmentViewSet.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
def check_permissions(self, user):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns True if the user has read access model instances.
|
|
|
|
"""
|
|
|
|
return user.has_perm('assignments.can_see')
|
2016-02-11 11:29:19 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
def get_serializer_class(self, user=None):
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
|
|
|
Returns different serializer classes according to users permissions.
|
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
from .serializers import AssignmentFullSerializer, AssignmentShortSerializer
|
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
if user is None or (user.has_perm('assignments.can_see') and user.has_perm('assignments.can_manage')):
|
2016-02-11 11:29:19 +01:00
|
|
|
serializer_class = AssignmentFullSerializer
|
|
|
|
else:
|
|
|
|
serializer_class = AssignmentShortSerializer
|
|
|
|
return serializer_class
|
2016-03-02 00:46:19 +01:00
|
|
|
|
|
|
|
def get_restricted_data(self, full_data, user):
|
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
2016-07-29 23:33:47 +02:00
|
|
|
for the user. Removes unpublished polls for non admins so that they
|
2016-03-02 00:46:19 +01:00
|
|
|
only get a result like the AssignmentShortSerializer would give them.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
if user.has_perm('assignments.can_see') and user.has_perm('assignments.can_manage'):
|
2016-03-02 00:46:19 +01:00
|
|
|
data = full_data
|
2016-09-17 22:26:23 +02:00
|
|
|
elif user.has_perm('assignments.can_see'):
|
2016-03-02 00:46:19 +01:00
|
|
|
data = full_data.copy()
|
|
|
|
data['polls'] = [poll for poll in data['polls'] if poll['published']]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_projector_data(self, full_data):
|
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
|
|
|
for the projector. Removes several fields.
|
|
|
|
"""
|
|
|
|
data = full_data.copy()
|
|
|
|
data['polls'] = [poll for poll in data['polls'] if poll['published']]
|
2016-03-02 00:46:19 +01:00
|
|
|
return data
|