Skip to content

Commit

Permalink
Merge pull request #12 from jamilatta/bug11
Browse files Browse the repository at this point in the history
Corrigi o retorno da função ``date_pagination`` quando a data tem o mesmo ano.
  • Loading branch information
jamilatta authored Jul 8, 2020
2 parents a32ade5 + cf9bb40 commit 11cc93a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
26 changes: 17 additions & 9 deletions articlemeta/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import socket

from datetime import datetime
from datetime import timedelta

from collections import namedtuple

Expand Down Expand Up @@ -45,6 +44,12 @@ class ServerError(ArticleMetaExceptions):


def dates_pagination(from_date, until_date):
"""
Essa função tem como responsabilidade criar páginas por ano, portanto ao
realizar uma consulta indicando ou não um período este será dividido,
entre os anos e o deslocamento.
"""

from_date = datetime.strptime(from_date, '%Y-%m-%d')
until_date = datetime.strptime(until_date, '%Y-%m-%d')

Expand All @@ -53,6 +58,10 @@ def dates_pagination(from_date, until_date):
dtbg = '%d-01-01' % year
dtnd = '%d-12-31' % year

if from_date.year == until_date.year:
yield (from_date.isoformat()[:10], until_date.isoformat()[:10])
continue

if year == from_date.year:
yield (from_date.isoformat()[:10], dtnd)
continue
Expand Down Expand Up @@ -321,7 +330,7 @@ def issues(
params['offset'] += 100

def issues_by_identifiers(self, collection=None, issn=None, from_date=None,
until_date=None, only_identifiers=False):
until_date=None, only_identifiers=False):

params = {
'limit': LIMIT
Expand Down Expand Up @@ -363,8 +372,7 @@ def issues_by_identifiers(self, collection=None, issn=None, from_date=None,

params['offset'] += LIMIT

def issues_history(self, collection=None, issn=None, from_date=None,
until_date=None, only_identifiers=False):
def issues_history(self, collection=None, issn=None, from_date=None, until_date=None, only_identifiers=False):

params = {
'limit': LIMIT
Expand Down Expand Up @@ -449,10 +457,12 @@ def documents(

fdate = from_date or DEFAULT_FROM_DATE
udate = until_date or datetime.today().isoformat()[:10]

for from_date, until_date in dates_pagination(fdate, udate):
params['from'] = from_date
params['until'] = until_date
params['offset'] = 0

while True:
url = urljoin(self.ARTICLEMETA_URL, self.ARTICLES_ENDPOINT)
articles = self._do_request(url, params=params, timeout=10)
Expand Down Expand Up @@ -651,7 +661,7 @@ def dispatcher(self, *args, **kwargs):
response = getattr(cl, func)(*args[1:], **kwargs)
return response

except (TTransportException, self.ARTICLEMETA_THRIFT.ServerError,
except (TTransportException, self.ARTICLEMETA_THRIFT.ServerError,
socket.timeout) as e:
msg = 'Error requesting articlemeta: %s args: %s kwargs: %s message: %s' % (
str(func), str(args[1:]), str(kwargs), str(e)
Expand Down Expand Up @@ -1074,7 +1084,6 @@ def documents(self, collection=None, issn=None, from_date=None,

fdate = from_date or DEFAULT_FROM_DATE
udate = until_date or datetime.today().isoformat()[:10]

for from_date, until_date in dates_pagination(fdate, udate):
offset = 0
while True:
Expand Down Expand Up @@ -1213,6 +1222,5 @@ def delete_document(self, code, collection):
return json.loads(result)

def get_issue_code_from_label(self, label, journal_code, collection):
return self.dispatcher('get_issue_code_from_label',
label, journal_code, collection)

return self.dispatcher('get_issue_code_from_label', label,
journal_code, collection)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setuptools.setup(
name="articlemetaapi",
version="1.26.6",
version="1.26.7",
description="SciELO ArticleMeta SDK for Python",
author="SciELO",
author_email="[email protected]",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ def test_dates_pagination(self):
]

self.assertEqual(sorted(expected), sorted(result))

def test_dates_pagination_same_year(self):

result = [i for i in client.dates_pagination('2020-05-15', '2020-05-16')]

expected = [
('2020-05-15', '2020-05-16'),
]

self.assertEqual(sorted(expected), sorted(result))

0 comments on commit 11cc93a

Please sign in to comment.