This commit is contained in:
Tim Lorsbach
2025-11-11 15:47:03 +01:00
parent 2a3e418f5b
commit 1e5b8f5a63
3 changed files with 39 additions and 5 deletions

View File

@ -20,7 +20,8 @@ from .models import (
Setting,
ExternalDatabase,
ExternalIdentifier,
JobLog, License,
JobLog,
License,
)

View File

@ -11,13 +11,16 @@ from epdb.models import (
EnviFormer,
Permission,
User,
ExternalDatabase, License,
ExternalDatabase,
License,
)
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("-ol", "--only-licenses", action="store_true", help="Only create licenses.")
parser.add_argument(
"-ol", "--only-licenses", action="store_true", help="Only create licenses."
)
def create_users(self):
# Anonymous User

View File

@ -2,11 +2,16 @@
import re
from django.contrib.postgres.aggregates import ArrayAgg
from django.db import migrations
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 license in License.objects.all():
pattern = r"/licenses/([^/]+)/4\.0"
match = re.search(pattern, license.link)
@ -14,8 +19,33 @@ def set_cc(apps, schema_editor):
license.cc_string = match.group(1)
license.save()
else:
print(f"Could not find license for {license.link}")
raise ValueError()
raise ValueError(f"Could not find license for {license.link}")
# Ensure we have all licenses
cc_strings = ["by", "by-nc", "by-nc-nd", "by-nc-sa", "by-nd", "by-sa"]
for cc_string in cc_strings:
if not License.objects.filter(cc_string=cc_string).exists():
new_license = License()
new_license.cc_string = cc_string
new_license.link = f"https://creativecommons.org/licenses/{cc_string}/4.0/"
new_license.image_link = f"https://licensebuttons.net/l/{cc_string}/4.0/88x31.png"
new_license.save()
license_lookup_qs = License.objects.values("cc_string").annotate(
lowest_pk=Min("id"), all_pks=ArrayAgg("id", order_by=("id",))
)
license_lookup = {
row["cc_string"]: (row["lowest_pk"], row["all_pks"]) for row in license_lookup_qs
}
Packages = apps.get_model("epdb", "Package")
for k, v in license_lookup.items():
# Set to min ID
print(Packages.objects.filter(pk__in=v[1]).update(license_id=v[0]))
# Delete Redundant License Objects
print(License.objects.filter(pk__in=v[1]).delete())
class Migration(migrations.Migration):