2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2021-03-03 03:20:40 +00:00
|
|
|
from orgs.models import Organization
|
2022-06-23 05:52:28 +00:00
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
|
|
|
from tickets.models import Ticket, TicketFlow
|
2022-07-01 11:07:55 +00:00
|
|
|
from tickets.const import TicketType
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2022-07-13 07:48:55 +00:00
|
|
|
'TicketDisplaySerializer', 'TicketApplySerializer', 'TicketListSerializer', 'TicketApproveSerializer'
|
2020-12-29 16:19:59 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class TicketSerializer(OrgResourceModelSerializerMixin):
|
2021-01-18 06:56:02 +00:00
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display'))
|
2021-08-25 11:02:50 +00:00
|
|
|
status_display = serializers.ReadOnlyField(source='get_status_display', label=_('Status display'))
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'title']
|
|
|
|
fields_small = fields_mini + [
|
2022-06-23 05:52:28 +00:00
|
|
|
'type', 'type_display', 'status', 'status_display',
|
|
|
|
'state', 'approval_step', 'rel_snapshot', 'comment',
|
|
|
|
'date_created', 'date_updated', 'org_id', 'rel_snapshot',
|
|
|
|
'process_map', 'org_name', 'serial_num'
|
2020-12-29 16:19:59 +00:00
|
|
|
]
|
2021-08-25 11:02:50 +00:00
|
|
|
fields_fk = ['applicant', ]
|
|
|
|
fields = fields_small + fields_fk
|
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
|
|
|
|
choices = tp._choices
|
|
|
|
choices.pop(TicketType.general, None)
|
|
|
|
tp._choices = choices
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
class TicketListSerializer(TicketSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
fields = [
|
|
|
|
'id', 'title', 'serial_num', 'type', 'type_display', 'status',
|
|
|
|
'state', 'rel_snapshot', 'date_created', 'rel_snapshot'
|
|
|
|
]
|
|
|
|
read_only_fields = fields
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
class TicketDisplaySerializer(TicketSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
fields = TicketSerializer.Meta.fields
|
2021-01-13 09:49:03 +00:00
|
|
|
read_only_fields = fields
|
2021-01-12 10:06:42 +00:00
|
|
|
|
|
|
|
|
2022-07-13 07:48:55 +00:00
|
|
|
class TicketApproveSerializer(TicketSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
fields = TicketSerializer.Meta.fields
|
2020-12-29 16:19:59 +00:00
|
|
|
extra_kwargs = {
|
2022-06-23 05:52:28 +00:00
|
|
|
'type': {'required': True}
|
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')
|
|
|
|
flow = TicketFlow.get_org_related_flows(org_id=org_id).filter(type=ticket_type).first()
|
2021-08-25 11:02:50 +00:00
|
|
|
if flow:
|
|
|
|
attrs['flow'] = flow
|
|
|
|
else:
|
|
|
|
error = _('The ticket flow `{}` does not exist'.format(ticket_type))
|
2020-12-29 16:19:59 +00:00
|
|
|
raise serializers.ValidationError(error)
|
2021-08-25 11:02:50 +00:00
|
|
|
return attrs
|