Skip to content

Commit

Permalink
Fix AttributeError 'zoneinfo.ZoneInfo' object has no attribute 'local…
Browse files Browse the repository at this point in the history
…ize'

Pytz is being deprecated and since Django 4.0 it now returns zoneinfo objects instead of pytz objects. This fix replaces the usage of .localize (which exists on pytz objects) with a .replace call (which is the way to do the same action on regular datetime objects now).

Docs Reference: https://docs.djangoproject.com/en/5.1/releases/4.0/#zoneinfo-default-timezone-implementation

Fixes crashes on personal tab transactions pages, as well as anywhere else in the code where the .localize function was used.
  • Loading branch information
Kurocon committed Sep 23, 2024
1 parent 3a2e191 commit 8874100
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
5 changes: 2 additions & 3 deletions amelie/personal_tab/debt_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ def save_cookie_corner_instructions(rows, batch):
timezone_amsterdam = timezone.get_default_timezone()
execution_date = batch.execution_date

debt_collection_datetime = timezone_amsterdam.localize(datetime.datetime.combine(execution_date,
datetime.time(0, 0)))
debt_collection_datetime = datetime.datetime.combine(execution_date, datetime.time(0, 0)).replace(tzinfo=timezone_amsterdam)

for row in rows:
person = row['person']
Expand Down Expand Up @@ -310,7 +309,7 @@ def process_reversal(reversal, actor):
instruction = reversal.instruction
person = instruction.authorization.person

reversal_datetime = timezone_amsterdam.localize(datetime.datetime.combine(reversal.date, datetime.time(0, 0)))
reversal_datetime = datetime.datetime.combine(reversal.date, datetime.time(0, 0)).replace(tzinfo=timezone_amsterdam)
with translation.override(person.preferred_language):
description = _('Reversal of direct withdrawal {date}').format(date=_date(instruction.batch.execution_date, "j F Y"))

Expand Down
36 changes: 18 additions & 18 deletions amelie/personal_tab/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,28 @@ def generate_overview_new(request, person, date_from=None, date_to=None):

if overview_type == 'total':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, 1, 1))
return datetime.datetime(t.year, 1, 1).replace(tzinfo=localtz)

def _datetime_end(t):
return localtz.localize(datetime.datetime(t.year + 1, 1, 1))
return datetime.datetime(t.year + 1, 1, 1).replace(tzinfo=localtz)

elif overview_type == 'year':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, t.month, 1))
return datetime.datetime(t.year, t.month, 1).replace(tzinfo=localtz)

def _datetime_end(t):
return localtz.localize(
datetime.datetime(t.year, t.month + 1, 1)) if t.month < 12 else localtz.localize(
datetime.datetime(t.year + 1, 1, 1)
)
if t.month < 12:
return datetime.datetime(t.year, t.month + 1, 1).replace(tzinfo=localtz)
else:
return datetime.datetime(t.year + 1, 1, 1).replace(tzinfo=localtz)

elif overview_type == 'month':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, t.month, t.day))
return datetime.datetime(t.year, t.month, t.day).replace(tzinfo=localtz)

def _datetime_end(t):
t2 = t + datetime.timedelta(days=1)
return localtz.localize(datetime.datetime(t2.year, t2.month, t2.day))
return datetime.datetime(t2.year, t2.month, t2.day).replace(tzinfo=localtz)
else:
raise ValueError

Expand Down Expand Up @@ -345,28 +345,28 @@ def generate_overview_exam_cookie_credit(request, person, date_from=None, date_t

if overview_type == 'total':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, 1, 1))
return datetime.datetime(t.year, 1, 1).replace(tzinfo=localtz)

def _datetime_end(t):
return localtz.localize(datetime.datetime(t.year + 1, 1, 1))
return datetime.datetime(t.year + 1, 1, 1).replace(tzinfo=localtz)

elif overview_type == 'year':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, t.month, 1))
return datetime.datetime(t.year, t.month, 1).replace(tzinfo=localtz)

def _datetime_end(t):
return localtz.localize(
datetime.datetime(t.year, t.month + 1, 1)) if t.month < 12 else localtz.localize(
datetime.datetime(t.year + 1, 1, 1)
)
if t.month < 12:
return datetime.datetime(t.year, t.month + 1, 1).replace(tzinfo=localtz)
else:
return datetime.datetime(t.year + 1, 1, 1).replace(tzinfo=localtz)

elif overview_type == 'month':
def _datetime_start(t):
return localtz.localize(datetime.datetime(t.year, t.month, t.day))
return datetime.datetime(t.year, t.month, t.day).replace(tzinfo=localtz)

def _datetime_end(t):
t2 = t + datetime.timedelta(days=1)
return localtz.localize(datetime.datetime(t2.year, t2.month, t2.day))
return datetime.datetime(t2.year, t2.month, t2.day).replace(tzinfo=localtz)

else:
raise ValueError
Expand Down
9 changes: 5 additions & 4 deletions amelie/statistics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def calculate():
committee_count = Committee.objects.active().count()
committee_category_count = CommitteeCategory.objects.all().count()

begin_study_year = timezone.get_default_timezone().localize(datetime(year=current_academic_year_strict(),
month=9, day=1))
begin_study_year = datetime(year=current_academic_year_strict(), month=9, day=1)\
.replace(tzinfo=timezone.get_default_timezone())

current_activity_count = Activity.objects.filter(begin__gt=begin_study_year, begin__lt=timezone.now())\
.count()
Expand All @@ -44,7 +44,8 @@ def calculate():
- CookieCornerTransaction.objects.all().order_by('date')[0].date).days

snacks_average_count = int(snacks_count / days)
begin_today = timezone.get_default_timezone().localize(datetime.combine(date.today(), time(0, 0)))
begin_today = datetime.combine(date.today(), time(0, 0))\
.replace(tzinfo=timezone.get_default_timezone())
snacks_today_count = CookieCornerTransaction.objects.filter(date__gt=begin_today).count()

transactions = CookieCornerTransaction.objects.values('article').annotate(count=Count('article'))\
Expand Down Expand Up @@ -79,7 +80,7 @@ def hits(request):

for values in Hits.objects.distinct().values('page'):
page = values['page']

pages.append({
"name": page,
"today": Hits.objects.filter(page=page, date_start=date.today())
Expand Down

0 comments on commit 8874100

Please sign in to comment.