2017-08-24 12:26:55 +02:00
|
|
|
from ..utils.access_permissions import ( # noqa
|
|
|
|
BaseAccessPermissions,
|
|
|
|
RestrictedData,
|
|
|
|
)
|
2016-12-17 09:30:20 +01:00
|
|
|
from ..utils.auth import has_perm
|
2017-05-01 23:12:42 +02:00
|
|
|
from ..utils.collection import Collection
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MediafileAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Mediafile and MediafileViewSet.
|
|
|
|
"""
|
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, 'mediafiles.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 MediafileSerializer
|
|
|
|
|
|
|
|
return MediafileSerializer
|
2016-05-14 14:49:24 +02:00
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
def get_restricted_data(self, container, user):
|
2016-05-14 14:49:24 +02:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
2017-05-01 23:12:42 +02:00
|
|
|
for the user. Removes hidden mediafiles for some users.
|
2016-05-14 14:49:24 +02:00
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
# Expand full_data to a list if it is not one.
|
|
|
|
full_data = container.get_full_data() if isinstance(container, Collection) else [container.get_full_data()]
|
|
|
|
|
|
|
|
# Parse data.
|
2017-04-28 00:50:37 +02:00
|
|
|
if has_perm(user, 'mediafiles.can_see') and has_perm(user, 'mediafiles.can_see_hidden'):
|
|
|
|
data = full_data
|
|
|
|
elif has_perm(user, 'mediafiles.can_see'):
|
2017-05-01 23:12:42 +02:00
|
|
|
# Exclude hidden mediafiles.
|
|
|
|
data = [full for full in full_data if not full['hidden']]
|
|
|
|
else:
|
|
|
|
data = []
|
|
|
|
|
|
|
|
# Reduce result to a single item or None if it was not a collection at
|
|
|
|
# the beginning of the method.
|
|
|
|
if isinstance(container, Collection):
|
2017-08-24 12:26:55 +02:00
|
|
|
restricted_data = data # type: RestrictedData
|
2017-05-01 23:12:42 +02:00
|
|
|
elif data:
|
|
|
|
restricted_data = data[0]
|
2017-04-28 00:50:37 +02:00
|
|
|
else:
|
2017-05-01 23:12:42 +02:00
|
|
|
restricted_data = None
|
|
|
|
|
|
|
|
return restricted_data
|