diff --git a/ddcz/migrations/0006_dates_vol2.py b/ddcz/migrations/0006_dates_vol2.py new file mode 100644 index 0000000..6219733 --- /dev/null +++ b/ddcz/migrations/0006_dates_vol2.py @@ -0,0 +1,45 @@ +# Generated by Django 3.1.14 on 2024-12-29 19:13 + +from django.db import migrations +from datetime import datetime + +import sentry_sdk + + +def migrate_dates(apps, schema_editor): + Market = apps.get_model("ddcz", "Market") + + for market in Market.objects.all(): + if not market.published_varchar: + sentry_sdk.capture_message( + "Market entry without date", + level="error", + extras={ + "market_id": market.id, + "invalid_date": market.published_varchar, + }, + ) + + try: + parsed_date = datetime.strptime(market.published_varchar, "%d. %m. %Y") + except ValueError: + sentry_sdk.capture_message( + "Failed to parse date in Market record", + level="error", + extras={ + "market_id": market.id, + "invalid_date": market.published_varchar, + }, + ) + continue + + market.created = parsed_date + market.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("ddcz", "0005_market_dates_20241229_1747"), + ] + + operations = [migrations.RunPython(migrate_dates)] diff --git a/ddcz/models/used/social.py b/ddcz/models/used/social.py index de35d77..ac86b14 100644 --- a/ddcz/models/used/social.py +++ b/ddcz/models/used/social.py @@ -94,6 +94,9 @@ class Market(models.Model): created = models.DateTimeField(blank=True, null=True, verbose_name="Přidáno") + def __str__(self): + return f"{self.name} ({self.group}) z {self.published}" + @property def published(self): # Windows workaround as `%-d` is platform-specific