Skip to content

Commit

Permalink
Merge branch 'robertatakenaka-last_issue'
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelveigarangel committed Mar 12, 2024
2 parents 3bf8724 + 1ee33e6 commit ab677ee
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 69 deletions.
115 changes: 114 additions & 1 deletion opac/webapp/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ou outras camadas superiores, evitando assim que as camadas superiores
acessem diretamente a camada inferior de modelos.
"""
import logging
import io
import re
from collections import OrderedDict
Expand All @@ -31,6 +32,7 @@
Pages,
PressRelease,
Sponsor,
LastIssue,
)
from scieloh5m5 import h5m5
from slugify import slugify
Expand Down Expand Up @@ -429,6 +431,9 @@ def get_journal_generator_for_csv(
extension="xls",
):
def format_csv_row(list_type, journal):
if not journal.last_issue or journal.last_issue.type not in ("volume_issue", "regular"):
set_last_issue_and_issue_count(journal)

if not journal.last_issue:
last_issue_volume = ""
last_issue_number = ""
Expand Down Expand Up @@ -738,7 +743,7 @@ def get_issues_for_grid_by_jid(jid, **kwargs):

# o primiero item da lista é o último número.
# condicional para verificar se issues contém itens
last_issue = issues[0] if issues else None
last_issue = issues[0].journal.last_issue

return {
"ahead": issue_ahead, # ahead of print
Expand All @@ -749,6 +754,114 @@ def get_issues_for_grid_by_jid(jid, **kwargs):
}


def get_issue_nav_bar_data(journal=None, issue=None):
"""
Retorna quanto à navegação os itens anterior e posterior,
a um dado issue, e o último issue regular de um periódico.
Caso issue não é informado, considera-se que o issue em questão
é o último issue regular odendo ter como item posterior
um suplemento, um número especial, um ahead ou nenhum item
"""
if issue:
journal = issue.journal
last_issue = None

elif journal:
if not journal.last_issue or journal.last_issue.type not in ("volume_issue", "regular") or journal.last_issue.type not in ("volume_issue", "regular"):
set_last_issue_and_issue_count(journal)

last_issue = get_issue_by_iid(journal.last_issue.iid)

item = issue or last_issue

if item.type == "ahead" or item.number == "ahead":
previous = Issue.objects(
journal=journal,
number__ne="ahead",
).order_by("-year", "-order").first()
next_ = None
else:
try:
previous = Issue.objects(
journal=journal,
year__lte=item.year,
order__lte=item.order,
number__ne="ahead",
).order_by("-year", "-order")[1]
except IndexError:
previous = None

try:
next_ = Issue.objects(
journal=journal,
year__gte=item.year,
order__gte=item.order,
number__ne="ahead",
).order_by("year", "order")[1]
except IndexError:
# aop
next_ = Issue.objects(
journal=journal,
number="ahead",
).order_by("-year", "-order").first()

return {
"previous_item": previous,
"next_item": next_,
"issue": issue,
"last_issue": last_issue,
}


def set_last_issue_and_issue_count(journal):
"""
O último issue tem que ser um issue regular, não pode ser aop, nem suppl, nem especial
"""
try:
order_by = ["-year", "-order"]
issues = Issue.objects(
journal=journal,
type__in=["regular", "volume_issue"],
is_public=True,
)
journal.issue_count = issues.count()
journal.save()
except Exception as e:
logging.exception(f"Unable to set_last_issue_and_issue_count for {journal.id}: {e} {type(e)}")

try:
last_issue = issues.order_by(*order_by).first()

journal.last_issue = LastIssue(
volume=last_issue.volume,
number=last_issue.number,
year=last_issue.year,
label=last_issue.label,
type=last_issue.type,
suppl_text=last_issue.suppl_text,
start_month=last_issue.start_month,
end_month=last_issue.end_month,
iid=last_issue.iid,
url_segment=last_issue.url_segment,
)
journal.save()
except Exception as e:
logging.exception(f"Unable to set_last_issue_and_issue_count for {journal.id}: {e} {type(e)}")
return journal


def journal_last_issues():
for j in Journal.objects.filter(last_issue=None):
set_last_issue_and_issue_count(j)
if j.last_issue and j.last_issue.url_segment:
yield {"journal": j.jid, "last_issue": j.last_issue.url_segment}

for j in Journal.objects.filter(last_issue__type__nin=["regular", "volume_issue"]):
set_last_issue_and_issue_count(j)
if j.last_issue and j.last_issue.url_segment:
yield {"journal": j.jid, "last_issue": j.last_issue.url_segment}


def get_issue_by_iid(iid, **kwargs):
"""
Retorna um número considerando os parâmetros ``iid`` e ``kwargs``.
Expand Down
74 changes: 56 additions & 18 deletions opac/webapp/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,14 @@ def collection_list_feed():
feed.add("Nenhum periódico encontrado", url=request.url, updated=datetime.now())

for journal in journals.items:
issues = controllers.get_issues_by_jid(journal.jid, is_public=True)
last_issue = issues[0] if issues else None
if (
not journal.last_issue
or journal.last_issue.type not in ("volume_issue", "regular")
or not journal.last_issue.url_segment
):
controllers.set_last_issue_and_issue_count(journal)
# Note: journal.last_issue (is instance of LastIssue, not Issue)
last_issue = journal.last_issue

articles = []
if last_issue:
Expand Down Expand Up @@ -535,7 +541,12 @@ def journal_detail(url_seg):
if not journal.is_public:
abort(404, JOURNAL_UNPUBLISH + _(journal.unpublish_reason))

utils.fix_journal_last_issue(journal)
if (
not journal.last_issue
or journal.last_issue.type not in ("volume_issue", "regular")
or not journal.last_issue.url_segment
):
controllers.set_last_issue_and_issue_count(journal)

# todo: ajustar para que seja só noticias relacionadas ao periódico
language = session.get("lang", get_locale())
Expand All @@ -561,6 +572,7 @@ def journal_detail(url_seg):
sections = []
recent_articles = []

# Note: journal.last_issue (is instance of LastIssue, not Issue)
latest_issue = journal.last_issue

if latest_issue:
Expand Down Expand Up @@ -593,7 +605,7 @@ def journal_detail(url_seg):
"news": news,
"journal_metrics": journal_metrics,
}

context.update(controllers.get_issue_nav_bar_data(journal=journal))
return render_template("journal/detail.html", **context)


Expand All @@ -608,10 +620,22 @@ def journal_feed(url_seg):
if not journal.is_public:
abort(404, JOURNAL_UNPUBLISH + _(journal.unpublish_reason))

issues = controllers.get_issues_by_jid(journal.jid, is_public=True)
last_issue = issues[0] if issues else None
articles = controllers.get_articles_by_iid(last_issue.iid, is_public=True)
if (
not journal.last_issue
or journal.last_issue.type not in ("volume_issue", "regular")
or not journal.last_issue.url_segment
):
controllers.set_last_issue_and_issue_count(journal)

# Note: journal.last_issue (is instance of LastIssue, not Issue)
last_issue = journal.last_issue

if last_issue:
articles = controllers.get_articles_by_iid(last_issue.iid, is_public=True)
if articles:
last_issue = articles[0].issue
else:
articles = []
feed = AtomFeed(
journal.title,
feed_url=request.url,
Expand Down Expand Up @@ -660,7 +684,15 @@ def about_journal(url_seg):
if not journal.is_public:
abort(404, JOURNAL_UNPUBLISH + _(journal.unpublish_reason))

latest_issue = utils.fix_journal_last_issue(journal)
if (
not journal.last_issue
or journal.last_issue.type not in ("volume_issue", "regular")
or not journal.last_issue.url_segment
):
controllers.set_last_issue_and_issue_count(journal)

# Note: journal.last_issue (is instance of LastIssue, not Issue)
latest_issue = journal.last_issue

if latest_issue:
latest_issue_legend = descriptive_short_format(
Expand Down Expand Up @@ -691,6 +723,7 @@ def about_journal(url_seg):
if page.updated_at:
context["page_updated_at"] = page.updated_at

context.update(controllers.get_issue_nav_bar_data(journal))
return render_template("journal/about.html", **context)


Expand Down Expand Up @@ -863,7 +896,15 @@ def issue_grid(url_seg):

# A ordenação padrão da função ``get_issues_by_jid``: "-year", "-volume", "-order"
issues_data = controllers.get_issues_for_grid_by_jid(journal.id, is_public=True)
latest_issue = issues_data["last_issue"]
if (
not journal.last_issue
or journal.last_issue.type not in ("volume_issue", "regular")
or not journal.last_issue.url_segment
):
controllers.set_last_issue_and_issue_count(journal)

# Note: journal.last_issue (is instance of LastIssue, not Issue)
latest_issue = journal.last_issue
if latest_issue:
latest_issue_legend = descriptive_short_format(
title=journal.title,
Expand All @@ -879,7 +920,7 @@ def issue_grid(url_seg):

context = {
"journal": journal,
"last_issue": issues_data["last_issue"],
"last_issue": latest_issue,
"latest_issue_legend": latest_issue_legend,
"volume_issue": issues_data["volume_issue"],
"ahead": issues_data["ahead"],
Expand All @@ -888,7 +929,7 @@ def issue_grid(url_seg):
STUDY_AREAS.get(study_area.upper()) for study_area in journal.study_areas
],
}

context.update(controllers.get_issue_nav_bar_data(journal=journal))
return render_template("issue/grid.html", **context)


Expand Down Expand Up @@ -934,9 +975,6 @@ def issue_toc(url_seg, url_seg_issue):
if not journal.is_public:
abort(404, JOURNAL_UNPUBLISH + _(journal.unpublish_reason))

# completa url_segment do last_issue
utils.fix_journal_last_issue(journal)

# goto_next_or_previous_issue (redireciona)
goto_url = goto_next_or_previous_issue(
issue, request.args.get("goto", None, type=str)
Expand Down Expand Up @@ -994,6 +1032,7 @@ def issue_toc(url_seg, url_seg_issue):
],
"last_issue": journal.last_issue,
}
context.update(controllers.get_issue_nav_bar_data(journal=journal, issue=issue))
return render_template("issue/toc.html", **context)


Expand Down Expand Up @@ -1053,9 +1092,6 @@ def aop_toc(url_seg):
journal = aop_issues[0].journal
if not journal.is_public:
abort(404, JOURNAL_UNPUBLISH + _(journal.unpublish_reason))

utils.fix_journal_last_issue(journal)

articles = []
for aop_issue in aop_issues:
_articles = controllers.get_articles_by_iid(aop_issue.iid, is_public=True)
Expand Down Expand Up @@ -1089,7 +1125,9 @@ def aop_toc(url_seg):
# o primeiro item da lista é o último número.
"last_issue": journal.last_issue,
}

context.update(
controllers.get_issue_nav_bar_data(journal=journal, issue=aop_issues[0])
)
return render_template("issue/toc.html", **context)


Expand Down
Loading

0 comments on commit ab677ee

Please sign in to comment.