From 914d2d67aca9c107310cf3ac2fcad663b2d56272 Mon Sep 17 00:00:00 2001 From: Ernesto Rico Schmidt <ernesto@helsinki.at> Date: Sun, 4 Oct 2020 21:35:55 -0400 Subject: [PATCH] Replace naive datetime with timezone aware. Also clean-up unused imports. --- program/admin.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/program/admin.py b/program/admin.py index f69de36f..e42ab83d 100644 --- a/program/admin.py +++ b/program/admin.py @@ -1,15 +1,14 @@ -# -*- 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}) -- GitLab