2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-05 08:07:14 +00:00
|
|
|
from rest_framework import serializers
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import LabeledChoiceField
|
2022-06-23 05:52:28 +00:00
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
2022-12-05 08:07:14 +00:00
|
|
|
from orgs.models import Organization
|
2022-11-18 03:30:31 +00:00
|
|
|
from tickets.const import TicketType, TicketStatus, TicketState
|
2022-12-05 08:07:14 +00:00
|
|
|
from tickets.models import Ticket, TicketFlow
|
|
|
|
from users.models import User
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2022-11-15 02:43:21 +00:00
|
|
|
'TicketApplySerializer', 'TicketApproveSerializer', 'TicketSerializer',
|
2020-12-29 16:19:59 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class TicketSerializer(OrgResourceModelSerializerMixin):
|
2022-11-15 02:43:21 +00:00
|
|
|
type = LabeledChoiceField(choices=TicketType.choices, read_only=True, label=_('Type'))
|
|
|
|
status = LabeledChoiceField(choices=TicketStatus.choices, read_only=True, label=_('Status'))
|
|
|
|
state = LabeledChoiceField(choices=TicketState.choices, read_only=True, label=_("State"))
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'title']
|
2022-11-17 05:52:10 +00:00
|
|
|
fields_small = fields_mini + ['org_id', 'comment']
|
|
|
|
read_only_fields = [
|
|
|
|
'serial_num', 'process_map', 'approval_step', 'type', 'state', 'applicant',
|
|
|
|
'status', 'date_created', 'date_updated', 'org_name', 'rel_snapshot'
|
2020-12-29 16:19:59 +00:00
|
|
|
]
|
2022-11-17 05:52:10 +00:00
|
|
|
fields = fields_small + read_only_fields
|
2023-01-16 11:02:09 +00:00
|
|
|
extra_kwargs = {}
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2022-07-01 11:07:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_type_choices()
|
|
|
|
|
|
|
|
def set_type_choices(self):
|
|
|
|
tp = self.fields.get('type')
|
|
|
|
if not tp:
|
|
|
|
return
|
2023-02-02 06:37:21 +00:00
|
|
|
choices = tp.choices
|
2022-07-01 11:07:55 +00:00
|
|
|
choices.pop(TicketType.general, None)
|
2023-02-02 06:37:21 +00:00
|
|
|
tp.choices = choices
|
2022-07-01 11:07:55 +00:00
|
|
|
|
2022-11-15 02:43:21 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
queryset = queryset.prefetch_related('ticket_steps')
|
|
|
|
return queryset
|
2021-01-12 10:06:42 +00:00
|
|
|
|
|
|
|
|
2022-07-13 07:48:55 +00:00
|
|
|
class TicketApproveSerializer(TicketSerializer):
|
2022-11-15 02:43:21 +00:00
|
|
|
class Meta(TicketSerializer.Meta):
|
2022-07-13 07:48:55 +00:00
|
|
|
fields = TicketSerializer.Meta.fields
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
class TicketApplySerializer(TicketSerializer):
|
2020-12-29 16:19:59 +00:00
|
|
|
org_id = serializers.CharField(
|
2021-01-13 09:49:03 +00:00
|
|
|
required=True, max_length=36, allow_blank=True, label=_("Organization")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
2023-05-31 02:20:37 +00:00
|
|
|
applicant = serializers.CharField(required=False, allow_blank=True)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2022-11-11 08:54:38 +00:00
|
|
|
def get_applicant(self, applicant_id):
|
|
|
|
current_user = self.context['request'].user
|
|
|
|
want_applicant = User.objects.filter(id=applicant_id).first()
|
|
|
|
if want_applicant and current_user.has_perm('tickets.add_superticket'):
|
|
|
|
applicant = want_applicant
|
|
|
|
else:
|
|
|
|
applicant = current_user
|
|
|
|
return applicant
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@staticmethod
|
|
|
|
def validate_org_id(org_id):
|
2021-03-03 03:20:40 +00:00
|
|
|
org = Organization.get_instance(org_id)
|
2020-12-29 16:19:59 +00:00
|
|
|
if not org:
|
|
|
|
error = _('The organization `{}` does not exist'.format(org_id))
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
return org_id
|
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
def validate(self, attrs):
|
2022-07-12 07:28:42 +00:00
|
|
|
if self.instance:
|
|
|
|
return attrs
|
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
ticket_type = attrs.get('type')
|
2021-12-11 04:50:30 +00:00
|
|
|
org_id = attrs.get('org_id')
|
2023-01-16 11:02:09 +00:00
|
|
|
flow = TicketFlow.get_org_related_flows(org_id=org_id).filter(type=ticket_type).first()
|
|
|
|
if not flow:
|
2021-08-25 11:02:50 +00:00
|
|
|
error = _('The ticket flow `{}` does not exist'.format(ticket_type))
|
2020-12-29 16:19:59 +00:00
|
|
|
raise serializers.ValidationError(error)
|
2023-01-16 11:02:09 +00:00
|
|
|
attrs['flow'] = flow
|
2022-11-11 08:54:38 +00:00
|
|
|
attrs['applicant'] = self.get_applicant(attrs.get('applicant'))
|
2021-08-25 11:02:50 +00:00
|
|
|
return attrs
|