From ba46735653648cd9d94d56fe787329d5da4a39c4 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Thu, 6 Jul 2023 15:43:48 +0200 Subject: [PATCH] change proof repo name to static property --- scipost_django/production/admin.py | 14 +++- .../migrations/0007_auto_20230706_1502.py | 67 +++++++++++++++++++ scipost_django/production/models.py | 13 ++-- 3 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 scipost_django/production/migrations/0007_auto_20230706_1502.py diff --git a/scipost_django/production/admin.py b/scipost_django/production/admin.py index 645fc77ac..191991474 100644 --- a/scipost_django/production/admin.py +++ b/scipost_django/production/admin.py @@ -15,6 +15,8 @@ from .models import ( ProofsRepository, ) +from django.utils.html import format_html + def event_count(obj): return obj.events.count() @@ -102,11 +104,17 @@ class ProofsRepositoryAdmin(GuardedModelAdmin): search_fields = [ "stream__submission__author_list", "stream__submission__title", - "stream__submission__preprint__identifier_w_vn_nr", + "name", ] + list_filter = ["status"] - list_display = ["stream", "status", "git_path"] - readonly_fields = ["template_path", "git_path"] + list_display = ["name", "status", "gitlab_link"] + readonly_fields = ["stream", "template_path", "gitlab_link"] + + def gitlab_link(self, obj): + return format_html( + '<a href="{1}" target="_blank">{0}</a>', obj.git_path, obj.git_url + ) admin.site.register(ProofsRepository, ProofsRepositoryAdmin) diff --git a/scipost_django/production/migrations/0007_auto_20230706_1502.py b/scipost_django/production/migrations/0007_auto_20230706_1502.py new file mode 100644 index 000000000..347c2862f --- /dev/null +++ b/scipost_django/production/migrations/0007_auto_20230706_1502.py @@ -0,0 +1,67 @@ +# Generated by Django 3.2.18 on 2023-07-06 13:02 + +from django.db import migrations, models + + +def add_name_to_repos(apps, schema_editor): + from common.utils import latinise + from django.db.models.functions import Concat + from django.db.models import Value + + ProofsRepository = apps.get_model("production", "ProofsRepository") + Profile = apps.get_model("profiles", "Profile") + + def _clean_author_list(authors_str: str): + comma_separated = authors_str.replace(", and", ", ") + comma_separated = comma_separated.replace(" and ", ", ") + comma_separated = comma_separated.replace(", & ", ", ") + comma_separated = comma_separated.replace(" & ", ", ") + comma_separated = comma_separated.replace(";", ", ") + return [e.lstrip().rstrip() for e in comma_separated.split(",")] + + def _get_repo_name(stream) -> str: + """ + Return the name of the repository in the form of "id_lastname". + """ + # Get the last name of the first author by getting the first author string from the submission + first_author_str = _clean_author_list(stream.submission.author_list)[0] + first_author_profile = ( + Profile.objects.annotate( + full_name=Concat("first_name", Value(" "), "last_name") + ) + .filter(full_name=first_author_str) + .first() + ) + if first_author_profile is None: + first_author_last_name = first_author_str.split(" ")[-1] + else: + first_author_last_name = first_author_profile.last_name + + # Remove accents from the last name to avoid encoding issues + # and join multiple last names into one + first_author_last_name = latinise(first_author_last_name).strip() + first_author_last_name = first_author_last_name.replace(" ", "-") + + return "{preprint_id}_{last_name}".format( + preprint_id=stream.submission.preprint.identifier_w_vn_nr, + last_name=first_author_last_name, + ) + + for repo in ProofsRepository.objects.all(): + repo.name = _get_repo_name(repo.stream) + repo.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("production", "0006_proofsrepository"), + ] + + operations = [ + migrations.AddField( + model_name="proofsrepository", + name="name", + field=models.CharField(default="", max_length=128), + ), + migrations.RunPython(add_name_to_repos, reverse_code=migrations.RunPython.noop), + ] diff --git a/scipost_django/production/models.py b/scipost_django/production/models.py index 1840901a7..1e52ef4eb 100644 --- a/scipost_django/production/models.py +++ b/scipost_django/production/models.py @@ -295,14 +295,18 @@ class ProofsRepository(models.Model): choices=PROOFS_REPO_STATUSES, default=PROOFS_REPO_UNINITIALIZED, ) + name = models.CharField(max_length=128, default="") - @property - def name(self) -> str: + def __str__(self): + return self.name + + @staticmethod + def _get_repo_name(stream) -> str: """ Return the name of the repository in the form of "id_lastname". """ # Get the last name of the first author by getting the first author string from the submission - first_author_str = self.stream.submission.authors_as_list[0] + first_author_str = stream.submission.authors_as_list[0] first_author_profile = ( Profile.objects.annotate( full_name=Concat("first_name", Value(" "), "last_name") @@ -321,7 +325,7 @@ class ProofsRepository(models.Model): first_author_last_name = first_author_last_name.replace(" ", "-") return "{preprint_id}_{last_name}".format( - preprint_id=self.stream.submission.preprint.identifier_w_vn_nr, + preprint_id=stream.submission.preprint.identifier_w_vn_nr, last_name=first_author_last_name, ) @@ -414,6 +418,7 @@ def production_stream_create_proofs_repo(sender, instance, created, **kwargs): ProofsRepository.objects.create( stream=instance, status=ProofsRepository.PROOFS_REPO_UNINITIALIZED, + name=ProofsRepository._get_repo_name(instance), ) -- GitLab