diff --git a/itou/metabase/db.py b/itou/metabase/db.py index 86ef32affb..198eb7dbb9 100644 --- a/itou/metabase/db.py +++ b/itou/metabase/db.py @@ -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: @@ -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), }, ] ) @@ -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}:") diff --git a/itou/metabase/utils.py b/itou/metabase/utils.py index 1e4b3cc896..ddd08cc050 100644 --- a/itou/metabase/utils.py +++ b/itou/metabase/utils.py @@ -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 diff --git a/tests/eligibility/factories.py b/tests/eligibility/factories.py index cf653a5c31..67bfd8a869 100644 --- a/tests/eligibility/factories.py +++ b/tests/eligibility/factories.py @@ -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) ), )