2016-11-02 00:09:59 +01:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2016-12-17 09:30:20 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2016-07-29 23:33:47 +02:00
|
|
|
from ..core.config import config
|
2016-02-11 22:58:32 +01:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions
|
2016-12-17 09:30:20 +01:00
|
|
|
from ..utils.auth import has_perm
|
|
|
|
from ..utils.collection import CollectionElement
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MotionAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Motion and MotionViewSet.
|
|
|
|
"""
|
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, 'motions.can_see')
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
def get_serializer_class(self, user=None):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import MotionSerializer
|
|
|
|
|
|
|
|
return MotionSerializer
|
|
|
|
|
2016-07-29 23:33:47 +02:00
|
|
|
def get_restricted_data(self, full_data, user):
|
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared for
|
2016-12-09 18:00:45 +01:00
|
|
|
the user. Removes motion if the user has not the permission to see
|
|
|
|
the motion in this state. Removes non public comment fields for
|
|
|
|
some unauthorized users.
|
2016-07-29 23:33:47 +02:00
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
if isinstance(user, get_user_model()):
|
|
|
|
# Converts a user object to a collection element.
|
|
|
|
# from_instance can not be used because the user serializer loads
|
|
|
|
# the group from the db. So each call to from_instance(user) consts
|
|
|
|
# one db query.
|
|
|
|
user = CollectionElement.from_values('users/user', user.id)
|
|
|
|
|
|
|
|
if isinstance(user, CollectionElement):
|
|
|
|
is_submitter = user.get_full_data()['id'] in full_data.get('submitters_id', [])
|
|
|
|
else:
|
|
|
|
# Anonymous users can not be submitters
|
|
|
|
is_submitter = False
|
|
|
|
|
|
|
|
required_permission_to_see = full_data['state_required_permission_to_see']
|
2016-12-09 18:00:45 +01:00
|
|
|
if (not required_permission_to_see or
|
2016-12-17 09:30:20 +01:00
|
|
|
has_perm(user, required_permission_to_see) or
|
|
|
|
has_perm(user, 'motions.can_manage') or
|
|
|
|
is_submitter):
|
|
|
|
if has_perm(user, 'motions.can_see_and_manage_comments') or not full_data.get('comments'):
|
2016-12-09 18:00:45 +01:00
|
|
|
data = full_data
|
|
|
|
else:
|
|
|
|
data = deepcopy(full_data)
|
|
|
|
for i, field in enumerate(config['motions_comments']):
|
|
|
|
if not field.get('public'):
|
|
|
|
try:
|
|
|
|
data['comments'][i] = None
|
|
|
|
except IndexError:
|
|
|
|
# No data in range. Just do nothing.
|
|
|
|
pass
|
2016-07-29 23:33:47 +02:00
|
|
|
else:
|
2016-12-09 18:00:45 +01:00
|
|
|
data = None
|
2016-07-29 23:33:47 +02:00
|
|
|
return data
|
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
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()
|
2016-11-14 13:16:34 +01:00
|
|
|
if data.get('comments') is not None:
|
|
|
|
for i, field in enumerate(config['motions_comments']):
|
|
|
|
if not field.get('public'):
|
|
|
|
try:
|
|
|
|
data['comments'][i] = None
|
|
|
|
except IndexError:
|
|
|
|
# No data in range. Just do nothing.
|
|
|
|
pass
|
2016-09-17 22:26:23 +02:00
|
|
|
return data
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
class MotionChangeRecommendationAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for MotionChangeRecommendation and MotionChangeRecommendationViewSet.
|
|
|
|
"""
|
|
|
|
def check_permissions(self, user):
|
|
|
|
"""
|
|
|
|
Returns True if the user has read access model instances.
|
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm(user, 'motions.can_see')
|
2016-09-10 18:49:38 +02:00
|
|
|
|
|
|
|
def get_serializer_class(self, user=None):
|
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import MotionChangeRecommendationSerializer
|
|
|
|
|
|
|
|
return MotionChangeRecommendationSerializer
|
|
|
|
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
class CategoryAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Category and CategoryViewSet.
|
|
|
|
"""
|
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, 'motions.can_see')
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
def get_serializer_class(self, user=None):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import CategorySerializer
|
|
|
|
|
|
|
|
return CategorySerializer
|
|
|
|
|
|
|
|
|
2016-10-01 20:42:44 +02:00
|
|
|
class MotionBlockAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Category and CategoryViewSet.
|
|
|
|
"""
|
|
|
|
def check_permissions(self, user):
|
|
|
|
"""
|
|
|
|
Returns True if the user has read access model instances.
|
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm(user, 'motions.can_see')
|
2016-10-01 20:42:44 +02:00
|
|
|
|
|
|
|
def get_serializer_class(self, user=None):
|
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import MotionBlockSerializer
|
|
|
|
|
|
|
|
return MotionBlockSerializer
|
|
|
|
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
class WorkflowAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Workflow and WorkflowViewSet.
|
|
|
|
"""
|
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, 'motions.can_see')
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
def get_serializer_class(self, user=None):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import WorkflowSerializer
|
|
|
|
|
|
|
|
return WorkflowSerializer
|