Show a form to create a new checklist, with the possibility to use a preset template

This commit is contained in:
Benbb96 2023-04-23 00:36:10 +02:00
parent 8be55fb7f2
commit b8d06a0fb1
No known key found for this signature in database
4 changed files with 53 additions and 3 deletions

View File

@ -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'))

View File

@ -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(

View File

@ -219,7 +219,13 @@
<h4>Add a checklist</h4>
</div>
<div class="card-body">
TODO
<form method="post">
{% csrf_token %}
{{ checklist_form.as_p }}
<button type="submit" class="btn btn-primary">
{% trans "Add" %}
</button>
</form>
</div>
</div>
</div>

View File

@ -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,
})