Merge pull request #1189 from jorge-leon/attachment-enable-setting

Attachment enable setting
This commit is contained in:
Christopher Broderick 2024-06-15 14:32:51 +01:00 committed by GitHub
commit f883868361
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 204 additions and 115 deletions

View File

@ -39,8 +39,8 @@ If you want to override the default settings for your users, create ``HELPDESK_D
}
Access controll & Security
---------------
Access control & Security
-------------------------
These settings can be used to change who can access the helpdesk.
- **HELPDESK_PUBLIC_VIEW_PROTECTOR** This is a function that takes a request and can either return `None` granting access to to a public view or a redirect denying access.
@ -51,16 +51,30 @@ These settings can be used to change who can access the helpdesk.
**Default:** ``HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = False``
- **HELPDESK_VALID_EXTENSIONS** Valid extensions for file types that can be attached to tickets. Note: This used to be calle **VALID_EXTENSIONS** which is now deprecated.
**Default:** ``HELPDESK_VALID_EXTENSIONS = ['.txt', '.asc', '.htm', '.html', '.pdf', '.doc', '.docx', '.odt', '.jpg', '.png', '.eml']
- **HELPDESK_VALIDATE_ATTACHMENT_TYPES** If you'd like to turn of filtering of helpdesk extension types you can set this to False.
- **HELPDESK_ANON_ACCESS_RAISES_404** If True, redirects user to a 404 page when attempting to reach ticket pages while not logged in, rather than redirecting to a login screen.
**Default:** ``HELPDESK_ANON_ACCESS_RAISES_404 = False``
Settings related to attachments:
- **HELPDESK_ENABLE_ATTACHMENTS** If set to ``True``, files can be
attached to tickets and followups, and emails are searched for
attachments which are then attached to the ticket. Also enables the
``HELPDESK_ALWAYS_SAVE_INCOMING_EMAIL_MESSAGE`` setting.
**Caution**: Set this to False, unless you have secured access to
the uploaded files. Otherwise anyone on the Internet will be able
to download your ticket attachments.
Attachments are enabled by default for backwards compatibility.
- **HELPDESK_VALID_EXTENSIONS** Valid extensions for file types that can be attached to tickets. Note: This used to be called **VALID_EXTENSIONS** which is now deprecated.
**Default:** ``HELPDESK_VALID_EXTENSIONS = ['.txt', '.asc', '.htm', '.html', '.pdf', '.doc', '.docx', '.odt', '.jpg', '.png', '.eml']``
- **HELPDESK_VALIDATE_ATTACHMENT_TYPES** If you'd like to turn of filtering of helpdesk extension types you can set this to False.
Generic Options
---------------
These changes are visible throughout django-helpdesk
@ -422,4 +436,9 @@ The following settings were defined in previous versions and are no longer suppo
- **HELPDESK_FULL_FIRST_MESSAGE_FROM_EMAIL** Do not ignore fowarded and replied text from the email messages which create a new ticket; useful for cases when customer forwards some email (error from service or something) and wants support to see that
- **HELPDESK_ALWAYS_SAVE_INCOMING_EMAIL_MESSAGE** Any incoming .eml message is saved and available, helps when customer spent some time doing fancy markup which has been corrupted during the email-to-ticket-comment translate process
- **HELPDESK_ALWAYS_SAVE_INCOMING_EMAIL_MESSAGE** Any incoming .eml
message is saved and available, helps when customer spent some time
doing fancy markup which has been corrupted during the
email-to-ticket-comment translate process.
Requires ``HELPDESK_ENABLE_ATTACHMENTS`` to be set to `True`

View File

@ -595,6 +595,7 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
logger.info("[%s-%s] %s" % (ticket.queue.slug, ticket.id, ticket.title,))
if settings.HELPDESK_ENABLE_ATTACHMENTS:
try:
attached = process_attachments(f, files)
except ValidationError as e:
@ -984,7 +985,7 @@ def extract_email_metadata(message: str,
filtered_body, full_body = extract_email_message_content(message_obj, files, include_chained_msgs)
# If the base part is not a multipart then it will have already been processed as the vbody content so
# no need to process attachments
if "multipart" == message_obj.get_content_maintype():
if "multipart" == message_obj.get_content_maintype() and settings.HELPDESK_ENABLE_ATTACHMENTS:
# Find and attach all other parts or part contents as attachments
counter, content_parts_excluded = extract_attachments(message_obj, files, logger)
if not content_parts_excluded:
@ -994,6 +995,7 @@ def extract_email_metadata(message: str,
Verify that there were no text/* parts containing message content.")
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Email parsed and %s attachments were found and attached.", counter)
if settings.HELPDESK_ENABLE_ATTACHMENTS:
add_file_if_always_save_incoming_email_message(files, message)
smtp_priority = message_obj.get('priority', '')

View File

@ -239,6 +239,7 @@ class AbstractTicketForm(CustomFieldMixin, forms.Form):
label=_('Due on'),
)
if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS:
attachment = forms.FileField(
widget=forms.FileInput(attrs={'class': 'form-control-file'}),
required=False,
@ -326,7 +327,7 @@ class AbstractTicketForm(CustomFieldMixin, forms.Form):
return followup
def _attach_files_to_follow_up(self, followup):
files = self.cleaned_data['attachment']
files = self.cleaned_data.get('attachment')
if files:
files = process_attachments(followup, [files])
return files
@ -418,7 +419,10 @@ class TicketForm(AbstractTicketForm):
followup = self._create_follow_up(ticket, title=title, user=user)
followup.save()
if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS:
files = self._attach_files_to_follow_up(followup)
else:
files = None
# emit signal when the TicketForm.save is done
new_ticket_done.send(sender="TicketForm", ticket=ticket)

View File

@ -56,6 +56,15 @@ HELPDESK_STAFF_VIEW_PROTECTOR = getattr(settings,
'HELPDESK_STAFF_VIEW_PROTECTOR',
lambda _: None)
# Enable ticket and Email attachments
#
# Caution! Set this to False, unless you have secured access to
# the uploaded files. Otherwise anyone on the Internet will be
# able to download your ticket attachments.
HELPDESK_ENABLE_ATTACHMENTS = getattr(settings,
'HELPDESK_ENABLE_ATTACHMENTS',
True)
# Enable the Dependencies field on ticket view
HELPDESK_ENABLE_DEPENDENCIES_ON_TICKET = getattr(settings,
'HELPDESK_ENABLE_DEPENDENCIES_ON_TICKET',

View File

@ -9,6 +9,7 @@
{% include 'helpdesk/base-head.html' %}
{% block helpdesk_head %}{% endblock %}
{% include 'helpdesk/base_js.html' %}
</head>

View File

@ -81,10 +81,13 @@
<li>{% blocktrans with change.field as field and change.old_value as old_value and change.new_value as new_value %}Changed {{ field }} from {{ old_value }} to {{ new_value }}.{% endblocktrans %}</li>
{% endfor %}
</ul></div>{% endif %}
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
{% for attachment in followup.followupattachment_set.all %}{% if forloop.first %}<div class='attachments'><ul>{% endif %}
<li><a href='{{ attachment.file.url }}'>{{ attachment.filename }}</a> ({{ attachment.mime_type }}, {{ attachment.size|filesizeformat }})</li>
{% if forloop.last %}</ul></div>{% endif %}
{% endfor %}
{% endif %}
</div>
{% endfor %}
{% endif %}
@ -123,6 +126,7 @@
</dl>
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
<p id='ShowFileUploadPara'><button class="btn btn-warning btn-sm"
id='ShowFileUpload' onclick="$('#FileUpload')[0].style.display='block';return false;" >{% trans "Attach File(s) &raquo;" %}</button></p>
@ -132,6 +136,7 @@
<dt><label for='id_file'>{% trans "Attach a File" %}</label></dt>
<dd>
<div class="add_file_fields_wrap">
<button class="add_file_field_button btn btn-success btn-xs">{% trans "Add Another File" %}</button>
<div><label class='btn btn-primary btn-sm btn-file'>
Browse... <input type="file" name='attachment' id='file0' style='display: none;'/>
</label><span>&nbsp;</span><span id='selectedfilename0'>{% trans 'No files selected.' %}</span></div>
@ -140,12 +145,52 @@
</dl>
</div>
{% endif %}
</fieldset>
<button class="btn btn-primary btn-lg" style="margin-bottom:10px" type='submit'>{% trans "Update This Ticket" %}</button>
{% csrf_token %}</form>
{% csrf_token %}
</form>
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
<script type='text/javascript' language='javascript'>
$(document).ready(function() {
$("#ShowFileUpload").click(function() {
$("#FileUpload").fadeIn();
$("#ShowFileUploadPara").hide();
});
// lists for file input change events, then updates the associated text label
// with the file name selected
$('.add_file_fields_wrap').on('fileselect', ':file', function(event, numFiles, label, browseButtonNum) {
$("#selectedfilename"+browseButtonNum).html(label);
});
var x = 0;
var wrapper = $(".add_file_fields_wrap"); //Fields wrapper
var add_button = $(".add_file_field_button"); //Add button ID
$(add_button).click(function(e){ //on add input button click
x++;
e.preventDefault();
$(wrapper).append("<div><label class='btn btn-primary btn-sm btn-file'>Browse... <input type='file' name='attachment' id='file" + x + "' multiple style='display: none;'/></label><span>&nbsp;</span><span id='selectedfilename" + x + "'>{% trans 'No files selected.' %}</span></div>"); //add input box
});
});
// this function listens for changes on any file input, and
// emits the appropriate event to update the input's text.
// Needed to have properly styled file input buttons! (this really shouldn't be this hard...)
$(document).on('change', ':file', function() {
var input = $(this),
inputWidgetNum = $(this).attr('id').split("file")[1],
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label, inputWidgetNum]);
});
</script>
{% endif %}
{% endblock %}

View File

@ -54,6 +54,7 @@
<li>{% blocktrans with change.field as field and change.old_value as old_value and change.new_value as new_value %}Changed {{ field }} from {{ old_value }} to {{ new_value }}.{% endblocktrans %}</li>
{% if forloop.last %}</ul></div>{% endif %}
{% endfor %}
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
{% for attachment in followup.followupattachment_set.all %}{% if forloop.first %}{% trans "Attachments" %}:<div class='attachments'><ul>{% endif %}
<li><a href='{{ attachment.file.url }}'>{{ attachment.filename }}</a> ({{ attachment.mime_type }}, {{ attachment.size|filesizeformat }})
{% if followup.user and request.user == followup.user %}
@ -62,6 +63,7 @@
</li>
{% if forloop.last %}</ul></div>{% endif %}
{% endfor %}
{% endif %}
</p>
<!--- ugly long test to suppress the following if it will be empty, to save vertical space -->
{% with possible=helpdesk_settings.HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP %}
@ -197,7 +199,9 @@
</div>
{% endif %}
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
<p id='ShowFileUploadPara'><button type="button" class="btn btn-warning btn-sm" id='ShowFileUpload'>{% trans "Attach File(s) &raquo;" %}</button></p>
{% endif %}
<div id='FileUpload' style='display: none;'>
@ -271,11 +275,6 @@ $(document).ready(function() {
$("#checklistEdit").toggle();
});
$("#ShowFileUpload").click(function() {
$("#FileUpload").fadeIn();
$("#ShowFileUploadPara").hide();
});
$('#id_preset').change(function() {
preset = $('#id_preset').val();
if (preset != '') {
@ -300,6 +299,12 @@ $(document).ready(function() {
$("[data-toggle=tooltip]").tooltip();
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
$("#ShowFileUpload").click(function() {
$("#FileUpload").fadeIn();
$("#ShowFileUploadPara").hide();
});
// lists for file input change events, then updates the associated text label
// with the file name selected
$('.add_file_fields_wrap').on('fileselect', ':file', function(event, numFiles, label, browseButtonNum) {
@ -315,9 +320,10 @@ $(document).ready(function() {
e.preventDefault();
$(wrapper).append("<div><label class='btn btn-primary btn-sm btn-file'>Browse... <input type='file' name='attachment' id='file" + x + "' multiple style='display: none;'/></label><span>&nbsp;</span><span id='selectedfilename" + x + "'>{% trans 'No files selected.' %}</span></div>"); //add input box
});
{% endif %}
});
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
// this function listens for changes on any file input, and
// emits the appropriate event to update the input's text.
// Needed to have properly styled file input buttons! (this really shouldn't be this hard...)
@ -328,5 +334,6 @@ $(document).on('change', ':file', function() {
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label, inputWidgetNum]);
});
{% endif %}
</script>
{% endblock %}

View File

@ -191,6 +191,7 @@
<td> <a href ="{{ticket.kbitem.query_url}}"> {{ticket.kbitem}} </a> </td>
</tr>
{% endif %}
{% if helpdesk_settings.HELPDESK_ENABLE_ATTACHMENTS %}
<tr>
<th class="table-active">{% trans "Attachments" %}</th>
<td colspan="3">
@ -212,6 +213,7 @@
</ul>
</td>
</tr>
{% endif %}
<tr>
<th class="table-active">{% trans "Checklists" %}</th>
<td colspan="3">