From c9f742227e58f992fccc1a4e2ee34060b1d2375c Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Tue, 16 Jan 2024 22:19:32 +0000 Subject: [PATCH] Include custom fields in user tickets api endpoint --- helpdesk/serializers.py | 130 ++++++++++++++++++++-------------------- helpdesk/views/api.py | 6 +- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/helpdesk/serializers.py b/helpdesk/serializers.py index 531db1b7..c8c4dbb7 100644 --- a/helpdesk/serializers.py +++ b/helpdesk/serializers.py @@ -72,47 +72,6 @@ class DatatablesTicketSerializer(serializers.ModelSerializer): return obj.kbitem.title if obj.kbitem else "" -class PublicTicketListingSerializer(serializers.ModelSerializer): - """ - A serializer to be used by the public API for listing tickets. Don't expose private fields here! - """ - ticket = serializers.SerializerMethodField() - submitter = serializers.SerializerMethodField() - created = serializers.SerializerMethodField() - due_date = serializers.SerializerMethodField() - status = serializers.SerializerMethodField() - queue = serializers.SerializerMethodField() - kbitem = serializers.SerializerMethodField() - secret_key = serializers.CharField(read_only=True) - - class Meta: - model = Ticket - # fields = '__all__' - fields = ('ticket', 'id', 'title', 'queue', 'status', - 'created', 'due_date', 'submitter', 'kbitem', 'secret_key') - - def get_queue(self, obj): - return {"title": obj.queue.title, "id": obj.queue.id} - - def get_ticket(self, obj): - return str(obj.id) + " " + obj.ticket - - def get_status(self, obj): - return obj.get_status - - def get_created(self, obj): - return humanize.naturaltime(obj.created) - - def get_due_date(self, obj): - return humanize.naturaltime(obj.due_date) - - def get_submitter(self, obj): - return obj.submitter_email - - def get_kbitem(self, obj): - return obj.kbitem.title if obj.kbitem else "" - - class FollowUpAttachmentSerializer(serializers.ModelSerializer): class Meta: model = FollowUpAttachment @@ -153,7 +112,72 @@ class FollowUpSerializer(serializers.ModelSerializer): ) -class TicketSerializer(serializers.ModelSerializer): +class UserSerializer(serializers.ModelSerializer): + password = serializers.CharField(write_only=True) + + class Meta: + model = get_user_model() + fields = ('first_name', 'last_name', 'username', 'email', 'password') + + def create(self, validated_data): + user = super(UserSerializer, self).create(validated_data) + user.is_active = True + user.set_password(validated_data['password']) + user.save() + return user + + +class BaseTicketSerializer(serializers.ModelSerializer): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Add custom fields + for field in CustomField.objects.all(): + self.fields['custom_%s' % field.name] = field.build_api_field() + + +class PublicTicketListingSerializer(BaseTicketSerializer): + """ + A serializer to be used by the public API for listing tickets. Don't expose private fields here! + """ + ticket = serializers.SerializerMethodField() + submitter = serializers.SerializerMethodField() + created = serializers.SerializerMethodField() + due_date = serializers.SerializerMethodField() + status = serializers.SerializerMethodField() + queue = serializers.SerializerMethodField() + kbitem = serializers.SerializerMethodField() + secret_key = serializers.CharField(read_only=True) + + class Meta: + model = Ticket + # fields = '__all__' + fields = ('ticket', 'id', 'title', 'queue', 'status', + 'created', 'due_date', 'submitter', 'kbitem', 'secret_key') + + def get_queue(self, obj): + return {"title": obj.queue.title, "id": obj.queue.id} + + def get_ticket(self, obj): + return str(obj.id) + " " + obj.ticket + + def get_status(self, obj): + return obj.get_status + + def get_created(self, obj): + return humanize.naturaltime(obj.created) + + def get_due_date(self, obj): + return humanize.naturaltime(obj.due_date) + + def get_submitter(self, obj): + return obj.submitter_email + + def get_kbitem(self, obj): + return obj.kbitem.title if obj.kbitem else "" + + +class TicketSerializer(BaseTicketSerializer): followup_set = FollowUpSerializer(many=True, read_only=True) attachment = serializers.FileField(write_only=True, required=False) @@ -164,13 +188,6 @@ class TicketSerializer(serializers.ModelSerializer): 'priority', 'due_date', 'merged_to', 'attachment', 'followup_set' ) - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - # Add custom fields - for field in CustomField.objects.all(): - self.fields['custom_%s' % field.name] = field.build_api_field() - def create(self, validated_data): """ Use TicketForm to validate and create ticket """ queues = HelpdeskUser(self.context['request'].user).get_queues() @@ -199,18 +216,3 @@ class TicketSerializer(serializers.ModelSerializer): instance = super().update(instance, validated_data) instance.save_custom_field_values(validated_data) return instance - - -class UserSerializer(serializers.ModelSerializer): - password = serializers.CharField(write_only=True) - - class Meta: - model = get_user_model() - fields = ('first_name', 'last_name', 'username', 'email', 'password') - - def create(self, validated_data): - user = super(UserSerializer, self).create(validated_data) - user.is_active = True - user.set_password(validated_data['password']) - user.save() - return user diff --git a/helpdesk/views/api.py b/helpdesk/views/api.py index f6cbe9e9..fce0e816 100644 --- a/helpdesk/views/api.py +++ b/helpdesk/views/api.py @@ -24,7 +24,11 @@ class UserTicketViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = [IsAuthenticated] def get_queryset(self): - return Ticket.objects.filter(submitter_email=self.request.user.email).order_by('-created') + tickets = Ticket.objects.filter(submitter_email=self.request.user.email).order_by('-created') + for ticket in tickets: + ticket.set_custom_field_values() + return tickets + class TicketViewSet(viewsets.ModelViewSet):