Merge pull request #3038 from dato/parsed_date_defaults

Stable defaults for incomplete parsed dates
This commit is contained in:
Jascha Ezra Urbach 2023-10-17 20:08:04 +02:00 committed by GitHub
commit 3ade72b90d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 24 deletions

View file

@ -1,5 +1,6 @@
""" activitypub-aware django model fields """ """ activitypub-aware django model fields """
from dataclasses import MISSING from dataclasses import MISSING
from datetime import datetime
import re import re
from uuid import uuid4 from uuid import uuid4
from urllib.parse import urljoin from urllib.parse import urljoin
@ -534,8 +535,10 @@ class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
return value.isoformat() return value.isoformat()
def field_from_activity(self, value, allow_external_connections=True): def field_from_activity(self, value, allow_external_connections=True):
missing_fields = datetime(1970, 1, 1) # "2022-10" => "2022-10-01"
try: try:
date_value = dateutil.parser.parse(value) # TODO(dato): investigate `ignoretz=True` wrt bookwyrm#3028.
date_value = dateutil.parser.parse(value, default=missing_fields)
try: try:
return timezone.make_aware(date_value) return timezone.make_aware(date_value)
except ValueError: except ValueError:

View file

@ -1,4 +1,5 @@
""" track progress of goodreads imports """ """ track progress of goodreads imports """
from datetime import datetime
import math import math
import re import re
import dateutil.parser import dateutil.parser
@ -259,38 +260,30 @@ class ImportItem(models.Model):
except ValueError: except ValueError:
return None return None
def _parse_datefield(self, field, /):
if not (date := self.normalized_data.get(field)):
return None
defaults = datetime(1970, 1, 1) # "2022-10" => "2022-10-01"
parsed = dateutil.parser.parse(date, default=defaults)
# Keep timezone if import already had one, else use default.
return parsed if timezone.is_aware(parsed) else timezone.make_aware(parsed)
@property @property
def date_added(self): def date_added(self):
"""when the book was added to this dataset""" """when the book was added to this dataset"""
if self.normalized_data.get("date_added"): return self._parse_datefield("date_added")
parsed_date_added = dateutil.parser.parse(
self.normalized_data.get("date_added")
)
if timezone.is_aware(parsed_date_added):
# Keep timezone if import already had one
return parsed_date_added
return timezone.make_aware(parsed_date_added)
return None
@property @property
def date_started(self): def date_started(self):
"""when the book was started""" """when the book was started"""
if self.normalized_data.get("date_started"): return self._parse_datefield("date_started")
return timezone.make_aware(
dateutil.parser.parse(self.normalized_data.get("date_started"))
)
return None
@property @property
def date_read(self): def date_read(self):
"""the date a book was completed""" """the date a book was completed"""
if self.normalized_data.get("date_finished"): return self._parse_datefield("date_finished")
return timezone.make_aware(
dateutil.parser.parse(self.normalized_data.get("date_finished"))
)
return None
@property @property
def reads(self): def reads(self):

View file

@ -7,6 +7,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.utils import timezone
from bookwyrm import forms, models, views from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html from bookwyrm.tests.validate_html import validate_html
@ -128,7 +129,7 @@ class ImportViews(TestCase):
def test_get_average_import_time_with_data(self): def test_get_average_import_time_with_data(self):
"""Now, with data""" """Now, with data"""
now = datetime.datetime.now() now = timezone.now()
two_hours_ago = now - datetime.timedelta(hours=2) two_hours_ago = now - datetime.timedelta(hours=2)
four_hours_ago = now - datetime.timedelta(hours=4) four_hours_ago = now - datetime.timedelta(hours=4)
models.ImportJob.objects.create( models.ImportJob.objects.create(
@ -152,7 +153,7 @@ class ImportViews(TestCase):
def test_get_average_import_time_ignore_stopped(self): def test_get_average_import_time_ignore_stopped(self):
"""Don't include stopped, do include no status""" """Don't include stopped, do include no status"""
now = datetime.datetime.now() now = timezone.now()
two_hours_ago = now - datetime.timedelta(hours=2) two_hours_ago = now - datetime.timedelta(hours=2)
four_hours_ago = now - datetime.timedelta(hours=4) four_hours_ago = now - datetime.timedelta(hours=4)
models.ImportJob.objects.create( models.ImportJob.objects.create(