Merge pull request #1198 from samsplunks/main

Adding custom fields modifications in follow-ups
This commit is contained in:
Christopher Broderick 2024-08-16 12:18:59 +01:00 committed by GitHub
commit 8c13c1dc42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 71 additions and 3 deletions

View File

@ -202,6 +202,9 @@ Options that change ticket updates
**Default:** ``HELPDESK_STAFF_ONLY_TICKET_CC = False`` **Default:** ``HELPDESK_STAFF_ONLY_TICKET_CC = False``
- **HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST** Show configured custom fields in the follow-up form.
**Default:** ``HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST = []``
Options that change ticket properties Options that change ticket properties
------------------------------------- -------------------------------------

View File

@ -33,7 +33,8 @@ from helpdesk.settings import (
CUSTOMFIELD_DATE_FORMAT, CUSTOMFIELD_DATE_FORMAT,
CUSTOMFIELD_DATETIME_FORMAT, CUSTOMFIELD_DATETIME_FORMAT,
CUSTOMFIELD_TIME_FORMAT, CUSTOMFIELD_TIME_FORMAT,
CUSTOMFIELD_TO_FIELD_DICT CUSTOMFIELD_TO_FIELD_DICT,
HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST,
) )
from helpdesk.validators import validate_file_extension from helpdesk.validators import validate_file_extension
from helpdesk.signals import new_ticket_done from helpdesk.signals import new_ticket_done
@ -180,6 +181,46 @@ class EditTicketForm(CustomFieldMixin, forms.ModelForm):
return super(EditTicketForm, self).save(*args, **kwargs) return super(EditTicketForm, self).save(*args, **kwargs)
class EditTicketCustomFieldForm(EditTicketForm):
"""
Uses the EditTicketForm logic to provide a form for Ticket custom fields.
"""
def __init__(self, *args, **kwargs):
"""
Add any custom fields that are defined to the form
"""
super(EditTicketCustomFieldForm, self).__init__(*args, **kwargs)
if HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST:
fields = list(self.fields)
for field in fields:
if field != 'id' and field.replace("custom_", "", 1) not in HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST:
self.fields.pop(field, None)
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
fields = ('id', 'merged_to',)
class EditFollowUpForm(forms.ModelForm): class EditFollowUpForm(forms.ModelForm):
class Meta: class Meta:

View File

@ -255,6 +255,10 @@ HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP = getattr(settings,
'HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP', 'HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP',
True) True)
HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST = getattr(settings,
'HELPDESK_SHOW_CUSTOM_FIELDS_FOLLOW_UP_LIST',
[])
# show delete buttons in ticket follow ups if user is 'superuser' # show delete buttons in ticket follow ups if user is 'superuser'
HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP = getattr( HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP = getattr(
settings, 'HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP', False) settings, 'HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP', False)

View File

@ -163,6 +163,10 @@
</dl> </dl>
<dl>
<dt>{{ customfields_form }}</dt>
</dl>
</div> </div>
{% if ticket.checklists.exists %} {% if ticket.checklists.exists %}

View File

@ -206,6 +206,7 @@ def update_ticket(
due_date=None, due_date=None,
new_checklists=None, new_checklists=None,
message_id=None, message_id=None,
customfields_form=None,
): ):
# We need to allow the 'ticket' and 'queue' contexts to be applied to the # We need to allow the 'ticket' and 'queue' contexts to be applied to the
# comment. # comment.
@ -313,6 +314,10 @@ def update_ticket(
) )
ticket.due_date = due_date ticket.due_date = due_date
# save custom fields and ticket changes
if customfields_form and customfields_form.is_valid():
customfields_form.save(followup=f)
for checklist in ticket.checklists.all(): for checklist in ticket.checklists.all():
if checklist.id not in new_checklists: if checklist.id not in new_checklists:
continue continue

View File

@ -46,6 +46,7 @@ from helpdesk.forms import (
CUSTOMFIELD_DATE_FORMAT, CUSTOMFIELD_DATE_FORMAT,
EditFollowUpForm, EditFollowUpForm,
EditTicketForm, EditTicketForm,
EditTicketCustomFieldForm,
EmailIgnoreForm, EmailIgnoreForm,
FormControlDeleteFormSet, FormControlDeleteFormSet,
MultipleTicketSelectForm, MultipleTicketSelectForm,
@ -430,6 +431,9 @@ def view_ticket(request, ticket_id):
default=2 default=2
)).order_by('rank') )).order_by('rank')
# add custom fields to further details panel
customfields_form = EditTicketCustomFieldForm(None, instance=ticket)
return render(request, 'helpdesk/ticket.html', { return render(request, 'helpdesk/ticket.html', {
'ticket': ticket, 'ticket': ticket,
'dependencies': dependencies, 'dependencies': dependencies,
@ -443,6 +447,7 @@ def view_ticket(request, ticket_id):
'ticketcc_string': ticketcc_string, 'ticketcc_string': ticketcc_string,
'SHOW_SUBSCRIBE': show_subscribe, 'SHOW_SUBSCRIBE': show_subscribe,
'checklist_form': checklist_form, 'checklist_form': checklist_form,
'customfields_form': customfields_form,
}) })
@ -573,11 +578,15 @@ def update_ticket_view(request, ticket_id, public=False):
comment = request.POST.get('comment', '') comment = request.POST.get('comment', '')
new_status = int(request.POST.get('new_status', ticket.status)) new_status = int(request.POST.get('new_status', ticket.status))
title = request.POST.get('title', '') title = request.POST.get('title', ticket.title)
owner = int(request.POST.get('owner', -1)) owner = int(request.POST.get('owner', -1))
priority = int(request.POST.get('priority', ticket.priority)) priority = int(request.POST.get('priority', ticket.priority))
queue = int(request.POST.get('queue', ticket.queue.id)) 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 # Check if a change happened on checklists
new_checklists = {} new_checklists = {}
changes_in_checklists = False changes_in_checklists = False
@ -604,6 +613,7 @@ def update_ticket_view(request, ticket_id, public=False):
due_date == ticket.due_date, due_date == ticket.due_date,
(owner == -1) or (not owner and not ticket.assigned_to) or (owner == -1) or (not owner and not ticket.assigned_to) or
(owner and User.objects.get(id=owner) == ticket.assigned_to), (owner and User.objects.get(id=owner) == ticket.assigned_to),
not customfields_form.has_changed(),
]) ])
if no_changes: if no_changes:
return return_to_ticket(request.user, helpdesk_settings, ticket) return return_to_ticket(request.user, helpdesk_settings, ticket)
@ -622,6 +632,7 @@ def update_ticket_view(request, ticket_id, public=False):
time_spent = get_time_spent_from_request(request), time_spent = get_time_spent_from_request(request),
due_date = get_due_date_from_request_or_ticket(request, ticket), due_date = get_due_date_from_request_or_ticket(request, ticket),
new_checklists = new_checklists, new_checklists = new_checklists,
customfields_form = customfields_form,
) )
return return_to_ticket(request.user, helpdesk_settings, ticket) return return_to_ticket(request.user, helpdesk_settings, ticket)