diff --git a/opac/tests/test_controller.py b/opac/tests/test_controller.py index ecdb2aaff..8135cf826 100644 --- a/opac/tests/test_controller.py +++ b/opac/tests/test_controller.py @@ -1,5 +1,6 @@ # coding: utf-8 from unittest.mock import patch +from unittest import TestCase from werkzeug.security import check_password_hash @@ -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', @@ -1359,6 +1361,7 @@ 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' @@ -1366,6 +1369,7 @@ def test_get_recent_articles_of_issue(self): self._make_one(attrib={ '_id': '012ijs9y1B', + 'publication_date': '2022', 'issue': '90210j83', 'type': 'research-article', 'journal': 'oak,ajimn1' @@ -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={ @@ -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={ @@ -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): @@ -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) + + diff --git a/opac/tests/utils.py b/opac/tests/utils.py index bf8cff556..087bf140c 100644 --- a/opac/tests/utils.py +++ b/opac/tests/utils.py @@ -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) diff --git a/opac/webapp/controllers.py b/opac/webapp/controllers.py index a23166f23..9a0013e72 100644 --- a/opac/webapp/controllers.py +++ b/opac/webapp/controllers.py @@ -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 @@ -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(): @@ -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( @@ -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() @@ -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() @@ -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() @@ -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`, @@ -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, @@ -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() @@ -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() @@ -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 @@ -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] @@ -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): @@ -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 @@ -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':