Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pilotage: Utiliser une date qui prend en compte la timezone pour EligibilityDiagnosis.created_at #5338

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions itou/metabase/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.utils import timezone
from psycopg import sql

from itou.metabase.utils import chunked_queryset, compose, convert_boolean_to_int
from itou.metabase.utils import chunked_queryset, compose, convert_boolean_to_int, convert_datetime_to_local_date


class MetabaseDatabaseCursor:
Expand Down Expand Up @@ -249,7 +249,7 @@ def populate_table(table, batch_size, querysets=None, extra_object=None):
"comment": "Date de dernière mise à jour de Metabase",
# As metabase daily updates run typically every night after midnight, the last day with
# complete data is yesterday, not today.
"fn": lambda o: timezone.now() + timezone.timedelta(days=-1),
"fn": lambda o: timezone.localdate() + timezone.timedelta(days=-1),
},
]
)
Expand All @@ -260,6 +260,8 @@ def populate_table(table, batch_size, querysets=None, extra_object=None):
if c["type"] == "boolean":
c["type"] = "integer"
c["fn"] = compose(convert_boolean_to_int, c["fn"])
if c["type"] == "date":
c["fn"] = compose(convert_datetime_to_local_date, c["fn"])

print(f"Injecting {total_rows} rows with {len(table.columns)} columns into table {table_name}:")

Expand Down
12 changes: 12 additions & 0 deletions itou/metabase/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import datetime

from django.utils import timezone


def convert_boolean_to_int(b):
# True => 1, False => 0, None => None.
return None if b is None else int(b)


def convert_datetime_to_local_date(dt):
if isinstance(dt, datetime.datetime):
# Datetimes are stored in UTC.
return timezone.localdate(dt)
return dt


def compose(f, g):
# Compose two lambda methods.
# https://stackoverflow.com/questions/16739290/composing-functions-in-python
Expand Down
2 changes: 1 addition & 1 deletion tests/eligibility/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Params:
expired = factory.Trait(
expires_at=factory.LazyFunction(lambda: timezone.localdate() - datetime.timedelta(days=1)),
created_at=factory.LazyAttribute(
lambda obj: timezone.make_aware(faker.date_time(end_datetime=obj.expires_at))
lambda obj: faker.date_time(tzinfo=timezone.get_current_timezone(), end_datetime=obj.expires_at)
),
)

Expand Down
Loading