SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit ba467356 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

change proof repo name to static property

parent 71bc8eb3
No related branches found
No related tags found
1 merge request!52New Production page fixes and oversights
......@@ -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)
# 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),
]
......@@ -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),
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment