This commit is contained in:
Tim Lorsbach
2025-11-11 19:57:50 +01:00
parent f7d88e4235
commit 91a52c254d
2 changed files with 8 additions and 5 deletions

View File

@ -8,10 +8,9 @@ from django.db.models import Min
def set_cc(apps, schema_editor):
# Set proper cc value
License = apps.get_model("epdb", "License")
# For all licenses extract cc_string
# For all existing licenses extract cc_string from link
for license in License.objects.all():
pattern = r"/licenses/([^/]+)/4\.0"
match = re.search(pattern, license.link)
@ -31,6 +30,8 @@ def set_cc(apps, schema_editor):
new_license.image_link = f"https://licensebuttons.net/l/{cc_string}/4.0/88x31.png"
new_license.save()
# As we might have existing Licenses representing the same License,
# get min pk and all pks as a list
license_lookup_qs = License.objects.values("cc_string").annotate(
lowest_pk=Min("id"), all_pks=ArrayAgg("id", order_by=("id",))
)
@ -42,11 +43,11 @@ def set_cc(apps, schema_editor):
Packages = apps.get_model("epdb", "Package")
for k, v in license_lookup.items():
# Set to min ID
# Set min pk to all packages pointing to any of the duplicates
Packages.objects.filter(pk__in=v[1]).update(license_id=v[0])
# Remove min ID
# remove the min pk from "other" pks as we use them for deletion
v[1].remove(v[0])
# Delete Redundant License Objects
# Delete redundant License objects
License.objects.filter(pk__in=v[1]).delete()