Default to Jan 1st too on incomplete dates received from ActivityPub

This commit is contained in:
Adeodato Simó 2023-10-15 19:58:51 -03:00
parent c066d11eb1
commit c02306a66b
No known key found for this signature in database
GPG key ID: CDF447845F1A986F

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: