Include custom fields in user tickets api endpoint

This commit is contained in:
Timothy Hobbs 2024-01-16 22:19:32 +00:00
parent b0977a44d0
commit c9f742227e
2 changed files with 71 additions and 65 deletions

View File

@ -72,47 +72,6 @@ class DatatablesTicketSerializer(serializers.ModelSerializer):
return obj.kbitem.title if obj.kbitem else "" 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 FollowUpAttachmentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = FollowUpAttachment 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) followup_set = FollowUpSerializer(many=True, read_only=True)
attachment = serializers.FileField(write_only=True, required=False) attachment = serializers.FileField(write_only=True, required=False)
@ -164,13 +188,6 @@ class TicketSerializer(serializers.ModelSerializer):
'priority', 'due_date', 'merged_to', 'attachment', 'followup_set' '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): def create(self, validated_data):
""" Use TicketForm to validate and create ticket """ """ Use TicketForm to validate and create ticket """
queues = HelpdeskUser(self.context['request'].user).get_queues() queues = HelpdeskUser(self.context['request'].user).get_queues()
@ -199,18 +216,3 @@ class TicketSerializer(serializers.ModelSerializer):
instance = super().update(instance, validated_data) instance = super().update(instance, validated_data)
instance.save_custom_field_values(validated_data) instance.save_custom_field_values(validated_data)
return instance 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

View File

@ -24,7 +24,11 @@ class UserTicketViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
def get_queryset(self): 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): class TicketViewSet(viewsets.ModelViewSet):