Initial general clean-up of stuff

This commit is contained in:
Alex Barcelo
2016-10-21 17:14:12 +02:00
parent 5e340338b4
commit 24d88be8d9
27 changed files with 827 additions and 710 deletions

View File

@ -6,7 +6,7 @@ django-helpdesk - A Django powered ticket tracker for small enterprise.
lib.py - Common functions (eg multipart e-mail)
"""
chart_colours = ('80C65A', '990066', 'FF9900', '3399CC', 'BBCCED', '3399CC', 'FFCC33')
import logging
try:
from base64 import urlsafe_b64encode as b64encode
@ -17,13 +17,20 @@ try:
except ImportError:
from base64 import decodestring as b64decode
import logging
logger = logging.getLogger('helpdesk')
from django.utils.encoding import smart_str
from django.db.models import Q
from django.utils.safestring import mark_safe
def send_templated_mail(template_name, email_context, recipients, sender=None, bcc=None, fail_silently=False, files=None):
logger = logging.getLogger('helpdesk')
def send_templated_mail(template_name,
email_context,
recipients,
sender=None,
bcc=None,
fail_silently=False,
files=None):
"""
send_templated_mail() is a warpper around Django's e-mail routines that
allows us to easily send multipart (text/plain & text/html) e-mails using
@ -83,16 +90,17 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b
try:
t = EmailTemplate.objects.get(template_name__iexact=template_name, locale__isnull=True)
except EmailTemplate.DoesNotExist:
logger.warning('template "%s" does not exist, no mail sent' %
template_name)
return # just ignore if template doesn't exist
logger.warning('template "%s" does not exist, no mail sent',
template_name)
return # just ignore if template doesn't exist
if not sender:
sender = settings.DEFAULT_FROM_EMAIL
footer_file = os.path.join('helpdesk', locale, 'email_text_footer.txt')
# get_template_from_string was removed in Django 1.8 http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
try:
from django.template import engines
template_func = engines['django'].from_string
@ -105,21 +113,22 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b
email_html_base_file = os.path.join('helpdesk', locale, 'email_html_base.html')
''' keep new lines in html emails '''
from django.utils.safestring import mark_safe
# keep new lines in html emails
if 'comment' in context:
html_txt = context['comment']
html_txt = html_txt.replace('\r\n', '<br>')
context['comment'] = mark_safe(html_txt)
# get_template_from_string was removed in Django 1.8 http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
html_part = template_func(
"{%% extends '%s' %%}{%% block title %%}%s{%% endblock %%}{%% block content %%}%s{%% endblock %%}" % (email_html_base_file, t.heading, t.html)
).render(context)
"{%% extends '%s' %%}{%% block title %%}"
"%s"
"{%% endblock %%}{%% block content %%}%s{%% endblock %%}" %
(email_html_base_file, t.heading, t.html)).render(context)
# get_template_from_string was removed in Django 1.8 http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
subject_part = template_func(
HELPDESK_EMAIL_SUBJECT_TEMPLATE % {
"subject": t.subject,
@ -129,13 +138,11 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b
if recipients.find(','):
recipients = recipients.split(',')
elif type(recipients) != list:
recipients = [recipients,]
recipients = [recipients, ]
msg = EmailMultiAlternatives( subject_part.replace('\n', '').replace('\r', ''),
text_part,
sender,
recipients,
bcc=bcc)
msg = EmailMultiAlternatives(
subject_part.replace('\n', '').replace('\r', ''),
text_part, sender, recipients, bcc=bcc)
msg.attach_alternative(html_part, "text/html")
if files:
@ -230,19 +237,19 @@ def safe_template_context(ticket):
}
queue = ticket.queue
for field in ( 'title', 'slug', 'email_address', 'from_address', 'locale'):
for field in ('title', 'slug', 'email_address', 'from_address', 'locale'):
attr = getattr(queue, field, None)
if callable(attr):
context['queue'][field] = attr()
else:
context['queue'][field] = attr
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'
):
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['ticket'][field] = '%s' % attr()
@ -278,10 +285,10 @@ def text_is_spam(text, request):
)
if hasattr(settings, 'TYPEPAD_ANTISPAM_API_KEY'):
ak.setAPIKey(key = settings.TYPEPAD_ANTISPAM_API_KEY)
ak.setAPIKey(key=settings.TYPEPAD_ANTISPAM_API_KEY)
ak.baseurl = 'api.antispam.typepad.com/1.1/'
elif hasattr(settings, 'AKISMET_API_KEY'):
ak.setAPIKey(key = settings.AKISMET_API_KEY)
ak.setAPIKey(key=settings.AKISMET_API_KEY)
else:
return False