From 9ee0207c3c7665e45f9f60d663567e01b900b8b6 Mon Sep 17 00:00:00 2001 From: Sam Splunks <72095718+samsplunks@users.noreply.github.com> Date: Fri, 26 Jul 2024 10:02:05 +0000 Subject: [PATCH] Save custom fields in a followup --- helpdesk/forms.py | 20 ++++++++++++++++++++ helpdesk/update_ticket.py | 7 ++++++- helpdesk/views/staff.py | 6 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index 3b2c3c81..7b2dd459 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -192,7 +192,27 @@ class EditTicketCustomFieldForm(EditTicketForm): super(EditTicketCustomFieldForm, self).__init__(*args, **kwargs) del self.fields['merged_to'] + + def save(self, *args, **kwargs): + + # if form is saved in a ticket update, it is passed + # a followup instance to trace custom fields changes + if "followup" in kwargs: + followup = kwargs.pop('followup', None) + + for field, value in self.cleaned_data.items(): + if field.startswith('custom_'): + if value != self.fields[field].initial: + c = followup.ticketchange_set.create( + field=field.replace('custom_', '', 1), + old_value=self.fields[field].initial, + new_value=value, + ) + + super(EditTicketCustomFieldForm, self).save(*args, **kwargs) + + class Meta: model = Ticket exclude = ('title', 'queue', 'created', 'modified', diff --git a/helpdesk/update_ticket.py b/helpdesk/update_ticket.py index 989e04e4..20bac2d1 100644 --- a/helpdesk/update_ticket.py +++ b/helpdesk/update_ticket.py @@ -206,6 +206,7 @@ def update_ticket( due_date=None, new_checklists=None, message_id=None, + customfields_form=None, ): # We need to allow the 'ticket' and 'queue' contexts to be applied to the # comment. @@ -312,7 +313,11 @@ def update_ticket( new_value=due_date, ) ticket.due_date = due_date - + + # save custom fields and ticket changes + if customfields_form.is_valid(): + customfields_form.save(followup=f) + for checklist in ticket.checklists.all(): if checklist.id not in new_checklists: continue diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 6466bb5c..be166546 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -583,6 +583,10 @@ def update_ticket_view(request, ticket_id, public=False): priority = int(request.POST.get('priority', ticket.priority)) queue = int(request.POST.get('queue', ticket.queue.id)) + # custom fields + customfields_form = EditTicketCustomFieldForm(request.POST or None, + instance=ticket) + # Check if a change happened on checklists new_checklists = {} changes_in_checklists = False @@ -609,6 +613,7 @@ def update_ticket_view(request, ticket_id, public=False): due_date == ticket.due_date, (owner == -1) or (not owner and not ticket.assigned_to) or (owner and User.objects.get(id=owner) == ticket.assigned_to), + not customfields_form.has_changed(), ]) if no_changes: return return_to_ticket(request.user, helpdesk_settings, ticket) @@ -627,6 +632,7 @@ def update_ticket_view(request, ticket_id, public=False): time_spent = get_time_spent_from_request(request), due_date = get_due_date_from_request_or_ticket(request, ticket), new_checklists = new_checklists, + customfields_form = customfields_form, ) return return_to_ticket(request.user, helpdesk_settings, ticket)