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
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-02-11 11:29:19 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
class ItemAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Item and ItemViewSet.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
def check_permissions(self, user):
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns True if the user has read access model instances.
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm(user, 'agenda.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 ItemSerializer
|
|
|
|
|
|
|
|
return ItemSerializer
|
2016-03-02 00:46:19 +01:00
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
# TODO: In the following method we use full_data['is_hidden'] but this can be out of date.
|
|
|
|
|
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
|
|
|
|
for the user.
|
|
|
|
"""
|
2017-04-28 00:50:37 +02:00
|
|
|
def filtered_data(full_data, blocked_keys):
|
|
|
|
"""
|
|
|
|
Returns a new dict like full_data but with all blocked_keys removed.
|
|
|
|
"""
|
|
|
|
whitelist = full_data.keys() - blocked_keys
|
|
|
|
return {key: full_data[key] for key in whitelist}
|
|
|
|
|
|
|
|
# many_items is True, when there are more then one items in full_data.
|
|
|
|
many_items = not isinstance(full_data, dict)
|
|
|
|
full_data = full_data if many_items else [full_data]
|
|
|
|
|
2017-03-03 10:54:41 +01:00
|
|
|
if has_perm(user, 'agenda.can_see'):
|
2017-04-28 00:50:37 +02:00
|
|
|
if has_perm(user, 'agenda.can_manage'):
|
|
|
|
data = full_data
|
|
|
|
elif has_perm(user, 'agenda.can_see_hidden_items'):
|
|
|
|
blocked_keys = ('comment',)
|
|
|
|
data = [filtered_data(full, blocked_keys) for full in full_data]
|
|
|
|
else:
|
|
|
|
data = []
|
|
|
|
filtered_blocked_keys = full_data[0].keys() - (
|
2017-03-03 10:54:41 +01:00
|
|
|
'id',
|
|
|
|
'title',
|
|
|
|
'speakers',
|
|
|
|
'speaker_list_closed',
|
2017-04-28 00:50:37 +02:00
|
|
|
'content_object')
|
|
|
|
not_filtered_blocked_keys = ('comment',)
|
|
|
|
for full in full_data:
|
|
|
|
if full['is_hidden']:
|
|
|
|
blocked_keys = filtered_blocked_keys
|
|
|
|
else:
|
|
|
|
blocked_keys = not_filtered_blocked_keys
|
|
|
|
data.append(filtered_data(full, blocked_keys))
|
2016-03-02 00:46:19 +01:00
|
|
|
else:
|
|
|
|
data = None
|
2017-04-28 00:50:37 +02:00
|
|
|
|
|
|
|
return data if many_items else data[0]
|
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 field 'comment'.
|
|
|
|
"""
|
|
|
|
data = {}
|
|
|
|
for key in full_data.keys():
|
|
|
|
if key != 'comment':
|
|
|
|
data[key] = full_data[key]
|
|
|
|
return data
|