forked from enviPath/enviPy
...
This commit is contained in:
@ -20,7 +20,8 @@ from .models import (
|
|||||||
Setting,
|
Setting,
|
||||||
ExternalDatabase,
|
ExternalDatabase,
|
||||||
ExternalIdentifier,
|
ExternalIdentifier,
|
||||||
JobLog, License,
|
JobLog,
|
||||||
|
License,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -11,13 +11,16 @@ from epdb.models import (
|
|||||||
EnviFormer,
|
EnviFormer,
|
||||||
Permission,
|
Permission,
|
||||||
User,
|
User,
|
||||||
ExternalDatabase, License,
|
ExternalDatabase,
|
||||||
|
License,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
def add_arguments(self, parser):
|
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):
|
def create_users(self):
|
||||||
# Anonymous User
|
# Anonymous User
|
||||||
|
|||||||
@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from django.contrib.postgres.aggregates import ArrayAgg
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
|
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 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)
|
||||||
@ -14,8 +19,33 @@ def set_cc(apps, schema_editor):
|
|||||||
license.cc_string = match.group(1)
|
license.cc_string = match.group(1)
|
||||||
license.save()
|
license.save()
|
||||||
else:
|
else:
|
||||||
print(f"Could not find license for {license.link}")
|
raise ValueError(f"Could not find license for {license.link}")
|
||||||
raise ValueError()
|
|
||||||
|
# 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):
|
class Migration(migrations.Migration):
|
||||||
|
|||||||
Reference in New Issue
Block a user