Fix datetime/timezone handling in Django <1.4; update management commands to be timezone-aware

This commit is contained in:
Ross Poulton 2013-01-23 11:35:18 +11:00
parent 30bce8c27e
commit 53273e3794
6 changed files with 35 additions and 13 deletions

View File

@ -14,7 +14,10 @@ from django.forms import extras
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
try:
from django.utils import timezone 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.lib import send_templated_mail, safe_template_context
from helpdesk.models import Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, CustomField, TicketCustomFieldValue, TicketDependency from helpdesk.models import Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, CustomField, TicketCustomFieldValue, TicketDependency

View File

@ -9,7 +9,7 @@ scripts/create_escalation_exclusion.py - Easy way to routinely add particular
escalation should take place. escalation should take place.
""" """
from datetime import datetime, timedelta, date from datetime import timedelta, date
import getopt import getopt
from optparse import make_option from optparse import make_option
import sys import sys

View File

@ -8,7 +8,7 @@ scripts/escalate_tickets.py - Easy way to escalate tickets based on their age,
designed to be run from Cron or similar. designed to be run from Cron or similar.
""" """
from datetime import datetime, timedelta, date from datetime import timedelta, date
import getopt import getopt
from optparse import make_option from optparse import make_option
import sys import sys
@ -17,6 +17,11 @@ from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q from django.db.models import Q
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
try:
from django.utils import timezone
except ImportError:
from datetime import datetime as timezone
from helpdesk.models import Queue, Ticket, FollowUp, EscalationExclusion, TicketChange from helpdesk.models import Queue, Ticket, FollowUp, EscalationExclusion, TicketChange
from helpdesk.lib import send_templated_mail, safe_template_context from helpdesk.lib import send_templated_mail, safe_template_context
@ -95,7 +100,7 @@ def escalate_tickets(queues, verbose):
| Q(last_escalation__isnull=True, created__lte=req_last_escl_date) | Q(last_escalation__isnull=True, created__lte=req_last_escl_date)
): ):
t.last_escalation = datetime.now() t.last_escalation = timezone.now()
t.priority -= 1 t.priority -= 1
t.save() t.save()
@ -138,7 +143,7 @@ def escalate_tickets(queues, verbose):
f = FollowUp( f = FollowUp(
ticket = t, ticket = t,
title = 'Ticket Escalated', title = 'Ticket Escalated',
date=datetime.now(), date=timezone.now(),
public=True, public=True,
comment=_('Ticket escalated after %s days' % q.escalate_days), comment=_('Ticket escalated after %s days' % q.escalate_days),
) )

View File

@ -16,7 +16,7 @@ import mimetypes
import poplib import poplib
import re import re
from datetime import datetime, timedelta from datetime import timedelta
from email.header import decode_header from email.header import decode_header
from email.Utils import parseaddr, collapse_rfc2231_value from email.Utils import parseaddr, collapse_rfc2231_value
from optparse import make_option from optparse import make_option
@ -27,6 +27,11 @@ from django.db.models import Q
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from helpdesk import settings 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.lib import send_templated_mail, safe_template_context
from helpdesk.models import Queue, Ticket, FollowUp, Attachment, IgnoreEmail from helpdesk.models import Queue, Ticket, FollowUp, Attachment, IgnoreEmail
@ -56,7 +61,7 @@ def process_email(quiet=False):
allow_email_submission=True): allow_email_submission=True):
if not q.email_box_last_check: if not q.email_box_last_check:
q.email_box_last_check = datetime.now()-timedelta(minutes=30) q.email_box_last_check = timezone.now()-timedelta(minutes=30)
if not q.email_box_interval: if not q.email_box_interval:
q.email_box_interval = 0 q.email_box_interval = 0
@ -64,12 +69,12 @@ def process_email(quiet=False):
queue_time_delta = timedelta(minutes=q.email_box_interval) queue_time_delta = timedelta(minutes=q.email_box_interval)
if (q.email_box_last_check + queue_time_delta) > datetime.now(): if (q.email_box_last_check + queue_time_delta) > timezone.now():
continue continue
process_queue(q, quiet=quiet) process_queue(q, quiet=quiet)
q.email_box_last_check = datetime.now() q.email_box_last_check = timezone.now()
q.save() q.save()
@ -215,7 +220,7 @@ def ticket_from_message(message, queue, quiet):
'type': 'text/html', 'type': 'text/html',
}) })
now = datetime.now() now = timezone.now()
if ticket: if ticket:
try: try:
@ -254,7 +259,7 @@ def ticket_from_message(message, queue, quiet):
f = FollowUp( f = FollowUp(
ticket = t, ticket = t,
title = _('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}), title = _('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),
date = datetime.now(), date = timezone.now(),
public = True, public = True,
comment = body, comment = body,
) )

View File

@ -11,7 +11,12 @@ from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext from django.utils.translation import ugettext_lazy as _, ugettext
try:
from django.utils import timezone from django.utils import timezone
except importError:
from datetime import datetime as timezone
from helpdesk.settings import HAS_TAG_SUPPORT from helpdesk.settings import HAS_TAG_SUPPORT
if HAS_TAG_SUPPORT: if HAS_TAG_SUPPORT:

View File

@ -24,9 +24,13 @@ from django.shortcuts import render_to_response, get_object_or_404
from django.template import loader, Context, RequestContext from django.template import loader, Context, RequestContext
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.html import escape from django.utils.html import escape
from django.utils import timezone
from django import forms from django import forms
try:
from django.utils import timezone
except importError:
from datetime import datetime as timezone
from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm
from helpdesk.lib import send_templated_mail, query_to_dict, apply_query, safe_template_context from helpdesk.lib import send_templated_mail, query_to_dict, apply_query, safe_template_context
from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency