Skip to content

Commit

Permalink
Adiciona modelos e Admin para Journal TocSections. (#872)
Browse files Browse the repository at this point in the history
* Adiciona modelos relacionado ao Journal TocSections

* Adiciona Admin para TOCSection

* Remove comentarios

* Migracoes

* Altera nome de modelo para TocItem

* Adiciona paramentro toc_item

* - Melhorias em metodos de institution_history
- Adiciona toc_items ao payload de Journal

* migracoes
  • Loading branch information
samuelveigarangel authored Oct 9, 2024
1 parent f133652 commit b120b04
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 79 deletions.
13 changes: 13 additions & 0 deletions article/migrations/0014_merge_20241008_0012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 5.0.3 on 2024-10-08 00:12

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("article", "0013_alter_article_pid_v3"),
("article", "0013_article_article_license"),
]

operations = []
67 changes: 0 additions & 67 deletions article/sources/xmlsps.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,41 +232,6 @@ def get_or_create_fundings(xmltree, user, item):
)
return data

# data = []
# if fundings_group:
# for funding in fundings_group:
# funding_source = funding.get("funding-source") or []
# award_ids = funding.get("award-id") or []

# for fs in funding_source:
# sponsor = create_or_update_sponsor(
# funding_name=fs, user=user, item=item
# )
# for award_id in award_ids:
# try:
# obj = ArticleFunding.get_or_create(
# award_id=award_id,
# funding_source=sponsor,
# user=user,
# )
# if obj:
# data.append(obj)
# except Exception as e:
# exc_type, exc_value, exc_traceback = sys.exc_info()
# UnexpectedEvent.create(
# item=item,
# action="article.xmlsps.sources.get_or_create_fundings",
# exception=e,
# exc_traceback=exc_traceback,
# detail=dict(
# xmltree=f"{etree.tostring(xmltree)}",
# function="article.xmlsps.sources.get_or_create_fundings",
# funding_source=fs,
# award_id=award_id,
# ),
# )
# return data


def get_or_create_toc_sections(xmltree, user):
toc_sections = ArticleTocSections(xmltree=xmltree).sections
Expand Down Expand Up @@ -301,38 +266,6 @@ def set_license(xmltree, article):
article.article_license = license


# def get_licenses(xmltree, user):
# xml_licenses = ArticleLicense(xmltree=xmltree).licenses
# data = []
# license = None
# for xml_license in xml_licenses:

# if not license and xml_license.get("link"):
# url_data = LicenseStatement.parse_url(xml_license.get("link"))
# license_type = url_data.get("license_type")
# if license_type:
# license = License.create_or_update(
# user=user,
# license_type=license_type,
# )
# if license or xml_license.get("license_p"):
# try:
# license_p = xml_license.get("license_p")
# license_p = license_p["html_text"]
# except (ValueError, TypeError, KeyError):
# pass

# obj = LicenseStatement.create_or_update(
# user=user,
# url=xml_license.get("link"),
# language=Language.get_or_create(code2=xml_license.get("lang")),
# license_p=license_p,
# license=license,
# )
# data.append(obj)
# return data


def create_or_update_keywords(xmltree, user, item):
article_keywords = ArticleKeywords(xmltree=xmltree)
article_keywords.configure(tags_to_convert_to_html={'bold': 'b'})
Expand Down
35 changes: 25 additions & 10 deletions journal/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,23 @@ class JournalSerializer(serializers.ModelSerializer):
copyright = serializers.SerializerMethodField()
next_journal_title = serializers.SerializerMethodField()
previous_journal_title = serializers.SerializerMethodField()
toc_items = serializers.SerializerMethodField()

def get_publisher(self, obj):
if queryset := obj.publisher_history.all():
def get_institution_history(self, institution_history):
if queryset := institution_history.all():
return [{"name": str(item)} for item in queryset]

def get_publisher(self, obj):
return self.get_institution_history(obj.publisher_history)

def get_owner(self, obj):
if queryset := obj.owner_history.all():
return [{"name": str(item)} for item in queryset]
return self.get_institution_history(obj.owner_history)

def get_sponsor(self, obj):
if queryset := obj.sponsor_history.all():
return [{"name": str(item)} for item in queryset]
return self.get_institution_history(obj.sponsor_history)

def get_copyright(self, obj):
if queryset := obj.copyright_holder_history.all():
return [{"name": str(item)} for item in queryset]
return self.get_institution_history(obj.copyright_holder_history)

def get_acronym(self, obj):
scielo_journal = obj.scielojournal_set.first()
Expand Down Expand Up @@ -210,6 +211,18 @@ def get_previous_journal_title(self, obj):
"issn_electronic": old_issn_electronic,
}

def get_toc_items(self, obj):
if queryset := obj.journaltocsection_set.all():
data = []
for item in queryset:
for section in item.toc_items.all():
data.append({
"value": section.text,
"language": section.language.code2 if section.language else None
})
return data


class Meta:
model = models.Journal
fields = [
Expand All @@ -228,8 +241,11 @@ class Meta:
"journal_use_license",
"publisher",
"owner",
"sponsor",
"copyright",
"subject_descriptor",
"subject",
"toc_items",
"submission_online_url",
"email",
"contact_address",
Expand All @@ -238,6 +254,5 @@ class Meta:
"title_in_database",
"url_logo",
"mission",
"sponsor",
"copyright",

]
4 changes: 4 additions & 0 deletions journal/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_queryset(self):
issn_electronic = self.request.query_params.get("issn_electronic")
issnl = self.request.query_params.get("issnl")
thematic_areas = self.request.query_params.get("thematic_areas")
toc_item = self.request.query_params.get("toc_item")
from_date_created = self.request.query_params.get("from_date_created")
until_date_created = self.request.query_params.get("until_date_created")
from_date_updated = self.request.query_params.get("from_date_updated")
Expand All @@ -35,6 +36,7 @@ def get_queryset(self):
"issnl",
"title",
"thematic_areas",
"toc_item",
"page",
"from_date_created",
"until_date_created",
Expand All @@ -56,6 +58,8 @@ def get_queryset(self):
params['official__issnl'] = issnl
if title:
params['title'] = title
if toc_item:
params['journaltocsection__toc_items__text'] = toc_item
if thematic_areas:
params['subject__value__in'] = thematic_areas.split(",")
if from_date_created:
Expand Down
161 changes: 161 additions & 0 deletions journal/migrations/0028_journaltocsection_tocitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Generated by Django 5.0.3 on 2024-10-09 12:53

import django.db.models.deletion
import modelcluster.fields
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0004_language_core_langua_code2_4f7261_idx"),
("journal", "0027_journal_doi_prefix"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="JournalTocSection",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"sort_order",
models.IntegerField(blank=True, editable=False, null=True),
),
(
"created",
models.DateTimeField(
auto_now_add=True, verbose_name="Creation date"
),
),
(
"updated",
models.DateTimeField(
auto_now=True, verbose_name="Last update date"
),
),
(
"creator",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="%(class)s_creator",
to=settings.AUTH_USER_MODEL,
verbose_name="Creator",
),
),
(
"journal",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="journal.journal",
),
),
(
"updated_by",
models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="%(class)s_last_mod_user",
to=settings.AUTH_USER_MODEL,
verbose_name="Updater",
),
),
],
options={
"ordering": ["sort_order"],
"abstract": False,
},
),
migrations.CreateModel(
name="TocItem",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"sort_order",
models.IntegerField(blank=True, editable=False, null=True),
),
(
"created",
models.DateTimeField(
auto_now_add=True, verbose_name="Creation date"
),
),
(
"updated",
models.DateTimeField(
auto_now=True, verbose_name="Last update date"
),
),
("text", models.TextField(blank=True, null=True, verbose_name="Texto")),
(
"creator",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="%(class)s_creator",
to=settings.AUTH_USER_MODEL,
verbose_name="Creator",
),
),
(
"journal_toc_section",
modelcluster.fields.ParentalKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="toc_items",
to="journal.journaltocsection",
),
),
(
"language",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="core.language",
verbose_name="Idioma",
),
),
(
"updated_by",
models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="%(class)s_last_mod_user",
to=settings.AUTH_USER_MODEL,
verbose_name="Updater",
),
),
],
options={
"ordering": ["sort_order"],
"abstract": False,
},
),
]
36 changes: 35 additions & 1 deletion journal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2730,4 +2730,38 @@ class EditorialPolicy(Orderable, RichTextWithLanguage, CommonControlField):
class OpenScienceCompliance(Orderable, RichTextWithLanguage, CommonControlField):
journal = ParentalKey(
Journal, on_delete=models.SET_NULL, related_name="open_science_compliance", null=True
)
)


class JournalTocSection(Orderable, CommonControlField, ClusterableModel):
journal = models.ForeignKey(
Journal,
on_delete=models.SET_NULL,
blank=True,
null=True,
)
panels = [
AutocompletePanel("journal"),
InlinePanel("toc_items", label="Table of Contents items"),
]

@staticmethod
def autocomplete_custom_queryset_filter(search_term):
return JournalTocSection.objects.filter(toc_items__text__icontains=search_term).distinct()

def autocomplete_label(self):
return str(self)

def __str__(self):
related_values = self.toc_items.all()
value_list = [f"{toc_section.text} ({toc_section.language.code2})" for toc_section in related_values]
return f" | ".join(value_list)


class TocItem(Orderable, TextWithLang, CommonControlField):
journal_toc_section = ParentalKey(
JournalTocSection, on_delete=models.SET_NULL, related_name="toc_items", null=True
)

def __str__(self):
return f"{self.text} | {self.language}"
Loading

0 comments on commit b120b04

Please sign in to comment.