From 676d9b7e74b365c511524fe82d1d4571501eb3f6 Mon Sep 17 00:00:00 2001 From: fabiobatalha Date: Thu, 6 Jul 2017 18:14:21 -0300 Subject: [PATCH] Ajustes de templates descritivos --- legendarium/formatter.py | 35 ++++++++++++++++----- legendarium/utils.py | 5 +++ tests/test_legendarium.py | 65 +++++++++++++++++++++++++++++++++------ 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/legendarium/formatter.py b/legendarium/formatter.py index 54ffda2..97fb77d 100644 --- a/legendarium/formatter.py +++ b/legendarium/formatter.py @@ -1,6 +1,7 @@ # coding: utf-8 - import re +import locale + from datetime import datetime from legendarium.utils import translations @@ -34,7 +35,7 @@ def parse_date(value): dt = datetime.strptime(value, '%Y') return dt.isoformat()[:4] except: - raise ValueError(u'Probably not a valid year') + raise ValueError(u'Probably not a valid date') class CitationFormatter: @@ -96,6 +97,22 @@ def yearpubdata(self): return self._pubdate[0:4] + @property + def descriptive_dmy_date(self): + + if len(self._pubdate) == 10: + dt = datetime.strptime(self._pubdate, '%Y-%m-%d') + dt = dt.strftime('%d %b %Y').upper() + + if len(self._pubdate) == 7: + dt = datetime.strptime(self._pubdate, '%Y-%m') + dt = dt.strftime('%b %Y').upper() + + if len(self._pubdate) == 4: + dt = self._pubdate + + return dt + @property def pubdate(self): @@ -199,6 +216,7 @@ def __format__(self, fmt_spec=''): '%T': self.title, '%t': self.short_title, '%Y': self.yearpubdata, + '%D': self.descriptive_dmy_date, '%v': self.volume, '%n': self.number, '%s': self.suppl, @@ -328,7 +346,7 @@ def descriptive_format(title='', short_title='', pubdate='', volume='', number=' Revista Mal-Estar Subjetivo, 2011, volume: 67, number: 9, supplement: 3, pages: 154-200 """ - template = ['%T, %Y'] + template = ['%T'] if volume: template.append(translations['volume'][language]+': %v') @@ -350,6 +368,8 @@ def descriptive_format(title='', short_title='', pubdate='', volume='', number=' template.pop() template.append(translations['article number'][language]+': %e') + template.append(translations['published'][language]+': %D') + output = CitationFormatter( title=title, short_title=short_title, @@ -399,7 +419,6 @@ def descriptive_html_format(title='', short_title='', pubdate='', volume='', num template = [] template.append('
') template.append('%T') - template.append('%Y') if volume: template.append(''+translations['volume'][language]+': %v') @@ -418,6 +437,7 @@ def descriptive_html_format(title='', short_title='', pubdate='', volume='', num template.pop() template.append(''+translations['article number'][language]+': %e') + template.append(''+translations['published'][language]+': %D') template.append('
') output = CitationFormatter( @@ -452,7 +472,7 @@ def descriptive_short_format(title='', short_title='', pubdate='', volume='', nu Revista Mal-Estar Subjetivo, 2011, Volume: 67, Number: 9, Supplement: 3 """ - template = ['%T, %Y'] + template = ['%T'] if volume: template.append(translations['volume'][language]+': %v') @@ -466,6 +486,8 @@ def descriptive_short_format(title='', short_title='', pubdate='', volume='', nu else: template[-1] += ' '+translations['supplement'][language]+' %s' + template.append(translations['published'][language]+': %D') + output = CitationFormatter( title=title, short_title=short_title, @@ -510,7 +532,6 @@ def descriptive_html_short_format(title='', short_title='', pubdate='', volume=' template = [] template.append('
') template.append('%T') - template.append('%Y') if volume: template.append(''+translations['volume'][language]+': %v') @@ -520,7 +541,7 @@ def descriptive_html_short_format(title='', short_title='', pubdate='', volume=' if suppl: template.append(''+translations['supplement'][language]+' %s') - + template.append(''+translations['published'][language]+': %D') template.append('
') output = CitationFormatter( diff --git a/legendarium/utils.py b/legendarium/utils.py index 3a9e755..e1bf793 100644 --- a/legendarium/utils.py +++ b/legendarium/utils.py @@ -31,4 +31,9 @@ "es": "Numero del articulo", "en": "Article number" }, + "published": { + "pt": "Publicado", + "es": "Publicado", + "en": "Published" + } } diff --git a/tests/test_legendarium.py b/tests/test_legendarium.py index 4d68606..09ec651 100644 --- a/tests/test_legendarium.py +++ b/tests/test_legendarium.py @@ -32,6 +32,51 @@ def setUp(self): self.legendarium = CitationFormatter(**self.sample) + def test_descriptive_ymd_date_pt(self): + + import locale + + locale.setlocale(locale.LC_TIME, 'pt_BR') + + self.sample['pubdate'] = '2011-12-31' + + legendarium = CitationFormatter(**self.sample) + + self.assertEqual( + '31 DEZ 2011', + legendarium.descriptive_dmy_date + ) + + def test_descriptive_ymd_date_en(self): + + import locale + + locale.setlocale(locale.LC_TIME, 'en_US') + + self.sample['pubdate'] = '2011-12-31' + + legendarium = CitationFormatter(**self.sample) + + self.assertEqual( + '31 DEC 2011', + legendarium.descriptive_dmy_date + ) + + def test_descriptive_ymd_date_es(self): + + import locale + + locale.setlocale(locale.LC_TIME, 'es_ES') + + self.sample['pubdate'] = '2011-12-31' + + legendarium = CitationFormatter(**self.sample) + + self.assertEqual( + '31 DIC 2011', + legendarium.descriptive_dmy_date + ) + def test_format(self): result = self.legendarium.format('%T, %Y, %v(%n), %p') @@ -253,7 +298,7 @@ def test_descriptive_format(self): result = descriptive_format(**self.sample) self.assertEqual( - 'Revista Mal-Estar Subjetivo, 2011, Volume: 67, Number: 9 Supplement 3, Article number: e00120416', + 'Revista Mal-Estar Subjetivo, Volume: 67, Number: 9 Supplement 3, Article number: e00120416, Published: 2011', result ) @@ -264,7 +309,7 @@ def test_descriptive_format_1(self): result = descriptive_format(**self.sample) self.assertEqual( - 'Revista Mal-Estar Subjetivo, 2011, Volume: 67, Number: 9 Supplement 3, Pages: 154-200', + 'Revista Mal-Estar Subjetivo, Volume: 67, Number: 9 Supplement 3, Pages: 154-200, Published: 2011', result ) @@ -285,7 +330,7 @@ def test_descriptive_format_number_only(self): result = descriptive_format(**data) self.assertEqual( - 'Cadernos Pagu, 2017, Number: 50, Article number: e175002', + 'Cadernos Pagu, Number: 50, Article number: e175002, Published: 2017', result ) @@ -307,7 +352,7 @@ def test_descriptive_format_suppl_number(self): result = descriptive_format(**data) self.assertEqual( - 'Revista de Odontologia da Universidade de São Paulo, 1997, Volume: 11, Number: 21 Supplement 1, Pages: 121-143', + 'Revista de Odontologia da Universidade de São Paulo, Volume: 11, Number: 21 Supplement 1, Pages: 121-143, Published: 1997', result ) @@ -329,7 +374,7 @@ def test_descriptive_format_suppl_number_1(self): result = descriptive_format(**data) self.assertEqual( - 'Revista de Odontologia da Universidade de São Paulo, 1997, Volume: 11, Number: 21 Supplement, Pages: 121-143', + 'Revista de Odontologia da Universidade de São Paulo, Volume: 11, Number: 21 Supplement, Pages: 121-143, Published: 1997', result ) @@ -351,7 +396,7 @@ def test_descriptive_format_suppl_volume(self): result = descriptive_format(**data) self.assertEqual( - 'Revista de Odontologia da Universidade de São Paulo, 1997, Volume: 11 Supplement 1, Pages: 121-143', + 'Revista de Odontologia da Universidade de São Paulo, Volume: 11 Supplement 1, Pages: 121-143, Published: 1997', result ) @@ -373,7 +418,7 @@ def test_descriptive_format_suppl_volume_1(self): result = descriptive_format(**data) self.assertEqual( - 'Revista de Odontologia da Universidade de São Paulo, 1997, Volume: 11 Supplement, Pages: 121-143', + 'Revista de Odontologia da Universidade de São Paulo, Volume: 11 Supplement, Pages: 121-143, Published: 1997', result ) @@ -387,7 +432,7 @@ def test_descriptive_short_format(self): result = descriptive_short_format(**self.sample) self.assertEqual( - 'Revista Mal-Estar Subjetivo, 2011, Volume: 67, Number: 9 Supplement 3', + 'Revista Mal-Estar Subjetivo, Volume: 67, Number: 9 Supplement 3, Published: 2011', result ) @@ -396,7 +441,7 @@ def test_descriptive_html_format(self): result = descriptive_html_format(**self.sample) self.assertEqual( - '
Revista Mal-Estar Subjetivo2011Volume: 67Number: 9Supplement 3Article number: e00120416
', + '
Revista Mal-Estar SubjetivoVolume: 67Number: 9Supplement 3Article number: e00120416Published: 2011
', result ) @@ -409,7 +454,7 @@ def test_descriptive_html_short_format(self): result = descriptive_html_short_format(**self.sample) self.assertEqual( - '
Revista Mal-Estar Subjetivo2011Volume: 67Number: 9Supplement 3
', + '
Revista Mal-Estar SubjetivoVolume: 67Number: 9Supplement 3Published: 2011
', result )