2022-07-12 07:28:42 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2022-06-23 05:52:28 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from perms.models import ApplicationPermission
|
2022-09-07 09:35:23 +00:00
|
|
|
from perms.serializers.permission import ActionsField
|
2022-06-23 05:52:28 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
|
|
|
from applications.models import Application
|
|
|
|
from tickets.models import ApplyApplicationTicket
|
|
|
|
from .ticket import TicketApplySerializer
|
|
|
|
from .common import BaseApplyAssetApplicationSerializer
|
|
|
|
|
2022-07-12 07:28:42 +00:00
|
|
|
__all__ = ['ApplyApplicationSerializer', 'ApplyApplicationDisplaySerializer', 'ApproveApplicationSerializer']
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApplyApplicationSerializer(BaseApplyAssetApplicationSerializer, TicketApplySerializer):
|
2022-07-22 08:24:57 +00:00
|
|
|
apply_actions = ActionsField(required=True, allow_empty=False)
|
2022-06-23 05:52:28 +00:00
|
|
|
permission_model = ApplicationPermission
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ApplyApplicationTicket
|
|
|
|
writeable_fields = [
|
|
|
|
'id', 'title', 'type', 'apply_category',
|
|
|
|
'apply_type', 'apply_applications', 'apply_system_users',
|
2022-07-22 08:24:57 +00:00
|
|
|
'apply_actions', 'apply_date_start', 'apply_date_expired', 'org_id'
|
2022-06-23 05:52:28 +00:00
|
|
|
]
|
2022-07-22 08:24:57 +00:00
|
|
|
fields = TicketApplySerializer.Meta.fields + \
|
|
|
|
writeable_fields + ['apply_permission_name', 'apply_actions_display']
|
2022-06-23 05:52:28 +00:00
|
|
|
read_only_fields = list(set(fields) - set(writeable_fields))
|
|
|
|
ticket_extra_kwargs = TicketApplySerializer.Meta.extra_kwargs
|
2022-06-28 09:19:33 +00:00
|
|
|
extra_kwargs = {
|
2022-07-12 07:28:42 +00:00
|
|
|
'apply_applications': {'required': False, 'allow_empty': True},
|
|
|
|
'apply_system_users': {'required': False, 'allow_empty': True},
|
2022-06-28 09:19:33 +00:00
|
|
|
}
|
2022-06-23 05:52:28 +00:00
|
|
|
extra_kwargs.update(ticket_extra_kwargs)
|
|
|
|
|
2022-06-27 02:15:29 +00:00
|
|
|
def validate_apply_applications(self, applications):
|
2022-07-12 07:28:42 +00:00
|
|
|
if self.is_final_approval and not applications:
|
|
|
|
raise serializers.ValidationError(_('This field is required.'))
|
2022-06-27 02:15:29 +00:00
|
|
|
tp = self.initial_data.get('apply_type')
|
|
|
|
return self.filter_many_to_many_field(Application, applications, type=tp)
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
|
2022-07-12 07:28:42 +00:00
|
|
|
class ApproveApplicationSerializer(ApplyApplicationSerializer):
|
|
|
|
class Meta(ApplyApplicationSerializer.Meta):
|
|
|
|
read_only_fields = ApplyApplicationSerializer.Meta.read_only_fields + ['title', 'type']
|
|
|
|
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
class ApplyApplicationDisplaySerializer(ApplyApplicationSerializer):
|
|
|
|
apply_applications = serializers.SerializerMethodField()
|
|
|
|
apply_system_users = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ApplyApplicationSerializer.Meta.model
|
|
|
|
fields = ApplyApplicationSerializer.Meta.fields
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_apply_applications(instance):
|
|
|
|
with tmp_to_org(instance.org_id):
|
|
|
|
return instance.apply_applications.values_list('id', flat=True)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_apply_system_users(instance):
|
|
|
|
with tmp_to_org(instance.org_id):
|
|
|
|
return instance.apply_system_users.values_list('id', flat=True)
|