From b470e7acea0064cc8b2375626b3c2d9300ff002a Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 19 Apr 2024 10:17:38 -0400 Subject: [PATCH] [scrobbles] Fix a migration and how we save timezones --- .../migrations/0051_alter_scrobble_scrobble_log.py | 12 ++++++++++++ vrobbler/apps/scrobbles/models.py | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/vrobbler/apps/scrobbles/migrations/0051_alter_scrobble_scrobble_log.py b/vrobbler/apps/scrobbles/migrations/0051_alter_scrobble_scrobble_log.py index 0d52317..7d3b378 100644 --- a/vrobbler/apps/scrobbles/migrations/0051_alter_scrobble_scrobble_log.py +++ b/vrobbler/apps/scrobbles/migrations/0051_alter_scrobble_scrobble_log.py @@ -1,8 +1,19 @@ # Generated by Django 4.2.9 on 2024-04-05 14:39 +import json from django.db import migrations, models +def convert_log_to_json(apps, schema_editor): + Scrobble = apps.get_model("scrobbles", "Scrobble") + for s in Scrobble.objects.filter(scrobble_log__isnull=False): + try: + s.scrobble_log = json.loads(s.scrobble_log) + except json.JSONDecodeError: + s.scrobble_log = json.dumps({"migrated_data": s.scrobble_log}) + s.save(update_fields=["scrobble_log"]) + + class Migration(migrations.Migration): dependencies = [ @@ -10,6 +21,7 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython(convert_log_to_json), migrations.AlterField( model_name="scrobble", name="scrobble_log", diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index d54b6dc..f4c9132 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -559,7 +559,10 @@ class Scrobble(TimeStampedModel): self.uuid = uuid4() if not self.timezone: - self.timezone = self.user.profile.timezone + timezone = settings.TIME_ZONE + if self.user.profile: + timezone = self.user.profile.timezone + self.timzeone = timezone # Microseconds mess up Django's filtering, and we don't need be that specific if self.timestamp: