2020-10-21 11:32:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
2020-10-28 06:45:30 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-10-21 11:32:27 +00:00
|
|
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
|
|
from perms.models import ApplicationPermission
|
2022-01-13 10:32:12 +00:00
|
|
|
from ..base import ActionsField, BasePermissionSerializer
|
2020-10-21 11:32:27 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'ApplicationPermissionSerializer'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-01-13 10:32:12 +00:00
|
|
|
class ApplicationPermissionSerializer(BasePermissionSerializer):
|
2021-12-22 09:35:32 +00:00
|
|
|
actions = ActionsField(required=False, allow_null=True, label=_("Actions"))
|
2021-07-30 11:13:47 +00:00
|
|
|
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category display'))
|
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display'))
|
|
|
|
is_valid = serializers.BooleanField(read_only=True, label=_('Is valid'))
|
|
|
|
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
|
2020-10-21 11:32:27 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ApplicationPermission
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
2021-07-30 11:13:47 +00:00
|
|
|
'category', 'category_display', 'type', 'type_display',
|
2021-12-22 09:35:32 +00:00
|
|
|
'actions',
|
2021-07-30 11:13:47 +00:00
|
|
|
'is_active', 'is_expired', 'is_valid',
|
2021-09-09 06:37:43 +00:00
|
|
|
'created_by', 'date_created', 'date_expired', 'date_start', 'comment', 'from_ticket'
|
2020-10-21 11:32:27 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_m2m = [
|
2020-10-21 11:32:27 +00:00
|
|
|
'users', 'user_groups', 'applications', 'system_users',
|
2021-07-30 11:13:47 +00:00
|
|
|
'users_amount', 'user_groups_amount', 'applications_amount',
|
|
|
|
'system_users_amount',
|
2020-10-21 11:32:27 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields = fields_small + fields_m2m
|
2021-09-09 06:37:43 +00:00
|
|
|
read_only_fields = ['created_by', 'date_created', 'from_ticket']
|
2021-07-30 11:13:47 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'is_expired': {'label': _('Is expired')},
|
|
|
|
'is_valid': {'label': _('Is valid')},
|
|
|
|
'actions': {'label': _('Actions')},
|
|
|
|
'users_amount': {'label': _('Users amount')},
|
|
|
|
'user_groups_amount': {'label': _('User groups amount')},
|
|
|
|
'system_users_amount': {'label': _('System users amount')},
|
|
|
|
'applications_amount': {'label': _('Applications amount')},
|
|
|
|
}
|
2020-10-21 11:32:27 +00:00
|
|
|
|
2022-01-13 10:32:12 +00:00
|
|
|
def _filter_actions_choices(self, choices):
|
2021-12-22 09:35:32 +00:00
|
|
|
if request := self.context.get('request'):
|
|
|
|
category = request.query_params.get('category')
|
|
|
|
else:
|
|
|
|
category = None
|
|
|
|
exclude_choices = ApplicationPermission.get_exclude_actions_choices(category=category)
|
|
|
|
for choice in exclude_choices:
|
|
|
|
choices.pop(choice, None)
|
2022-01-13 10:32:12 +00:00
|
|
|
return choices
|
2021-12-22 09:35:32 +00:00
|
|
|
|
2020-10-21 11:32:27 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2021-07-30 11:13:47 +00:00
|
|
|
queryset = queryset.prefetch_related(
|
|
|
|
'users', 'user_groups', 'applications', 'system_users'
|
|
|
|
)
|
2020-10-21 11:32:27 +00:00
|
|
|
return queryset
|
|
|
|
|
2020-10-30 09:22:26 +00:00
|
|
|
def validate_applications(self, applications):
|
|
|
|
if self.instance:
|
|
|
|
permission_type = self.instance.type
|
|
|
|
else:
|
|
|
|
permission_type = self.initial_data['type']
|
|
|
|
|
|
|
|
other_type_applications = [
|
|
|
|
application for application in applications
|
|
|
|
if application.type != permission_type
|
|
|
|
]
|
|
|
|
if len(other_type_applications) > 0:
|
|
|
|
error = _(
|
|
|
|
'The application list contains applications '
|
|
|
|
'that are different from the permission type. ({})'
|
|
|
|
).format(', '.join([application.name for application in other_type_applications]))
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
return applications
|