From 341a40b9ae226ee6a9985978a828f6116b92feb5 Mon Sep 17 00:00:00 2001 From: Ross Poulton Date: Wed, 23 Jan 2013 10:59:12 +1100 Subject: [PATCH] Make datetime handline offset-aware. Now requires pytz. Fixes #142. --- helpdesk/forms.py | 10 +++++----- helpdesk/models.py | 15 +++++++-------- helpdesk/views/api.py | 8 +++----- helpdesk/views/public.py | 2 -- helpdesk/views/staff.py | 15 ++++++++------- setup.py | 2 +- 6 files changed, 24 insertions(+), 28 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index f3c33dc7..04e43605 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -7,7 +7,6 @@ forms.py - Definitions of newforms-based forms for creating and maintaining tickets. """ -from datetime import datetime from StringIO import StringIO from django import forms @@ -15,6 +14,7 @@ from django.forms import extras from django.conf import settings from django.contrib.auth.models import User from django.utils.translation import ugettext as _ +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, CustomField, TicketCustomFieldValue, TicketDependency @@ -243,7 +243,7 @@ class TicketForm(forms.Form): t = Ticket( title = self.cleaned_data['title'], submitter_email = self.cleaned_data['submitter_email'], - created = datetime.now(), + created = timezone.now(), status = Ticket.OPEN_STATUS, queue = q, description = self.cleaned_data['body'], @@ -273,7 +273,7 @@ class TicketForm(forms.Form): f = FollowUp( ticket = t, title = _('Ticket Opened'), - date = datetime.now(), + date = timezone.now(), public = True, comment = self.cleaned_data['body'], user = user, @@ -462,7 +462,7 @@ class PublicTicketForm(forms.Form): t = Ticket( title = self.cleaned_data['title'], submitter_email = self.cleaned_data['submitter_email'], - created = datetime.now(), + created = timezone.now(), status = Ticket.OPEN_STATUS, queue = q, description = self.cleaned_data['body'], @@ -484,7 +484,7 @@ class PublicTicketForm(forms.Form): f = FollowUp( ticket = t, title = _('Ticket Opened Via Web'), - date = datetime.now(), + date = timezone.now(), public = True, comment = self.cleaned_data['body'], ) diff --git a/helpdesk/models.py b/helpdesk/models.py index 0252830c..ceb67d9d 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -7,12 +7,11 @@ models.py - Model (and hence database) definitions. This is the core of the helpdesk structure. """ -from datetime import datetime - from django.contrib.auth.models import User from django.db import models from django.conf import settings from django.utils.translation import ugettext_lazy as _, ugettext +from django.utils import timezone from helpdesk.settings import HAS_TAG_SUPPORT if HAS_TAG_SUPPORT: @@ -451,12 +450,12 @@ class Ticket(models.Model): def save(self, *args, **kwargs): if not self.id: # This is a new ticket as no ID yet exists. - self.created = datetime.now() + self.created = timezone.now() if not self.priority: self.priority = 3 - self.modified = datetime.now() + self.modified = timezone.now() super(Ticket, self).save(*args, **kwargs) @@ -489,7 +488,7 @@ class FollowUp(models.Model): date = models.DateTimeField( _('Date'), - default = datetime.now() + default = timezone.now() ) title = models.CharField( @@ -541,7 +540,7 @@ class FollowUp(models.Model): def save(self, *args, **kwargs): t = self.ticket - t.modified = datetime.now() + t.modified = timezone.now() t.save() super(FollowUp, self).save(*args, **kwargs) @@ -857,7 +856,7 @@ class KBItem(models.Model): def save(self, *args, **kwargs): if not self.last_updated: - self.last_updated = datetime.now() + self.last_updated = timezone.now() return super(KBItem, self).save(*args, **kwargs) def _score(self): @@ -1038,7 +1037,7 @@ class IgnoreEmail(models.Model): def save(self, *args, **kwargs): if not self.date: - self.date = datetime.now() + self.date = timezone.now() return super(IgnoreEmail, self).save(*args, **kwargs) def test(self, email): diff --git a/helpdesk/views/api.py b/helpdesk/views/api.py index 5a88f3da..6a1bbcd3 100644 --- a/helpdesk/views/api.py +++ b/helpdesk/views/api.py @@ -11,15 +11,13 @@ The API documentation can be accessed by visiting http://helpdesk/api/help/ through templates/helpdesk/help_api.html. """ -from datetime import datetime - from django import forms from django.contrib.auth import authenticate from django.contrib.auth.models import User from django.http import HttpResponse from django.shortcuts import render_to_response from django.template import loader, Context -from django.utils import simplejson +from django.utils import simplejson, timezone from django.views.decorators.csrf import csrf_exempt from helpdesk.forms import TicketForm @@ -182,7 +180,7 @@ class API: f = FollowUp( ticket=ticket, - date=datetime.now(), + date=timezone.now(), comment=message, user=self.request.user, title='Comment Added', @@ -257,7 +255,7 @@ class API: f = FollowUp( ticket=ticket, - date=datetime.now(), + date=timezone.now(), comment=resolution, user=self.request.user, title='Resolved', diff --git a/helpdesk/views/public.py b/helpdesk/views/public.py index 8423d14b..e164f059 100644 --- a/helpdesk/views/public.py +++ b/helpdesk/views/public.py @@ -7,8 +7,6 @@ views/public.py - All public facing views, eg non-staff (no authentication required) views. """ -from datetime import datetime - from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect, Http404, HttpResponse from django.shortcuts import render_to_response, get_object_or_404 diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 12b301b6..27a87b83 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -24,6 +24,7 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import loader, Context, RequestContext from django.utils.translation import ugettext as _ from django.utils.html import escape +from django.utils import timezone from django import forms from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm @@ -339,7 +340,7 @@ def update_ticket(request, ticket_id, public=False): if ticket.due_date: due_date = ticket.due_date else: - due_date = datetime.now() + due_date = timezone.now() due_date = due_date.replace(due_date_year, due_date_month, due_date_day) tags = request.POST.get('tags', '') @@ -369,7 +370,7 @@ def update_ticket(request, ticket_id, public=False): if owner is -1 and ticket.assigned_to: owner = ticket.assigned_to.id - f = FollowUp(ticket=ticket, date=datetime.now(), comment=comment) + f = FollowUp(ticket=ticket, date=timezone.now(), comment=comment) if request.user.is_staff or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE: f.user = request.user @@ -596,22 +597,22 @@ def mass_update(request): if action == 'assign' and t.assigned_to != user: t.assigned_to = user t.save() - f = FollowUp(ticket=t, date=datetime.now(), title=_('Assigned to %(username)s in bulk update' % {'username': user.username}), public=True, user=request.user) + f = FollowUp(ticket=t, date=timezone.now(), title=_('Assigned to %(username)s in bulk update' % {'username': user.username}), public=True, user=request.user) f.save() elif action == 'unassign' and t.assigned_to is not None: t.assigned_to = None t.save() - f = FollowUp(ticket=t, date=datetime.now(), title=_('Unassigned in bulk update'), public=True, user=request.user) + f = FollowUp(ticket=t, date=timezone.now(), title=_('Unassigned in bulk update'), public=True, user=request.user) f.save() elif action == 'close' and t.status != Ticket.CLOSED_STATUS: t.status = Ticket.CLOSED_STATUS t.save() - f = FollowUp(ticket=t, date=datetime.now(), title=_('Closed in bulk update'), public=False, user=request.user, new_status=Ticket.CLOSED_STATUS) + f = FollowUp(ticket=t, date=timezone.now(), title=_('Closed in bulk update'), public=False, user=request.user, new_status=Ticket.CLOSED_STATUS) f.save() elif action == 'close_public' and t.status != Ticket.CLOSED_STATUS: t.status = Ticket.CLOSED_STATUS t.save() - f = FollowUp(ticket=t, date=datetime.now(), title=_('Closed in bulk update'), public=True, user=request.user, new_status=Ticket.CLOSED_STATUS) + f = FollowUp(ticket=t, date=timezone.now(), title=_('Closed in bulk update'), public=True, user=request.user, new_status=Ticket.CLOSED_STATUS) f.save() # Send email to Submitter, Owner, Queue CC context = safe_template_context(t) @@ -953,7 +954,7 @@ def hold_ticket(request, ticket_id, unhold=False): ticket = ticket, user = request.user, title = title, - date = datetime.now(), + date = timezone.now(), public = True, ) f.save() diff --git a/setup.py b/setup.py index 327e51b2..3c087182 100644 --- a/setup.py +++ b/setup.py @@ -133,6 +133,6 @@ setup( package_data=find_package_data("helpdesk", only_in_packages=False), include_package_data=True, zip_safe=False, - install_requires=['setuptools'], + install_requires=['setuptools', 'pytz'], )