Allow a file to be attached to the ticket when the ticket is opened. These files are attached to outgoing emails. Also, updated forms to loop over form.fields rather than explicitly naming each field. Fixes issue #15.

This commit is contained in:
Ross Poulton 2009-01-22 08:08:22 +00:00
parent fa79cac822
commit 11293b0b66
5 changed files with 83 additions and 57 deletions

View File

@ -10,11 +10,12 @@ forms.py - Definitions of newforms-based forms for creating and maintaining
from datetime import datetime
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from helpdesk.lib import send_templated_mail
from helpdesk.models import Ticket, Queue, FollowUp, IgnoreEmail
from helpdesk.models import Ticket, Queue, FollowUp, Attachment, IgnoreEmail
class TicketForm(forms.Form):
queue = forms.ChoiceField(
@ -60,6 +61,12 @@ class TicketForm(forms.Form):
'as \'3\'.'),
)
attachment = forms.FileField(
required=False,
label=_('Attach File'),
help_text=_('You can attach a file such as a document or screenshot to this ticket.'),
)
def save(self, user):
"""
Writes and returns a Ticket() object
@ -98,6 +105,25 @@ class TicketForm(forms.Form):
f.save()
files = []
if self.cleaned_data['attachment']:
import mimetypes
file = self.cleaned_data['attachment']
filename = file.name.replace(' ', '_')
a = Attachment(
followup=f,
filename=filename,
mime_type=mimetypes.guess_type(filename)[0] or 'application/octet-stream',
size=file.size,
)
a.file.save(file.name, file, save=False)
a.save()
if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000):
# Only files smaller than 512kb (or as defined in
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
files.append(a.file.path)
context = {
'ticket': t,
'queue': q,
@ -110,6 +136,7 @@ class TicketForm(forms.Form):
recipients=t.submitter_email,
sender=q.from_address,
fail_silently=True,
files=files,
)
if t.assigned_to and t.assigned_to != user and getattr(t.assigned_to.usersettings.settings, 'email_on_ticket_assign', False):
@ -119,6 +146,7 @@ class TicketForm(forms.Form):
recipients=t.assigned_to.email,
sender=q.from_address,
fail_silently=True,
files=files,
)
if q.new_ticket_cc:
@ -128,6 +156,7 @@ class TicketForm(forms.Form):
recipients=q.new_ticket_cc,
sender=q.from_address,
fail_silently=True,
files=files,
)
if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc:
@ -137,6 +166,7 @@ class TicketForm(forms.Form):
recipients=q.updated_ticket_cc,
sender=q.from_address,
fail_silently=True,
files=files,
)
return t
@ -178,6 +208,12 @@ class PublicTicketForm(forms.Form):
help_text=_('Please select a priority carefully.'),
)
attachment = forms.FileField(
required=False,
label=_('Attach File'),
help_text=_('You can attach a file such as a document or screenshot to this ticket.'),
)
def save(self):
"""
Writes and returns a Ticket() object
@ -207,6 +243,25 @@ class PublicTicketForm(forms.Form):
f.save()
files = []
if self.cleaned_data['attachment']:
import mimetypes
file = self.cleaned_data['attachment']
filename = file.name.replace(' ', '_')
a = Attachment(
followup=f,
filename=filename,
mime_type=mimetypes.guess_type(filename)[0] or 'application/octet-stream',
size=file.size,
)
a.file.save(file.name, file, save=False)
a.save()
if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000):
# Only files smaller than 512kb (or as defined in
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
files.append(a.file.path)
context = {
'ticket': t,
'queue': q,
@ -218,6 +273,7 @@ class PublicTicketForm(forms.Form):
recipients=t.submitter_email,
sender=q.from_address,
fail_silently=True,
files=files,
)
if q.new_ticket_cc:
@ -227,6 +283,7 @@ class PublicTicketForm(forms.Form):
recipients=q.new_ticket_cc,
sender=q.from_address,
fail_silently=True,
files=files,
)
if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc:
@ -236,6 +293,7 @@ class PublicTicketForm(forms.Form):
recipients=q.updated_ticket_cc,
sender=q.from_address,
fail_silently=True,
files=files,
)
return t

View File

@ -7,35 +7,19 @@
<p>Unless otherwise stated, all fields are required. Please provide as descriptive a title and description as possible.</p>{% endblocktrans %}
<form method='post' action='./'>
<form method='post' action='./' encrypt='multipart/form-data'>
<fieldset>
<dl>
<dt><label for='id_queue'>{{ form.queue.label }}</label></dt>
<dd>{{ form.queue }}</dd>
{% if form.queue.errors %}<dd class='error'>{{ form.queue.errors }}</dd>{% endif %}
<dt><label for='id_title'>{{ form.title.label }}</label></dt>
<dd>{{ form.title }}</dd>
{% if form.title.errors %}<dd class='error'>{{ form.title.errors }}</dd>{% endif %}
<dt><label for='id_submitter_email'>{{ form.submitter_email.label }}</label> <span class='form_optional'>{% trans "(Optional)" %}</span></dt>
<dd>{{ form.submitter_email }}</dd>
{% if form.submitter_email.errors %}<dd class='error'>{{ form.submitter_email.errors }}</dd>{% endif %}
<dd class='form_help_text'>{{ form.submitter_email.help_text }}</dd>
<dt><label for='id_body'>{{ form.body.label }}</label></dt>
<dd>{{ form.body }}</dd>
{% if form.body.errors %}<dd class='error'>{{ form.body.errors }}</dd>{% endif %}
<dt><label for='id_assigned_to'>{{ form.assigned_to.label }}</label> <span class='form_optional'>{% trans "(Optional)" %}</span></dt>
<dd>{{ form.assigned_to }}</dd>
{% if form.assigned_to.errors %}<dd class='error'>{{ form.assigned_to.errors }}</dd>{% endif %}
<dd class='form_help_text'>{{ form.assigned_to.help_text }}</dd>
<dt><label for='id_priority'>{{ form.priority.label }}</label></dt>
<dd>{{ form.priority }}</dd>
{% if form.priority.errors %}<dd class='error'>{{ form.priority.errors }}</dd>{% endif %}
<dd class='form_help_text'>{{ form.priority.help_text }}</dd>
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
<dt><label for='id_{{ field.name }}'>{{ field.label }}</label>{% if not field.field.required %} <span class='form_optional'>{% trans "(Optional)" %}</span>{% endif %}</dt>
<dd>{{ field }}</dd>
{% if field.errors %}<dd class='error'>{{ field.errors }}</dd>{% endif %}
{% if field.help_text %}<dd class='form_help_text'>{{ field.help_text }}</dd>{% endif %}</label>
{% endif %}
{% endfor %}
</dl>
<div class='buttons'>

View File

@ -21,35 +21,19 @@
<p>{% trans "All fields are required. Please provide as descriptive a title and description as possible." %}</p>
<form method='post' action='./#submit'>
<form method='post' action='./#submit' enctype='multipart/form-data'>
<fieldset>
<dl>
<dt><label for='id_queue'>{{ form.queue.label }}</label></dt>
<dd>{{ form.queue }}</dd>
{% if form.queue.errors %}
<dd class='error'>{{ form.queue.errors }}</dd>{% endif %}
<dt><label for='id_title'>{{ form.title.label }}</label></dt>
<dd>{{ form.title }}</dd>
{% if form.title.errors %}
<dd class='error'>{{ form.title.errors }}</dd>{% endif %}
<dt><label for='id_submitter_email'>{{ form.submitter_email.label }}</label></dt>
<dd>{{ form.submitter_email }}</dd>
{% if form.submitter_email.errors %}
<dd class='error'>{{ form.submitter_email.errors }}</dd>{% endif %}
<dd class='form_help_text'>{{ form.submitter_email.help_text }}</dd>
<dt><label for='id_body'>{{ form.body.label }}</label></dt>
<dd>{{ form.body }}</dd>
{% if form.body.errors %}
<dd class='error'>{{ form.body.errors }}</dd>{% endif %}
<dt><label for='id_priority'>{{ form.priority.label }}</label></dt>
<dd>{{ form.priority }}</dd>
{% if form.priority.errors %}
<dd class='error'>{{ form.priority.errors }}</dd>{% endif %}
<dd class='form_help_text'>{{ form.priority.help_text }}</dd>
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
<dt><label for='id_{{ field.name }}'>{{ field.label }}</label>{% if not field.field.required %} <span class='form_optional'>{% trans "(Optional)" %}</span>{% endif %}</dt>
<dd>{{ field }}</dd>
{% if field.errors %}<dd class='error'>{{ field.errors }}</dd>{% endif %}
{% if field.help_text %}<dd class='form_help_text'>{{ field.help_text }}</dd>{% endif %}</label>
{% endif %}
{% endfor %}
</dl>
<div class='buttons'>

View File

@ -28,7 +28,7 @@ def homepage(request):
return HttpResponseRedirect(reverse('helpdesk_dashboard'))
if request.method == 'POST':
form = PublicTicketForm(request.POST)
form = PublicTicketForm(request.POST, request.FILES)
form.fields['queue'].choices = [('', '--------')] + [[q.id, q.title] for q in Queue.objects.filter(allow_public_submission=True)]
if form.is_valid():
ticket = form.save()

View File

@ -402,7 +402,7 @@ ticket_list = staff_member_required(ticket_list)
def create_ticket(request):
if request.method == 'POST':
form = TicketForm(request.POST)
form = TicketForm(request.POST, request.FILES)
form.fields['queue'].choices = [('', '--------')] + [[q.id, q.title] for q in Queue.objects.all()]
form.fields['assigned_to'].choices = [('', '--------')] + [[u.id, u.username] for u in User.objects.filter(is_active=True)]
if form.is_valid():