Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona Suporte ao Formato articlemeta na API de Journal #917

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions journal/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ class Meta:


class MissionSerializer(serializers.ModelSerializer):
language = serializers.CharField(source="language.code2")
language = serializers.SerializerMethodField()

class Meta:
model = models.Mission
fields = [
"rich_text",
"language",
]

def get_language(self, obj):
if obj.language is not None:
return obj.language.code2
return None

class JournalSerializer(serializers.ModelSerializer):
# Serializadores para campos de relacionamento, como 'official', devem corresponder aos campos do modelo.
Expand Down Expand Up @@ -181,7 +184,7 @@ def get_other_titles(self, obj):
def get_next_journal_title(self, obj):
if obj.official.next_journal_title:
try:
journal_new_title = models.Journal.objects.get(title__icontains=obj.official.next_journal_title)
journal_new_title = models.Journal.objects.get(title__exact=obj.official.next_journal_title)
issn_print = journal_new_title.official.issn_print
issn_electronic = journal_new_title.official.issn_electronic
except models.Journal.DoesNotExist:
Expand All @@ -197,7 +200,7 @@ def get_previous_journal_title(self, obj):
if obj.official.previous_journal_titles:
try:
old_journal = obj.official.old_title.get(
title__icontains=obj.official.previous_journal_titles
title__exact=obj.official.previous_journal_titles
)
old_issn_print = old_journal.issn_print
old_issn_electronic = old_journal.issn_electronic
Expand All @@ -221,7 +224,6 @@ def get_toc_items(self, obj):
"language": section.language.code2 if section.language else None
})
return data


class Meta:
model = models.Journal
Expand Down Expand Up @@ -254,5 +256,4 @@ class Meta:
"title_in_database",
"url_logo",
"mission",

]
4 changes: 4 additions & 0 deletions journal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ def __str__(self):
title = self.title or str(self.official)
return f"{title} ({collection_acronym}) | ({issns})"

def articlemeta_format(self):
from journal.sources.api_article_meta import get_article_meta
return get_article_meta(self)

base_form_class = CoreAdminModelForm


Expand Down
105 changes: 105 additions & 0 deletions journal/sources/api_article_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from journal.models import SciELOJournal, TitleInDatabase

def get_article_meta(obj):
result = {}
scielo_journal = SciELOJournal.objects.filter(journal=obj, collection__is_active=True).first()
publisher_exists = obj.publisher_history.exists()

def add_to_result(key, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelveigarangel

que tal criar as funções:

def add_value(key, value):
     add_to_result(key, [{"_": value}] if value else None)

Uso:

add_value("v5", obj.type_of_literature)

def add_items(key, items):

     add_to_result(key, [{"_": item} for item in items])

Uso:

add_items("v37", [sc.identifier for sc in secs_code.all()])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Muito bom!!

if value:
result[key] = value

add_to_result("v5", [{"_": obj.type_of_literature}] if obj.type_of_literature else None)
add_to_result("v6", [{"_": obj.treatment_level}] if obj.treatment_level else None)
add_to_result("v10", [{"_": obj.center_code}] if obj.center_code else None)
add_to_result("v20", [{"_": obj.national_code}] if obj.national_code else None)
add_to_result("v30", [{"_": obj.identification_number}] if obj.identification_number else None)

secs_code = TitleInDatabase.objects.filter(journal=obj, indexed_at__acronym__iexact="secs")
add_to_result("v37", [{"_": sc.identifier} for sc in secs_code] if secs_code.exists() else None)

add_to_result("v50", [{"_": scielo_journal.status}] if scielo_journal and scielo_journal.status else None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_to_result("v62", [{"_": ch.institution.institution.institution_identification.name}
for ch in obj.copyright_holder_history.all() if ch.institution] if obj.copyright_holder_history.exists() else None)
add_to_result("v66", [{"_": obj.ftp}] if obj.ftp else None)
add_to_result("v67", [{"_": obj.user_subscription}] if obj.user_subscription else None)
add_to_result("v68", [{"_": scielo_journal.journal_acron}] if scielo_journal and scielo_journal.journal_acron else None)
add_to_result("v69", [{"_": obj.journal_url}] if obj.journal_url else None)
add_to_result("v85", [{"_": obj.vocabulary.acronym}] if obj.vocabulary else None)
add_to_result("v100", [{"_": obj.title}] if obj.official and obj.official.title else None)
add_to_result("v110", [{"_": obj.subtitle}] if obj.subtitle else None)
add_to_result("v117", [{"_": obj.standard.code}] if obj.standard and obj.standard.code else None)
add_to_result("v130", [{"_": obj.section}] if obj.section else None)
add_to_result("v140", [{"_": sponsor.institution.institution.institution_identification.name}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelveigarangel sugiro criar uma função padrão para lidar com instituição, pois em breve faremos remodelagem e aí facilitaria a modificação em um só lugar. Por ex.:

def institution_name(obj):
    return obj.institution.institution.institution_identification.name

Copy link
Member

@robertatakenaka robertatakenaka Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_to_result("v140", institution_name(sponsor))

for sponsor in obj.sponsor_history.all() if sponsor.institution] if obj.sponsor_history.exists() else None)
add_to_result("v150", [{"_": obj.short_title}] if obj.short_title else None)
add_to_result("v151", [{"_": obj.official.iso_short_title}] if obj.official and obj.official.iso_short_title else None)

parallel_titles = [{"_": pt.text} for pt in obj.official.parallel_titles if pt.text]
add_to_result("v230", parallel_titles if parallel_titles else None)

add_to_result("v240", [{"_": other_title.title} for other_title in obj.other_titles.all()] if obj.other_titles.exists() else None)
add_to_result("v301", [{"_": obj.official.initial_year}] if obj.official and obj.official.initial_year else None)
add_to_result("v302", [{"_": obj.official.initial_volume}] if obj.official and obj.official.initial_volume else None)
add_to_result("v303", [{"_": obj.official.initial_number}] if obj.official and obj.official.initial_number else None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelveigarangel inserir as 3 linhas dentro do if seguinte

add_to_result("v304", [{"_": obj.official.terminate_year + obj.official.terminate_month}] if obj.official and obj.official.terminate_year else None)
add_to_result("v305", [{"_": obj.official.final_volume}] if obj.official and obj.official.final_volume else None)
add_to_result("v306", [{"_": obj.official.final_number}] if obj.official and obj.official.final_number else None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelveigarangel colocar dentro do if


if publisher_exists:
add_to_result("v310", [{"_": publisher.institution.institution.location.country.name}
for publisher in obj.publisher_history.all() if publisher.institution and publisher.institution.institution.location and publisher.institution.institution.location.country])
add_to_result("v320", [{"_": publisher.institution.institution.location.state.name}
for publisher in obj.publisher_history.all() if publisher.institution and publisher.institution.institution.location and publisher.institution.institution.location.state])
add_to_result("v480", [{"_": publisher.institution.institution.institution_identification.name}
for publisher in obj.publisher_history.all() if publisher.institution and publisher.institution.institution.location and publisher.institution.institution.location.country])
add_to_result("v490", [{"_": publisher.institution.institution.location.city.name}
for publisher in obj.publisher_history.all() if publisher.institution and publisher.institution.institution.location and publisher.institution.institution.location.city])

add_to_result("v330", [{"_": obj.level_of_publication}] if obj.level_of_publication else None)
add_to_result("v340", [{"_": obj.alphabet}] if obj.alphabet else None)
add_to_result("v350", [{"_": lang.code2} for lang in obj.text_language.all()] if obj.text_language.exists() else None)
add_to_result("v360", [{"_": lang.code2} for lang in obj.abstract_language.all()] if obj.abstract_language.exists() else None)
add_to_result("v380", [{"_": obj.frequency}] if obj.frequency else None)

medline_titles = TitleInDatabase.objects.filter(journal=obj, indexed_at__acronym__iexact="medline")
if medline_titles.exists():
v420 = [{"_": medline.identifier} for medline in medline_titles if medline.identifier]
if v420:
add_to_result("v420", v420)
add_to_result("v421", [{"_": medline.title} for medline in medline_titles])

add_to_result("v430", [{"_": obj.classification}] if obj.classification else None)

issns = []
if obj.official and obj.official.issn_print:
issns.append({"_": obj.official.issn_print, "t": "PRINT"})
if obj.official and obj.official.issn_electronic:
issns.append({"_": obj.official.issn_electronic, "t": "ONLIN"})
add_to_result("v435", issns if issns else None)

add_to_result("v440", [{"_": descriptor.value} for descriptor in obj.subject_descriptor.all()] if obj.subject_descriptor.exists() else None)
add_to_result("v441", [{"_": subject.value} for subject in obj.subject.all()] if obj.subject.exists() else None)
add_to_result("v450", [{"_": index.name} for index in obj.indexed_at.all()] if obj.indexed_at.exists() else None)

add_to_result("v550", [{"_": obj.has_supplement}] if obj.has_supplement else None)
add_to_result("v560", [{"_": obj.is_supplement}] if obj.is_supplement else None)

if obj.official and obj.official.old_title:
add_to_result("v610", [{"_": old_title.title} for old_title in obj.official.old_title.all()])
if obj.official and obj.official.new_title:
add_to_result("v710", [{"_": obj.official.new_title.title}])

add_to_result("v900", [{"_": annotation.notes} for annotation in obj.annotation.all()] if obj.annotation.exists() else None)
add_to_result("v901", [{"l": mission.language.code2, "_": mission.get_text_pure}
for mission in obj.mission.all() if mission.language and mission.get_text_pure] if obj.mission else None)

result["v940"] = [{"_": obj.created}]
result["v941"] = [{"_": obj.updated}]

if scielo_journal:
result["collection"] = scielo_journal.collection.acron3

# Ordena o dicionário por chave
result = dict(sorted(result.items()))
return result