From 7738bba2ab4800689c2374fd402842d02b4770ec Mon Sep 17 00:00:00 2001 From: Jonathan Barratt Date: Mon, 21 Nov 2016 09:12:19 +0700 Subject: [PATCH] remove special-case imports for legacy versions standardise use, or avoidance, of timezone, template from_string, and Context --- helpdesk/forms.py | 9 +++------ helpdesk/lib.py | 21 +++----------------- helpdesk/management/commands/get_email.py | 10 ++-------- helpdesk/models.py | 6 +----- helpdesk/views/staff.py | 24 ++++------------------- 5 files changed, 13 insertions(+), 57 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index f8a89d92..7222a2cd 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -6,19 +6,16 @@ django-helpdesk - A Django powered ticket tracker for small enterprise. forms.py - Definitions of newforms-based forms for creating and maintaining tickets. """ + + from django.core.exceptions import ObjectDoesNotExist - from django.utils.six import StringIO - from django import forms from django.forms import extras from django.conf import settings from django.utils.translation import ugettext_lazy as _ from django.contrib.auth import get_user_model -try: - from django.utils import timezone -except ImportError: - from datetime import datetime as timezone +from django.utils import timezone from helpdesk.lib import send_templated_mail, safe_template_context from helpdesk.models import (Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, diff --git a/helpdesk/lib.py b/helpdesk/lib.py index 364abc1e..3bf24ff9 100644 --- a/helpdesk/lib.py +++ b/helpdesk/lib.py @@ -57,21 +57,15 @@ def send_templated_mail(template_name, along with the File objects to be read. files can be blank. """ - from django import VERSION from django.conf import settings from django.core.mail import EmailMultiAlternatives - from django.template import loader, Context from helpdesk.models import EmailTemplate from helpdesk.settings import HELPDESK_EMAIL_SUBJECT_TEMPLATE, \ HELPDESK_EMAIL_FALLBACK_LOCALE import os - # RemovedInDjango110Warning: render() must be called with a dict, not a Context. - if VERSION >= (1, 8): - context = email_context - else: - context = Context(email_context) + context = email_context if hasattr(context['queue'], 'locale'): locale = getattr(context['queue'], 'locale', '') @@ -99,13 +93,8 @@ def send_templated_mail(template_name, 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 - try: - from django.template import engines - template_func = engines['django'].from_string - except ImportError: # occurs in django < 1.8 - template_func = loader.get_template_from_string + from django.template import engines + template_func = engines['django'].from_string text_part = template_func( "%s{%% include '%s' %%}" % (t.plain_text, footer_file) @@ -119,16 +108,12 @@ def send_templated_mail(template_name, html_txt = html_txt.replace('\r\n', '
') 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 html_part = template_func( "{%% 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 subject_part = template_func( HELPDESK_EMAIL_SUBJECT_TEMPLATE % { "subject": t.subject, diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 51c326a6..833a1358 100644 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -27,19 +27,13 @@ from optparse import make_option from email_reply_parser import EmailReplyParser -from django import VERSION from django.core.files.base import ContentFile from django.core.management.base import BaseCommand from django.db.models import Q from django.utils.translation import ugettext as _ -from django.utils import six +from django.utils import six, timezone + from helpdesk import settings - -try: - from django.utils import timezone -except ImportError: - from datetime import datetime as timezone - from helpdesk.lib import send_templated_mail, safe_template_context from helpdesk.models import Queue, Ticket, FollowUp, Attachment, IgnoreEmail diff --git a/helpdesk/models.py b/helpdesk/models.py index 9e02da2b..0c3bd93b 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -13,14 +13,10 @@ from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.conf import settings +from django.utils import timezone from django.utils.translation import ugettext_lazy as _, ugettext from django.utils.encoding import python_2_unicode_compatible -try: - from django.utils import timezone -except ImportError: - from datetime import datetime as timezone - @python_2_unicode_compatible class Queue(models.Model): diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 9c606c9c..510efdb1 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -9,7 +9,6 @@ views/staff.py - The bulk of the application - provides most business logic and from __future__ import unicode_literals from datetime import datetime, timedelta -from django import VERSION from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.decorators import user_passes_test @@ -24,11 +23,7 @@ from django.utils.dates import MONTHS_3 from django.utils.translation import ugettext as _ from django.utils.html import escape from django import forms - -try: - from django.utils import timezone -except ImportError: - from datetime import datetime as timezone +from django.utils import timezone from helpdesk.forms import ( TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, @@ -407,25 +402,14 @@ def update_ticket(request, ticket_id, public=False): # We need to allow the 'ticket' and 'queue' contexts to be applied to the # comment. - from django.template import loader, Context context = safe_template_context(ticket) + # this line sometimes creates problems if code is sent as a comment. # if comment contains some django code, like "why does {% if bla %} crash", # then the following line will give us a crash, since django expects {% if %} # to be closed with an {% endif %} tag. - - # 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 - except ImportError: # occurs in django < 1.8 - template_func = loader.get_template_from_string - - # RemovedInDjango110Warning: render() must be called with a dict, not a Context. - if VERSION < (1, 8): - context = Context(context) - + from django.template import engines + template_func = engines['django'].from_string comment = template_func(comment).render(context) if owner is -1 and ticket.assigned_to: