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): def set_cc(apps, schema_editor):
# Set proper cc value
License = apps.get_model("epdb", "License") 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(): for license in License.objects.all():
pattern = r"/licenses/([^/]+)/4\.0" pattern = r"/licenses/([^/]+)/4\.0"
match = re.search(pattern, license.link) 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.image_link = f"https://licensebuttons.net/l/{cc_string}/4.0/88x31.png"
new_license.save() 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( license_lookup_qs = License.objects.values("cc_string").annotate(
lowest_pk=Min("id"), all_pks=ArrayAgg("id", order_by=("id",)) 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") Packages = apps.get_model("epdb", "Package")
for k, v in license_lookup.items(): 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]) 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]) v[1].remove(v[0])
# Delete Redundant License Objects # Delete redundant License objects
License.objects.filter(pk__in=v[1]).delete() License.objects.filter(pk__in=v[1]).delete()

View File

@ -1112,7 +1112,9 @@ def package(request, package_uuid):
PackageManager.update_permissions(current_user, current_package, grantee, max_perm) PackageManager.update_permissions(current_user, current_package, grantee, max_perm)
return redirect(current_package.url) return redirect(current_package.url)
elif cc_string is not None: elif cc_string is not None:
cc_string = cc_string.strip()
if cc_string == "no-license": # Reset the package's license if cc_string == "no-license": # Reset the package's license
current_package.license = None current_package.license = None
current_package.save() current_package.save()