2017-09-04 00:25:45 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
from ..utils.access_permissions import BaseAccessPermissions # noqa
|
2016-12-17 09:30:20 +01:00
|
|
|
from ..utils.auth import has_perm
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.collection import CollectionElement
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm(user, '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-12-17 09:30:20 +01:00
|
|
|
if user is None or (has_perm(user, 'assignments.can_see') and has_perm(user, '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
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
def get_restricted_data(
|
|
|
|
self,
|
|
|
|
full_data: List[Dict[str, Any]],
|
|
|
|
user: Optional[CollectionElement]) -> List[Dict[str, Any]]:
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
# Parse data.
|
2016-12-17 09:30:20 +01:00
|
|
|
if has_perm(user, 'assignments.can_see') and has_perm(user, 'assignments.can_manage'):
|
2016-03-02 00:46:19 +01:00
|
|
|
data = full_data
|
2016-12-17 09:30:20 +01:00
|
|
|
elif has_perm(user, 'assignments.can_see'):
|
2017-05-01 23:12:42 +02:00
|
|
|
# Exclude unpublished polls.
|
|
|
|
data = []
|
|
|
|
for full in full_data:
|
|
|
|
full_copy = full.copy()
|
|
|
|
full_copy['polls'] = [poll for poll in full['polls'] if poll['published']]
|
|
|
|
data.append(full_copy)
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-05-01 23:12:42 +02:00
|
|
|
data = []
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
def get_projector_data(self, full_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
2016-09-17 22:26:23 +02:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
2017-06-13 21:44:45 +02:00
|
|
|
for the projector. Removes unpublished polls.
|
2016-09-17 22:26:23 +02:00
|
|
|
"""
|
2017-06-13 21:44:45 +02:00
|
|
|
# Parse data. Exclude unpublished polls.
|
|
|
|
data = []
|
|
|
|
for full in full_data:
|
|
|
|
full_copy = full.copy()
|
|
|
|
full_copy['polls'] = [poll for poll in full['polls'] if poll['published']]
|
|
|
|
data.append(full_copy)
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|