diff --git a/docs/settings.rst b/docs/settings.rst index 3d0131b3..cac6b07a 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -202,6 +202,9 @@ Options that change ticket updates **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 ------------------------------------- diff --git a/helpdesk/forms.py b/helpdesk/forms.py index 83a3de45..b2fbdb25 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -33,7 +33,8 @@ from helpdesk.settings import ( CUSTOMFIELD_DATE_FORMAT, CUSTOMFIELD_DATETIME_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.signals import new_ticket_done @@ -180,6 +181,46 @@ class EditTicketForm(CustomFieldMixin, forms.ModelForm): 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 Meta: diff --git a/helpdesk/settings.py b/helpdesk/settings.py index d6d1acbb..0d983e53 100644 --- a/helpdesk/settings.py +++ b/helpdesk/settings.py @@ -255,6 +255,10 @@ HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP = getattr(settings, 'HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP', 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' HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP = getattr( settings, 'HELPDESK_SHOW_DELETE_BUTTON_SUPERUSER_FOLLOW_UP', False) diff --git a/helpdesk/templates/helpdesk/ticket.html b/helpdesk/templates/helpdesk/ticket.html index ce7d61e4..dfa8c9ee 100644 --- a/helpdesk/templates/helpdesk/ticket.html +++ b/helpdesk/templates/helpdesk/ticket.html @@ -163,6 +163,10 @@ +