Skip to content

Commit

Permalink
Inibe a disponibilização de artigos que estão em "embargo" (#2155)
Browse files Browse the repository at this point in the history
* Cria uma função para criar um parâmetro `publication_date__lte` cujo valor é a data de hoje para que sejam retornados os artigos sem embargo

* Faz com que `get_article_by_aid` use o filtro para trazer apenas documentos sem embargo e acrescenta em `makeOneArticle` o atributo `publication_date`

* Faz com que `get_article_by_url_seg` use o filtro para trazer apenas documentos sem embargo

* Usa o filtro em `get_article_by_issue_article_seg` para obter artigos sem embargo

* Adiciona filtro em `get_article_by_aop_url_segs` para retornar artigos sem embargo

* Aplica filtro para trazer somente artigos sem embargo em `get_articles_by_iid`

* Aplica o filtro para trazer somente artigos sem embargo em função que obtém artigo pelos vários tipos de ID

* Insere o filtro para trazer somente artigos sem embargo nas funções:
`get_recent_articles_of_issue`, `get_article_by_pdf_filename`
Faz ajustes nos testes

* Conta apenas os artigos que não estão com embargo

* Melhoria no código
  • Loading branch information
robertatakenaka authored Dec 10, 2021
1 parent ba74f04 commit 658a29a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 16 deletions.
56 changes: 45 additions & 11 deletions opac/tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
from unittest.mock import patch
from unittest import TestCase

from werkzeug.security import check_password_hash

Expand Down Expand Up @@ -1329,7 +1330,8 @@ def test_get_article_by_scielo_pid_returns_article(self):
self.assertEqual(article._id, '012ijs9y24')
self.assertEqual(article.scielo_pids["v1"], 'S0101-0202(99)12345')

def test_get_recent_articles_of_issue(self):
@patch('webapp.controllers.now', return_value="2020-01-01")
def test_get_recent_articles_of_issue(self, mk):
self._make_one(attrib={
'_id': '012ijs9y24',
'issue': '90210j83',
Expand Down Expand Up @@ -1359,13 +1361,15 @@ def test_get_recent_articles_of_issue(self):

self._make_one(attrib={
'_id': '2183ikos9B',
'publication_date': '2022',
'issue': '90210j83',
'type': 'rapid-communication',
'journal': 'oak,ajimn1'
})

self._make_one(attrib={
'_id': '012ijs9y1B',
'publication_date': '2022',
'issue': '90210j83',
'type': 'research-article',
'journal': 'oak,ajimn1'
Expand All @@ -1390,17 +1394,20 @@ def test_get_recent_articles_of_issue(self):
})
result = controllers.get_recent_articles_of_issue(
issue_iid='90210j83', is_public=True)
self.assertEqual(len(result), 6)
self.assertEqual(len(result), 4)
result = [article._id for article in result]
expected = ['2183ikos90', '2183ikoD90', '2183ikos9B', '012ijs9y1B',
expected = ['2183ikos90', '2183ikoD90',
'2183ikoD9F', '012ijs9y14']
self.assertEqual(set(result), set(expected))

@patch('webapp.controllers.now', return_value="2020-01-01")
@patch('webapp.controllers.Article.objects')
@patch('webapp.controllers.get_journal_by_acron')
@patch('webapp.controllers.get_issue_by_label')
def test_get_article_by_pdf_filename_retrieves_articles_by_pdf_filename(
self, mk_get_issue_by_label, mk_get_journal_by_acron, mk_article_objects
self, mk_get_issue_by_label, mk_get_journal_by_acron,
mk_article_objects,
mk_now,
):

article = self._make_one(attrib={
Expand All @@ -1416,17 +1423,22 @@ def test_get_article_by_pdf_filename_retrieves_articles_by_pdf_filename(
article.journal.acronym, article.issue.label, "article.pdf"
)

mk_article_objects.filter.assert_called_once_with(is_public=True,
issue=article.issue,
journal=article.journal,
pdfs__filename='article.pdf'
)
mk_article_objects.filter.assert_called_once_with(
is_public=True,
issue=article.issue,
journal=article.journal,
pdfs__filename='article.pdf',
publication_date__lte='2020-01-01',
)

@patch('webapp.controllers.now', return_value="2021-01-01")
@patch('webapp.controllers.Article.objects')
@patch('webapp.controllers.get_journal_by_acron')
@patch('webapp.controllers.get_issue_by_label')
def test_get_article_by_pdf_filename_retrieves_aop_article_by_pdf_filename(
self, mk_get_issue_by_label, mk_get_journal_by_acron, mk_article_objects
self, mk_get_issue_by_label, mk_get_journal_by_acron,
mk_article_objects,
mk_now,
):

article = self._make_one(attrib={
Expand All @@ -1445,7 +1457,8 @@ def test_get_article_by_pdf_filename_retrieves_aop_article_by_pdf_filename(
journal=article.journal,
is_public=True,
issue='oak,ajimn1-aop',
pdfs__filename='article.pdf'
pdfs__filename='article.pdf',
publication_date__lte='2021-01-01',
)

def test_get_article_by_pdf_filename_raises_error_if_no_journal_acronym(self):
Expand Down Expand Up @@ -1846,3 +1859,24 @@ def test_get_page_by_slug_name(self):
{'Critérios', 'Criterios'}
)


@patch("webapp.controllers.now", return_value="2021-01-01")
class AddFilterWithoutEmbargoTestCase(TestCase):

def test_add_filter_without_embargo_returns_filter_if_input_is_None(self, mock_now):
expected = {"publication_date__lte": "2021-01-01"}
result = controllers.add_filter_without_embargo()
self.assertDictEqual(expected, result)

def test_add_filter_without_embargo_returns_filter_if_input_is_empty_dict(self, mock_now):
expected = {"publication_date__lte": "2021-01-01"}
result = controllers.add_filter_without_embargo({})
self.assertDictEqual(expected, result)

def test_add_filter_without_embargo_returns_filter_if_input_is_public_false(self, mock_now):
kwargs = {"is_public": False}
expected = {"is_public": False, "publication_date__lte": "2021-01-01"}
result = controllers.add_filter_without_embargo(kwargs)
self.assertDictEqual(expected, result)


1 change: 1 addition & 0 deletions opac/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def makeOneArticle(attrib=None): # noqa
'lpage': attrib.get('lpage', '16'),
'translated_titles': attrib.get('translated_titles', []),
'languages': attrib.get('languages', ['pt', ]),
'publication_date': "2000",
}
article.update(attrib)

Expand Down
67 changes: 62 additions & 5 deletions opac/webapp/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ou outras camadas superiores, evitando assim que as camadas superiores
acessem diretamente a camada inferior de modelos.
"""

from datetime import datetime
import re
import unicodecsv
import io
Expand Down Expand Up @@ -75,6 +75,21 @@ class PreviousOrNextArticleNotFoundError(Exception):
...


def now():
return datetime.utcnow().isoformat()[:10]


def add_filter_without_embargo(kwargs={}):
"""
Add filter to publish only articles which is allowed to be published
(not embargoed)
(only articles which are publication date is before or equal today date)
"""
kwargs = kwargs or {}
kwargs["publication_date__lte"] = now()
return kwargs


# -------- COLLECTION --------

def get_current_collection():
Expand Down Expand Up @@ -807,6 +822,9 @@ def get_article_by_aid(aid, journal_url_seg, lang=None, gs_abstract=False, **kwa
if not aid:
raise ValueError(__('Obrigatório um aid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

articles = Article.objects(pk=aid, is_public=True, **kwargs)
if not articles:
articles = Article.objects(
Expand Down Expand Up @@ -963,6 +981,9 @@ def get_article_by_url_seg(url_seg_article, **kwargs):
if not url_seg_article:
raise ValueError(__('Obrigatório um url_seg_article.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(url_segment=url_seg_article, **kwargs).first()


Expand All @@ -978,6 +999,9 @@ def get_article_by_issue_article_seg(iid, url_seg_article, **kwargs):
if not iid and url_seg_article:
raise ValueError(__('Obrigatório um iid and url_seg_article.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(issue=iid, url_segment=url_seg_article, **kwargs).first()


Expand All @@ -999,6 +1023,9 @@ def get_article_by_aop_url_segs(jid, url_seg_issue, url_seg_article, **kwargs):
"url_seg_issue": url_seg_issue
}

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(journal=jid, aop_url_segs=aop_url_segs, **kwargs).first()


Expand Down Expand Up @@ -1066,6 +1093,9 @@ def get_articles_by_iid(iid, **kwargs):
if not iid:
raise ValueError(__('Obrigatório um iid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

# FIXME - Melhorar esta consulta
# Em um fascículo em que não é aop nem publicação contínua
# todas as datas são iguais, então, `order_by`,
Expand Down Expand Up @@ -1109,6 +1139,9 @@ def get_article_by_pid_v1(v1, **kwargs):
if not v1:
raise ValueError(__('Obrigatório um pid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(
scielo_pids__v1=v1,
is_public=True,
Expand All @@ -1126,6 +1159,9 @@ def get_article_by_pid(pid, **kwargs):
if not pid:
raise ValueError(__('Obrigatório um pid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(pid=pid, **kwargs).first()


Expand All @@ -1139,6 +1175,9 @@ def get_article_by_oap_pid(aop_pid, **kwargs):
if not aop_pid:
raise ValueError(__('Obrigatório um aop_pid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(aop_pid=aop_pid, **kwargs).first()


Expand All @@ -1152,6 +1191,9 @@ def get_article_by_scielo_pid(scielo_pid, **kwargs):
if not scielo_pid:
raise ValueError(__('Obrigatório um pid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

return Article.objects(
(Q(pid=scielo_pid) | Q(scielo_pids__v1=scielo_pid) | Q(scielo_pids__v2=scielo_pid) | Q(scielo_pids__v3=scielo_pid)),
**kwargs
Expand All @@ -1170,6 +1212,9 @@ def get_article_by_pid_v2(v2, **kwargs):

v2 = v2.upper()

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo(kwargs)

articles = Article.objects(pid=v2, is_public=True, **kwargs)
if articles:
return articles[0]
Expand All @@ -1193,9 +1238,14 @@ def get_recent_articles_of_issue(issue_iid, is_public=True):
if not issue_iid:
raise ValueError(__('Parámetro obrigatório: issue_iid.'))

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo()

return Article.objects.filter(
issue=issue_iid, is_public=is_public,
type__in=HIGHLIGHTED_TYPES).order_by('-order')
type__in=HIGHLIGHTED_TYPES,
**kwargs
).order_by('-order')


def get_article_by_pdf_filename(journal_acron, issue_label, pdf_filename):
Expand Down Expand Up @@ -1226,10 +1276,15 @@ def get_article_by_pdf_filename(journal_acron, issue_label, pdf_filename):
prefix = splitted[0]
pdf_filename = pdf_filename[3:]

# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo()

article = Article.objects.filter(
journal=journal,
issue=issue, pdfs__filename=pdf_filename,
is_public=True).first()
is_public=True,
**kwargs,
).first()
if article:
for pdf in article.pdfs:
if ((pdf["filename"] == pdf_filename and prefix == '') or
Expand Down Expand Up @@ -1357,10 +1412,12 @@ def count_elements_by_type_and_visibility(type, public_only=False):
else:
return Issue.objects.count()
elif type == 'article':
# add filter publication_date__lte_today_date
kwargs = add_filter_without_embargo()
if public_only:
return Article.objects(is_public=True).count()
return Article.objects(is_public=True, **kwargs).count()
else:
return Article.objects.count()
return Article.objects(**kwargs).count()
elif type == 'news':
return News.objects.count()
elif type == 'sponsor':
Expand Down

0 comments on commit 658a29a

Please sign in to comment.