From b8d06a0fb1d25e4e0f0b284092b8afb61781cbaf Mon Sep 17 00:00:00 2001 From: Benbb96 Date: Sun, 23 Apr 2023 00:36:10 +0200 Subject: [PATCH] Show a form to create a new checklist, with the possibility to use a preset template --- helpdesk/forms.py | 27 ++++++++++++++++++- helpdesk/models.py | 6 +++++ .../templates/helpdesk/ticket_desc_table.html | 8 +++++- helpdesk/views/staff.py | 15 ++++++++++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index 8a6781d2..0c6ac3d3 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -26,7 +26,9 @@ from helpdesk.models import ( TicketCC, TicketCustomFieldValue, TicketDependency, - UserSettings + UserSettings, + Checklist, + ChecklistTemplate ) from helpdesk.settings import ( CUSTOMFIELD_DATE_FORMAT, @@ -602,3 +604,26 @@ class MultipleTicketSelectForm(forms.Form): raise ValidationError( _('All selected tickets must share the same queue in order to be merged.')) return tickets + + +class ChecklistForm(forms.ModelForm): + checklist_template = forms.ModelChoiceField( + label=_("Template"), + queryset=ChecklistTemplate.objects.all(), + widget=forms.Select(attrs={'class': 'form-wontrol'}), + required=False, + ) + name = forms.CharField( + widget=forms.TextInput(attrs={'class': 'form-wontrol'}), + required=False, + ) + + class Meta: + model = Checklist + fields = ('name',) + + def clean(self): + if not self.cleaned_data.get('checklist_template') and not self.cleaned_data.get('name'): + raise ValidationError(_('Please choose at least a name or a template for the new checklist')) + if self.cleaned_data.get('checklist_template') and self.cleaned_data.get('name'): + raise ValidationError(_('Please choose either a name or a template for the new checklist')) diff --git a/helpdesk/models.py b/helpdesk/models.py index ec3f68f4..d090529d 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -2028,6 +2028,12 @@ class ChecklistTemplate(models.Model): def __str__(self): return self.name + def create_checklist_for_ticket(self, ticket): + checklist = ticket.checklists.create(name=self.name) + for task in self.task_list: + checklist.tasks.create(description=task) + return checklist + class Checklist(models.Model): ticket = models.ForeignKey( diff --git a/helpdesk/templates/helpdesk/ticket_desc_table.html b/helpdesk/templates/helpdesk/ticket_desc_table.html index 8aa85c04..36eba139 100644 --- a/helpdesk/templates/helpdesk/ticket_desc_table.html +++ b/helpdesk/templates/helpdesk/ticket_desc_table.html @@ -219,7 +219,13 @@

Add a checklist

- TODO +
+ {% csrf_token %} + {{ checklist_form.as_p }} + +
diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 934a9af9..40e8dbe1 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -48,7 +48,8 @@ from helpdesk.forms import ( TicketCCUserForm, TicketDependencyForm, TicketForm, - UserSettingsForm + UserSettingsForm, + ChecklistForm ) from helpdesk.lib import process_attachments, queue_template_context, safe_template_context from helpdesk.models import ( @@ -406,6 +407,17 @@ def view_ticket(request, ticket_id): else: submitter_userprofile_url = None + checklist_form = ChecklistForm(request.POST or None) + if checklist_form.is_valid(): + checklist_template = checklist_form.cleaned_data.get('checklist_template') + if checklist_template: + checklist_template.create_checklist_for_ticket(ticket) + else: + checklist = checklist_form.save(commit=False) + checklist.ticket = ticket + checklist.save() + return redirect(ticket) + return render(request, 'helpdesk/ticket.html', { 'ticket': ticket, 'submitter_userprofile_url': submitter_userprofile_url, @@ -416,6 +428,7 @@ def view_ticket(request, ticket_id): Q(queues=ticket.queue) | Q(queues__isnull=True)), 'ticketcc_string': ticketcc_string, 'SHOW_SUBSCRIBE': show_subscribe, + 'checklist_form': checklist_form, })