[fix] Use timezone aware datetimes

This commit is contained in:
Alexis Métaireau 2023-11-24 00:34:24 +01:00
parent 1e9406c54f
commit 35dc1acb07
2 changed files with 9 additions and 7 deletions

View file

@ -1,17 +1,18 @@
import json import json
import socket import socket
from datetime import date, timedelta from datetime import date, datetime, timedelta
import pytest import pytest
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user, get_user_model from django.contrib.auth import get_user, get_user_model
from django.urls import reverse
from django.test import RequestFactory from django.test import RequestFactory
from django.urls import reverse
from django.utils.timezone import make_aware
from umap import VERSION from umap import VERSION
from umap.views import validate_url from umap.views import validate_url
from .base import UserFactory, MapFactory from .base import MapFactory, UserFactory
User = get_user_model() User = get_user_model()
@ -186,9 +187,9 @@ def test_stats_empty(client):
@pytest.mark.django_db @pytest.mark.django_db
def test_stats_basic(client, map, datalayer, user2): def test_stats_basic(client, map, datalayer, user2):
map.owner.last_login = date.today() map.owner.last_login = make_aware(datetime.now())
map.owner.save() map.owner.save()
user2.last_login = date.today() - timedelta(days=8) user2.last_login = make_aware(datetime.now()) - timedelta(days=8)
user2.save() user2.save()
response = client.get(reverse("stats")) response = client.get(reverse("stats"))
assert json.loads(response.content.decode()) == { assert json.loads(response.content.decode()) == {

View file

@ -3,7 +3,7 @@ import mimetypes
import os import os
import re import re
import socket import socket
from datetime import date, timedelta from datetime import datetime, timedelta
from http.client import InvalidURL from http.client import InvalidURL
from pathlib import Path from pathlib import Path
from urllib.error import URLError from urllib.error import URLError
@ -33,6 +33,7 @@ from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils.encoding import smart_bytes from django.utils.encoding import smart_bytes
from django.utils.http import http_date from django.utils.http import http_date
from django.utils.timezone import make_aware
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.utils.translation import to_locale from django.utils.translation import to_locale
from django.views.decorators.cache import cache_control from django.views.decorators.cache import cache_control
@ -1019,7 +1020,7 @@ class PictogramJSONList(ListView):
def stats(request): def stats(request):
last_week = date.today() - timedelta(days=7) last_week = make_aware(datetime.now()) - timedelta(days=7)
return simple_json_response( return simple_json_response(
**{ **{
"version": VERSION, "version": VERSION,