diff --git a/epdb/migrations/0011_auto_20251111_1413.py b/epdb/migrations/0011_auto_20251111_1413.py index fff5d79e..d0a3463a 100644 --- a/epdb/migrations/0011_auto_20251111_1413.py +++ b/epdb/migrations/0011_auto_20251111_1413.py @@ -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() diff --git a/epdb/views.py b/epdb/views.py index e2dca3fb..9af2ebe5 100644 --- a/epdb/views.py +++ b/epdb/views.py @@ -1112,7 +1112,9 @@ def package(request, package_uuid): PackageManager.update_permissions(current_user, current_package, grantee, max_perm) return redirect(current_package.url) + elif cc_string is not None: + cc_string = cc_string.strip() if cc_string == "no-license": # Reset the package's license current_package.license = None current_package.save()