2008-08-19 10:50:38 +02:00
|
|
|
"""
|
2011-01-26 00:08:41 +01:00
|
|
|
django-helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-02-06 05:36:07 +01:00
|
|
|
|
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
|
|
|
|
|
|
|
lib.py - Common functions (eg multipart e-mail)
|
2008-01-07 21:22:13 +01:00
|
|
|
"""
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2016-10-21 17:14:12 +02:00
|
|
|
import logging
|
2016-11-10 17:23:16 +01:00
|
|
|
import mimetypes
|
|
|
|
import os
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2018-07-19 06:06:57 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models import Q
|
2018-12-28 16:53:28 +01:00
|
|
|
from django.utils.encoding import smart_text, smart_str
|
2018-07-19 06:06:57 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
2019-03-07 21:58:04 +01:00
|
|
|
from helpdesk.models import FollowUpAttachment, EmailTemplate
|
2018-07-19 06:06:57 +02:00
|
|
|
|
2018-10-15 02:23:28 +02:00
|
|
|
from model_utils import Choices
|
|
|
|
|
2018-12-22 01:14:54 +01:00
|
|
|
from base64 import b64encode
|
|
|
|
from base64 import b64decode
|
|
|
|
|
|
|
|
import json
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2016-10-21 17:14:12 +02:00
|
|
|
logger = logging.getLogger('helpdesk')
|
|
|
|
|
2018-12-22 01:24:33 +01:00
|
|
|
|
2018-12-22 01:14:54 +01:00
|
|
|
def query_to_base64(query):
|
|
|
|
"""
|
|
|
|
Converts a query dict object to a base64-encoded bytes object.
|
|
|
|
"""
|
|
|
|
return b64encode(json.dumps(query).encode('UTF-8'))
|
|
|
|
|
2018-12-22 01:24:33 +01:00
|
|
|
|
2018-12-22 01:14:54 +01:00
|
|
|
def query_from_base64(b64data):
|
|
|
|
"""
|
|
|
|
Converts base64-encoded bytes object back to a query dict object.
|
|
|
|
"""
|
|
|
|
return json.loads(b64decode(b64data).decode('utf-8'))
|
|
|
|
|
2011-07-01 10:13:01 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
def query_to_dict(results, descriptions):
|
2008-08-19 10:50:38 +02:00
|
|
|
"""
|
|
|
|
Replacement method for cursor.dictfetchall() as that method no longer
|
2008-05-07 11:04:18 +02:00
|
|
|
exists in psycopg2, and I'm guessing in other backends too.
|
2008-08-19 10:50:38 +02:00
|
|
|
|
|
|
|
Converts the results of a raw SQL query into a list of dictionaries, suitable
|
|
|
|
for use in templates etc.
|
|
|
|
"""
|
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
output = []
|
|
|
|
for data in results:
|
|
|
|
row = {}
|
|
|
|
i = 0
|
|
|
|
for column in descriptions:
|
|
|
|
row[column[0]] = data[i]
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
output.append(row)
|
|
|
|
return output
|
2008-08-28 11:06:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def apply_query(queryset, params):
|
|
|
|
"""
|
2012-05-11 17:15:46 +02:00
|
|
|
Apply a dict-based set of filters & parameters to a queryset.
|
2008-08-28 11:06:24 +02:00
|
|
|
|
2011-11-05 01:56:53 +01:00
|
|
|
queryset is a Django queryset, eg MyModel.objects.all() or
|
2008-08-28 11:06:24 +02:00
|
|
|
MyModel.objects.filter(user=request.user)
|
|
|
|
|
|
|
|
params is a dictionary that contains the following:
|
|
|
|
filtering: A dict of Django ORM filters, eg:
|
|
|
|
{'user__id__in': [1, 3, 103], 'title__contains': 'foo'}
|
2016-10-14 10:04:28 +02:00
|
|
|
|
2016-07-04 17:47:53 +02:00
|
|
|
search_string: A freetext search string
|
|
|
|
|
2008-08-28 11:06:24 +02:00
|
|
|
sorting: The name of the column to sort by
|
|
|
|
"""
|
|
|
|
for key in params['filtering'].keys():
|
|
|
|
filter = {key: params['filtering'][key]}
|
|
|
|
queryset = queryset.filter(**filter)
|
|
|
|
|
2016-07-04 17:47:53 +02:00
|
|
|
search = params.get('search_string', None)
|
|
|
|
if search:
|
|
|
|
qset = (
|
|
|
|
Q(title__icontains=search) |
|
|
|
|
Q(description__icontains=search) |
|
|
|
|
Q(resolution__icontains=search) |
|
|
|
|
Q(submitter_email__icontains=search)
|
|
|
|
)
|
|
|
|
|
|
|
|
queryset = queryset.filter(qset)
|
2008-08-28 11:06:24 +02:00
|
|
|
|
2012-01-18 14:39:36 +01:00
|
|
|
sorting = params.get('sorting', None)
|
2012-05-11 17:15:46 +02:00
|
|
|
if sorting:
|
2012-01-18 14:39:36 +01:00
|
|
|
sortreverse = params.get('sortreverse', None)
|
2012-01-18 23:36:58 +01:00
|
|
|
if sortreverse:
|
2012-01-18 14:39:36 +01:00
|
|
|
sorting = "-%s" % sorting
|
|
|
|
queryset = queryset.order_by(sorting)
|
2008-08-28 11:06:24 +02:00
|
|
|
|
|
|
|
return queryset
|
2008-08-29 11:11:02 +02:00
|
|
|
|
|
|
|
|
2017-08-21 01:26:45 +02:00
|
|
|
def ticket_template_context(ticket):
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
for field in ('title', 'created', 'modified', 'submitter_email',
|
|
|
|
'status', 'get_status_display', 'on_hold', 'description',
|
|
|
|
'resolution', 'priority', 'get_priority_display',
|
|
|
|
'last_escalation', 'ticket', 'ticket_for_url',
|
|
|
|
'get_status', 'ticket_url', 'staff_url', '_get_assigned_to'
|
|
|
|
):
|
|
|
|
attr = getattr(ticket, field, None)
|
|
|
|
if callable(attr):
|
|
|
|
context[field] = '%s' % attr()
|
|
|
|
else:
|
|
|
|
context[field] = attr
|
|
|
|
context['assigned_to'] = context['_get_assigned_to']
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
def queue_template_context(queue):
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
for field in ('title', 'slug', 'email_address', 'from_address', 'locale'):
|
|
|
|
attr = getattr(queue, field, None)
|
|
|
|
if callable(attr):
|
|
|
|
context[field] = attr()
|
|
|
|
else:
|
|
|
|
context[field] = attr
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2008-08-29 11:11:02 +02:00
|
|
|
def safe_template_context(ticket):
|
|
|
|
"""
|
|
|
|
Return a dictionary that can be used as a template context to render
|
2012-05-11 17:15:46 +02:00
|
|
|
comments and other details with ticket or queue parameters. Note that
|
2011-11-05 01:56:53 +01:00
|
|
|
we don't just provide the Ticket & Queue objects to the template as
|
2008-08-29 11:11:02 +02:00
|
|
|
they could reveal confidential information. Just imagine these two options:
|
|
|
|
* {{ ticket.queue.email_box_password }}
|
|
|
|
* {{ ticket.assigned_to.password }}
|
|
|
|
|
|
|
|
Ouch!
|
|
|
|
|
|
|
|
The downside to this is that if we make changes to the model, we will also
|
|
|
|
have to update this code. Perhaps we can find a better way in the future.
|
|
|
|
"""
|
|
|
|
|
|
|
|
context = {
|
2017-08-21 01:26:45 +02:00
|
|
|
'queue': queue_template_context(ticket.queue),
|
|
|
|
'ticket': ticket_template_context(ticket),
|
2016-10-23 22:09:17 +02:00
|
|
|
}
|
2008-08-29 11:11:02 +02:00
|
|
|
context['ticket']['queue'] = context['queue']
|
|
|
|
|
|
|
|
return context
|
2008-11-18 01:14:36 +01:00
|
|
|
|
2009-06-25 13:22:53 +02:00
|
|
|
|
|
|
|
def text_is_spam(text, request):
|
|
|
|
# Based on a blog post by 'sciyoshi':
|
|
|
|
# http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/
|
2011-11-05 01:56:53 +01:00
|
|
|
# This will return 'True' is the given text is deemed to be spam, or
|
2009-06-25 13:22:53 +02:00
|
|
|
# False if it is not spam. If it cannot be checked for some reason, we
|
|
|
|
# assume it isn't spam.
|
|
|
|
from django.contrib.sites.models import Site
|
2017-12-06 08:06:07 +01:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2009-06-25 13:22:53 +02:00
|
|
|
try:
|
2019-01-02 01:55:08 +01:00
|
|
|
from akismet import Akismet
|
2017-12-06 08:06:07 +01:00
|
|
|
except ImportError:
|
2009-06-25 13:22:53 +02:00
|
|
|
return False
|
2012-08-08 06:31:51 +02:00
|
|
|
try:
|
|
|
|
site = Site.objects.get_current()
|
2017-12-06 08:06:07 +01:00
|
|
|
except ImproperlyConfigured:
|
2012-08-08 06:31:51 +02:00
|
|
|
site = Site(domain='configure-django-sites.com')
|
2009-06-25 13:22:53 +02:00
|
|
|
|
2019-01-03 01:17:19 +01:00
|
|
|
# see https://akismet.readthedocs.io/en/latest/overview.html#using-akismet
|
2019-01-03 02:08:55 +01:00
|
|
|
|
2019-01-03 01:17:19 +01:00
|
|
|
apikey = None
|
2019-01-03 02:08:55 +01:00
|
|
|
|
2009-06-25 13:22:53 +02:00
|
|
|
if hasattr(settings, 'TYPEPAD_ANTISPAM_API_KEY'):
|
2019-01-03 01:17:19 +01:00
|
|
|
apikey = settings.TYPEPAD_ANTISPAM_API_KEY
|
2009-06-25 13:22:53 +02:00
|
|
|
ak.baseurl = 'api.antispam.typepad.com/1.1/'
|
2019-01-03 01:17:19 +01:00
|
|
|
elif hasattr(settings, 'PYTHON_AKISMET_API_KEY'):
|
|
|
|
# new env var expected by python-akismet package
|
|
|
|
apikey = settings.PYTHON_AKISMET_API_KEY
|
2009-06-25 13:22:53 +02:00
|
|
|
elif hasattr(settings, 'AKISMET_API_KEY'):
|
2019-01-03 01:17:19 +01:00
|
|
|
# deprecated, but kept for backward compatibility
|
|
|
|
apikey = settings.AKISMET_API_KEY
|
2009-06-25 13:22:53 +02:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-01-03 01:17:19 +01:00
|
|
|
ak = Akismet(
|
|
|
|
blog_url='http://%s/' % site.domain,
|
|
|
|
key=apikey,
|
|
|
|
)
|
|
|
|
|
2009-06-25 13:22:53 +02:00
|
|
|
if ak.verify_key():
|
|
|
|
ak_data = {
|
|
|
|
'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'),
|
|
|
|
'user_agent': request.META.get('HTTP_USER_AGENT', ''),
|
|
|
|
'referrer': request.META.get('HTTP_REFERER', ''),
|
|
|
|
'comment_type': 'comment',
|
|
|
|
'comment_author': '',
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:23:16 +01:00
|
|
|
return ak.comment_check(smart_text(text), data=ak_data)
|
2009-06-25 13:22:53 +02:00
|
|
|
|
|
|
|
return False
|
2016-11-10 17:23:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def process_attachments(followup, attached_files):
|
|
|
|
max_email_attachment_size = getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000)
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
for attached in attached_files:
|
2017-10-30 08:17:40 +01:00
|
|
|
|
2016-11-10 17:23:16 +01:00
|
|
|
if attached.size:
|
|
|
|
filename = smart_text(attached.name)
|
2019-03-07 21:58:04 +01:00
|
|
|
att = FollowUpAttachment(
|
2016-11-10 17:23:16 +01:00
|
|
|
followup=followup,
|
|
|
|
file=attached,
|
|
|
|
filename=filename,
|
|
|
|
mime_type=attached.content_type or
|
|
|
|
mimetypes.guess_type(filename, strict=False)[0] or
|
|
|
|
'application/octet-stream',
|
|
|
|
size=attached.size,
|
|
|
|
)
|
|
|
|
att.save()
|
|
|
|
|
|
|
|
if attached.size < max_email_attachment_size:
|
|
|
|
# Only files smaller than 512kb (or as defined in
|
|
|
|
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
|
|
|
|
attachments.append([filename, att.file])
|
|
|
|
|
|
|
|
return attachments
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
ORDER_COLUMN_CHOICES = Choices(
|
|
|
|
('0', 'id'),
|
|
|
|
('2', 'priority'),
|
|
|
|
('3', 'title'),
|
|
|
|
('4', 'queue'),
|
|
|
|
('5', 'status'),
|
|
|
|
('6', 'created'),
|
|
|
|
('7', 'due_date'),
|
|
|
|
('8', 'assigned_to')
|
|
|
|
)
|
|
|
|
|
2018-10-15 19:56:54 +02:00
|
|
|
|
2018-10-15 02:23:28 +02:00
|
|
|
def query_tickets_by_args(objects, order_by, **kwargs):
|
|
|
|
"""
|
|
|
|
This function takes in a list of ticket objects from the views and throws it
|
|
|
|
to the datatables on ticket_list.html. If a search string was entered, this
|
|
|
|
function filters existing dataset on search string and returns a filtered
|
|
|
|
filtered list. The `draw`, `length` etc parameters are for datatables to
|
|
|
|
display meta data on the table contents. The returning queryset is passed
|
|
|
|
to a Serializer called TicketSerializer in serializers.py.
|
|
|
|
"""
|
|
|
|
draw = int(kwargs.get('draw', None)[0])
|
|
|
|
length = int(kwargs.get('length', None)[0])
|
|
|
|
start = int(kwargs.get('start', None)[0])
|
|
|
|
search_value = kwargs.get('search[value]', None)[0]
|
|
|
|
order_column = kwargs.get('order[0][column]', None)[0]
|
|
|
|
order = kwargs.get('order[0][dir]', None)[0]
|
|
|
|
|
|
|
|
order_column = ORDER_COLUMN_CHOICES[order_column]
|
|
|
|
# django orm '-' -> desc
|
|
|
|
if order == 'desc':
|
|
|
|
order_column = '-' + order_column
|
|
|
|
|
|
|
|
queryset = objects.all().order_by(order_by)
|
|
|
|
total = queryset.count()
|
|
|
|
|
|
|
|
if search_value:
|
|
|
|
queryset = queryset.filter(Q(id__icontains=search_value) |
|
2018-10-15 19:56:54 +02:00
|
|
|
Q(priority__icontains=search_value) |
|
|
|
|
Q(title__icontains=search_value) |
|
|
|
|
Q(queue__title__icontains=search_value) |
|
|
|
|
Q(status__icontains=search_value) |
|
|
|
|
Q(created__icontains=search_value) |
|
|
|
|
Q(due_date__icontains=search_value) |
|
|
|
|
Q(assigned_to__email__icontains=search_value))
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
count = queryset.count()
|
|
|
|
queryset = queryset.order_by(order_column)[start:start + length]
|
|
|
|
return {
|
|
|
|
'items': queryset,
|
|
|
|
'count': count,
|
|
|
|
'total': total,
|
|
|
|
'draw': draw
|
2018-10-15 19:56:54 +02:00
|
|
|
}
|