From ef88a2ee8a42c70b8ac8c9ac5a95f0a08f42fc3d Mon Sep 17 00:00:00 2001 From: Benbb96 Date: Sun, 30 Apr 2023 03:11:27 +0200 Subject: [PATCH] Better workflow for creating new ticket checklist, into a modal --- helpdesk/forms.py | 11 +---- helpdesk/models.py | 10 ++--- helpdesk/templates/helpdesk/ticket.html | 40 +++++++++++++++++++ .../templates/helpdesk/ticket_desc_table.html | 20 +++------- helpdesk/views/staff.py | 14 ++++--- 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index f1496ccb..7dee26ce 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -641,15 +641,8 @@ class CreateChecklistForm(ChecklistForm): required=False, ) - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.fields['name'].required = False - - 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')) + class Meta(ChecklistForm.Meta): + fields = ('checklist_template', 'name') class FormControlDeleteFormSet(forms.BaseInlineFormSet): diff --git a/helpdesk/models.py b/helpdesk/models.py index c2e90bb6..eeca54e2 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -2028,12 +2028,6 @@ 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 position, task in enumerate(self.task_list): - checklist.tasks.create(description=task, position=position) - return checklist - class Checklist(models.Model): ticket = models.ForeignKey( @@ -2054,6 +2048,10 @@ class Checklist(models.Model): def __str__(self): return self.name + def create_tasks_from_template(self, template): + for position, task in enumerate(template.task_list): + self.tasks.create(description=task, position=position) + class ChecklistTaskQuerySet(models.QuerySet): def todo(self): diff --git a/helpdesk/templates/helpdesk/ticket.html b/helpdesk/templates/helpdesk/ticket.html index 772b49a9..bf817d52 100644 --- a/helpdesk/templates/helpdesk/ticket.html +++ b/helpdesk/templates/helpdesk/ticket.html @@ -245,6 +245,33 @@ + + {% endif %} {% endblock %} @@ -280,6 +307,19 @@ $(document).ready(function() { } }); + // Preset name of checklist when a template is selected + $('#id_checklist_template').on('change', function() { + const nameField = $('#id_name') + const selectedTemplate = $(this).children(':selected') + if (nameField.val() === '' && selectedTemplate.val()) { + nameField.val(selectedTemplate.text()) + } + }) + + $('.disabledTask').on('click', () => { + alert('{% trans 'If you want to update state of checklist tasks, please do a Follow-Up response and click on "Update checklists"' %}') + }) + $("[data-toggle=tooltip]").tooltip(); // lists for file input change events, then updates the associated text label diff --git a/helpdesk/templates/helpdesk/ticket_desc_table.html b/helpdesk/templates/helpdesk/ticket_desc_table.html index 967196e2..1c3d3cfd 100644 --- a/helpdesk/templates/helpdesk/ticket_desc_table.html +++ b/helpdesk/templates/helpdesk/ticket_desc_table.html @@ -196,7 +196,7 @@
{% for task in checklist.tasks.all %}
-
{% endfor %}
-
-
-
Add a checklist
-
-
-
- {% csrf_token %} - {{ checklist_form.as_p }} - -
-
-
+
diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 31dc0ff3..e8f3f1e5 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -416,14 +416,16 @@ def view_ticket(request, ticket_id): checklist_form = CreateChecklistForm(request.POST or None) if checklist_form.is_valid(): + checklist = checklist_form.save(commit=False) + checklist.ticket = ticket + checklist.save() + checklist_template = checklist_form.cleaned_data.get('checklist_template') + # Add predefined tasks if template has been selected 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) + checklist.create_tasks_from_template(checklist_template) + + return redirect('helpdesk:edit_ticket_checklist', ticket.id, checklist.id) return render(request, 'helpdesk/ticket.html', { 'ticket': ticket,