Merge pull request #1152 from timthelion/user-tickets-custom-fields

User tickets custom fields
This commit is contained in:
Christopher Broderick 2024-01-17 22:38:55 +00:00 committed by GitHub
commit a50065e768
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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 ""
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

View File

@ -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):