Skip to content
Snippets Groups Projects
Unverified Commit 914d2d67 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Replace naive datetime with timezone aware.

Also clean-up unused imports.
parent 9a83db63
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
from datetime import date, datetime, timedelta
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render
from django.conf import settings
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from .models import Language, Type, MusicFocus, Category, Topic, FundingCategory, Host, Note, RRule, Schedule, Show, TimeSlot
from .forms import MusicFocusForm
from datetime import date, datetime, time, timedelta
from .models import Language, Type, MusicFocus, Category, Topic, FundingCategory, Host, Note, RRule, Schedule, Show, TimeSlot
class ActivityFilter(admin.SimpleListFilter):
......@@ -24,21 +23,21 @@ class ActivityFilter(admin.SimpleListFilter):
def queryset(self, request, queryset):
if self.parameter_name == 'has_timeslots': # active/inactive Schedules
if self.value() == 'yes':
return queryset.filter(until__gt=datetime.now()).distinct()
return queryset.filter(until__gt=timezone.now()).distinct()
if self.value() == 'no':
return queryset.filter(until__lt=datetime.now()).distinct()
return queryset.filter(until__lt=timezone.now()).distinct()
if self.parameter_name == 'has_schedules_timeslots': # active/inactive Shows
if self.value() == 'yes':
return queryset.filter(schedules__until__gt=datetime.now()).distinct()
return queryset.filter(schedules__until__gt=timezone.now()).distinct()
if self.value() == 'no':
return queryset.filter(schedules__until__lt=datetime.now()).distinct()
return queryset.filter(schedules__until__lt=timezone.now()).distinct()
if self.parameter_name == 'has_shows_schedules_timeslots': # active/inactive Hosts
if self.value() == 'yes':
return queryset.filter(shows__schedules__until__gt=datetime.now()).distinct()
return queryset.filter(shows__schedules__until__gt=timezone.now()).distinct()
if self.value() == 'no':
return queryset.filter(shows__schedules__until__lt=datetime.now()).distinct()
return queryset.filter(shows__schedules__until__lt=timezone.now()).distinct()
class ActiveSchedulesFilter(ActivityFilter):
......@@ -126,8 +125,8 @@ class NoteAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
four_weeks_ago = datetime.now() - timedelta(weeks=4)
in_twelve_weeks = datetime.now() + timedelta(weeks=12)
four_weeks_ago = timezone.now() - timedelta(weeks=4)
in_twelve_weeks = timezone.now() + timedelta(weeks=12)
if db_field.name == 'timeslot':
# Adding/Editing a note: load timeslots of the user's shows into the dropdown
......@@ -399,8 +398,8 @@ class ShowAdmin(admin.ModelAdmin):
tstart = datetime.strptime(request.POST.get('ps_save_tstart'), '%H:%M').time()
tend = datetime.strptime(request.POST.get('ps_save_tend'), '%H:%M').time()
dstart = datetime.strptime(request.POST.get('ps_save_dstart'), '%Y-%m-%d').date()
if dstart < datetime.today().date(): # Create or delete upcoming timeslots only
dstart = datetime.today().date()
if dstart < timezone.now().date(): # Create or delete upcoming timeslots only
dstart = timezone.now().date()
until = datetime.strptime(request.POST.get('ps_save_until'), '%Y-%m-%d').date()
is_repetition = request.POST.get('ps_save_is_repetition')
automation_id = int(request.POST.get('ps_save_automation_id')) if request.POST.get('ps_save_automation_id') != 'None' else 0
......@@ -486,7 +485,7 @@ class ShowAdmin(admin.ModelAdmin):
if ts != None:
start_end = ts.split(' - ')
# Only create upcoming timeslots
if datetime.strptime(start_end[0], "%Y-%m-%d %H:%M:%S") > datetime.today():
if datetime.strptime(start_end[0], "%Y-%m-%d %H:%M:%S") > timezone.now():
timeslot_created = TimeSlot.objects.create(schedule=new_schedule, is_repetition=new_schedule.is_repetition, start=start_end[0], end=start_end[1])
# Link a note to the new timeslot
......@@ -584,7 +583,7 @@ class ShowAdmin(admin.ModelAdmin):
'num_inputs': len(self.timeslots),
'step': self.step,
'max_steps': self.max_steps,
'now': datetime.now(),
'now': timezone.now(),
'num_collisions': self.num_collisions})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment