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

Cria Processamento Para Logos dos Periódicos e Adiciona o campo url_logo na API de Journal #838

Merged
merged 16 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion institution/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ class BaseHistoryItem(CommonControlField):
final_date = models.DateField(_("Final Date"), null=True, blank=True)

panels = [
AutocompletePanel("institution"),
FieldPanel("initial_date"),
FieldPanel("final_date"),
]
Expand Down Expand Up @@ -432,6 +431,11 @@ def get_or_create(cls, institution, initial_date=None, final_date=None, user=Non
class Meta:
abstract = True

def __str__(self):
try:
return self.institution.institution.institution_identification.name
except AttributeError:
return ''

class BaseInstitution(CommonControlField):
institution = models.ForeignKey(
Expand Down
97 changes: 70 additions & 27 deletions journal/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from wagtail.models.sites import Site
from rest_framework import serializers

from core.api.v1.serializers import LanguageSerializer
Expand Down Expand Up @@ -67,6 +68,7 @@ class Meta:

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

class Meta:
model = models.Mission
fields = [
Expand All @@ -86,61 +88,69 @@ class JournalSerializer(serializers.ModelSerializer):
owner = serializers.SerializerMethodField()
acronym = serializers.SerializerMethodField()
scielo_journal = serializers.SerializerMethodField()
url_logo = serializers.SerializerMethodField()
mission = MissionSerializer(many=True, read_only=True)
issn_print = serializers.CharField(source="official.issn_print")
issn_electronic = serializers.CharField(source="official.issn_electronic")
next_journal_title = serializers.CharField(source="official.next_journal_title")
previous_journal_titles = serializers.CharField(source="official.previous_journal_titles")
other_titles = serializers.SerializerMethodField()
sponsor = serializers.SerializerMethodField()
email = serializers.SerializerMethodField()
copyright = serializers.SerializerMethodField()
next_journal_title = serializers.SerializerMethodField()
previous_journal_title = serializers.SerializerMethodField()

def get_institution_data(self, history):
data = []
for record in history.all():
if record.institution:
data.append({"name": record.institution.institution.institution_identification.name})
return data if data else None

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

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

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

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

def get_acronym(self, obj):
scielo_journal = obj.scielojournal_set.first()
return scielo_journal.journal_acron if scielo_journal else None

def get_scielo_journal(self, obj):
results = models.SciELOJournal.objects.filter(journal=obj).prefetch_related("journal_history")
results = models.SciELOJournal.objects.filter(journal=obj).prefetch_related(
"journal_history"
)
journals = []
for item in results:
journal_dict = {
'collection_acron': item.collection.acron3,
'issn_scielo': item.issn_scielo,
'journal_acron': item.journal_acron,
'journal_history': [
"collection_acron": item.collection.acron3,
"issn_scielo": item.issn_scielo,
"journal_acron": item.journal_acron,
"journal_history": [
{
'day': history.day,
'month': history.month,
'year': history.year,
'event_type': history.event_type,
'interruption_reason': history.interruption_reason,
} for history in item.journal_history.all()
"day": history.day,
"month": history.month,
"year": history.year,
"event_type": history.event_type,
"interruption_reason": history.interruption_reason,
}
for history in item.journal_history.all()
],
}
journals.append(journal_dict)

return journals

def get_url_logo(self, obj):
if obj.logo:
domain = Site.objects.get(is_default_site=True).hostname
domain = f"http://{domain}"
return f"{domain}{obj.logo.file.url}"
return None

def get_email(self, obj):
if obj.journal_email.all():
return [email.email for email in obj.journal_email.all()]
Expand All @@ -150,7 +160,39 @@ def get_other_titles(self, obj):
if obj.other_titles.all():
return [other_title.title for other_title in obj.other_titles.all()]
return None


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)
issn_print = journal_new_title.official.issn_print
issn_electronic = journal_new_title.official.issn_electronic
except models.Journal.DoesNotExist:
issn_print = None
issn_electronic = None
return {
"next_journal_title": obj.official.next_journal_title,
"issn_print": issn_print,
"issn_electronic": issn_electronic,
}

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
)
old_issn_print = old_journal.issn_print
old_issn_electronic = old_journal.issn_electronic
except models.OfficialJournal.DoesNotExist:
old_issn_print = None
old_issn_electronic = None

return {
"previous_journal_title": obj.official.previous_journal_titles,
"issn_print": old_issn_print,
"issn_electronic": old_issn_electronic,
}

class Meta:
model = models.Journal
Expand All @@ -160,7 +202,7 @@ class Meta:
"title",
"short_title",
"next_journal_title",
"previous_journal_titles",
"previous_journal_title",
"other_titles",
"acronym",
"issn_print",
Expand All @@ -174,6 +216,7 @@ class Meta:
"email",
"contact_address",
"text_language",
"url_logo",
"mission",
"sponsor",
"copyright",
Expand Down
18 changes: 16 additions & 2 deletions journal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,9 @@ class OwnerHistory(Orderable, BaseHistoryItem):
null=True,
)

panels = BaseHistoryItem.panels +[
AutocompletePanel("institution")
]

class PublisherHistory(Orderable, BaseHistoryItem):
journal = ParentalKey(
Expand All @@ -1010,6 +1013,9 @@ class PublisherHistory(Orderable, BaseHistoryItem):
null=True,
)

panels = BaseHistoryItem.panels +[
AutocompletePanel("institution")
]

class SponsorHistory(Orderable, BaseHistoryItem):
journal = ParentalKey(
Expand All @@ -1022,6 +1028,9 @@ class SponsorHistory(Orderable, BaseHistoryItem):
blank=True,
)

panels = BaseHistoryItem.panels +[
AutocompletePanel("institution")
]

class CopyrightHolderHistory(Orderable, BaseHistoryItem):
journal = ParentalKey(
Expand All @@ -1036,7 +1045,10 @@ class CopyrightHolderHistory(Orderable, BaseHistoryItem):
blank=True,
null=True,
)


panels = BaseHistoryItem.panels +[
AutocompletePanel("institution")
]

class JournalSocialNetwork(Orderable, SocialNetwork):
page = ParentalKey(
Expand Down Expand Up @@ -2558,7 +2570,9 @@ def create_or_update(
user,
):
try:
return cls.get(journal=journal, logo=logo)
obj = cls.get(journal=journal, logo=logo)
obj.save()
return obj
except cls.DoesNotExist:
return cls.create(journal=journal, logo=logo, user=user)

Expand Down
16 changes: 16 additions & 0 deletions journal/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,19 @@ def fetch_and_process_journal_logo(
"domain": domain,
},
)


@celery_app.task(bind=True)
def fetch_and_process_journal_logos_in_collection(self, collection_acron3=None, user_id=None,username=None):
if collection_acron3:
collection = Collection.objects.get(acron3=collection_acron3)
journals = Journal.objects.filter(scielojournal__collection=collection)
else:
journals = Journal.objects.all()

for journal in journals:
fetch_and_process_journal_logo(
journal_id=journal.id,
user_id=user_id,
username=username,
)
Loading