[scrobbles] Fix management command tag lookup
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import models
|
||||
from scrobbles.models import Scrobble
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Backfill tags on scrobbles from labels in their log data"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--commit",
|
||||
@ -13,32 +16,34 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
commit = options["commit"]
|
||||
|
||||
task_scrobbles = Scrobble.objects.filter(media_type=Scrobble.MediaType.TASK)
|
||||
scrobbles = Scrobble.objects.filter(
|
||||
models.Q(log__labels__isnull=False) & ~models.Q(log__labels=[])
|
||||
)
|
||||
|
||||
updated_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
for scrobble in task_scrobbles:
|
||||
logdata = scrobble.logdata
|
||||
if not logdata or not hasattr(logdata, "labels") or not logdata.labels:
|
||||
for scrobble in scrobbles:
|
||||
labels = scrobble.log.get("labels", [])
|
||||
if not labels:
|
||||
skipped_count += 1
|
||||
continue
|
||||
|
||||
existing_tags = list(scrobble.tags.all().values_list("name", flat=True))
|
||||
new_labels = [
|
||||
label for label in logdata.labels if label not in existing_tags
|
||||
]
|
||||
new_labels = [label for label in labels if label not in existing_tags]
|
||||
|
||||
if new_labels:
|
||||
updated_count += 1
|
||||
if commit:
|
||||
for label in new_labels:
|
||||
scrobble.tags.add(label)
|
||||
print(f"Updated scrobble {scrobble.id} with tags: {new_labels}")
|
||||
self.stdout.write(
|
||||
f"Updated scrobble {scrobble.id} with tags: {new_labels}"
|
||||
)
|
||||
else:
|
||||
print(
|
||||
self.stdout.write(
|
||||
f"[DRY RUN] Would update scrobble {scrobble.id} with tags: {new_labels}"
|
||||
)
|
||||
|
||||
print(f"\nUpdated {updated_count} scrobbles")
|
||||
print(f"Skipped {skipped_count} scrobbles (no labels in logdata)")
|
||||
self.stdout.write(f"\nUpdated {updated_count} scrobbles")
|
||||
self.stdout.write(f"Skipped {skipped_count} scrobbles (no new labels)")
|
||||
|
||||
Reference in New Issue
Block a user