Skip to content

Commit

Permalink
Ajustes de templates descritivos
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobatalha committed Jul 6, 2017
1 parent 58a9a93 commit 676d9b7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 17 deletions.
35 changes: 28 additions & 7 deletions legendarium/formatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8

import re
import locale

from datetime import datetime

from legendarium.utils import translations
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -399,7 +419,6 @@ def descriptive_html_format(title='', short_title='', pubdate='', volume='', num
template = []
template.append('<div class="biblio_label">')
template.append('<span class="title">%T</span>')
template.append('<span class="year">%Y</span>')

if volume:
template.append('<span class="prefix volume">'+translations['volume'][language]+':</span> <span class="value volume">%v</span>')
Expand All @@ -418,6 +437,7 @@ def descriptive_html_format(title='', short_title='', pubdate='', volume='', num
template.pop()
template.append('<span class="prefix pages">'+translations['article number'][language]+':</span> <span class="value pages">%e</span>')

template.append('<span class="prefix published">'+translations['published'][language]+':</span> <span class="value published">%D</span>')
template.append('</div>')

output = CitationFormatter(
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -510,7 +532,6 @@ def descriptive_html_short_format(title='', short_title='', pubdate='', volume='
template = []
template.append('<div class="biblio_label">')
template.append('<span class="title">%T</span>')
template.append('<span class="year">%Y</span>')

if volume:
template.append('<span class="prefix volume">'+translations['volume'][language]+':</span> <span class="value volume">%v</span>')
Expand All @@ -520,7 +541,7 @@ def descriptive_html_short_format(title='', short_title='', pubdate='', volume='

if suppl:
template.append('<span class="prefix supplement">'+translations['supplement'][language]+'</span> <span class="value supplement">%s</span>')

template.append('<span class="prefix published">'+translations['published'][language]+':</span> <span class="value published">%D</span>')
template.append('</div>')

output = CitationFormatter(
Expand Down
5 changes: 5 additions & 0 deletions legendarium/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@
"es": "Numero del articulo",
"en": "Article number"
},
"published": {
"pt": "Publicado",
"es": "Publicado",
"en": "Published"
}
}
65 changes: 55 additions & 10 deletions tests/test_legendarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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

)
Expand All @@ -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

)
Expand All @@ -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

)
Expand All @@ -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

)
Expand All @@ -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

)
Expand All @@ -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
)

Expand All @@ -396,7 +441,7 @@ def test_descriptive_html_format(self):
result = descriptive_html_format(**self.sample)

self.assertEqual(
'<div class="biblio_label"><span class="title">Revista Mal-Estar Subjetivo</span><span class="year">2011</span><span class="prefix volume">Volume:</span> <span class="value volume">67</span><span class="prefix number">Number:</span> <span class="value number">9</span><span class="prefix supplement">Supplement</span> <span class="value supplement">3</span><span class="prefix pages">Article number:</span> <span class="value pages">e00120416</span></div>',
'<div class="biblio_label"><span class="title">Revista Mal-Estar Subjetivo</span><span class="prefix volume">Volume:</span> <span class="value volume">67</span><span class="prefix number">Number:</span> <span class="value number">9</span><span class="prefix supplement">Supplement</span> <span class="value supplement">3</span><span class="prefix pages">Article number:</span> <span class="value pages">e00120416</span><span class="prefix published">Published:</span> <span class="value published">2011</span></div>',
result
)

Expand All @@ -409,7 +454,7 @@ def test_descriptive_html_short_format(self):
result = descriptive_html_short_format(**self.sample)

self.assertEqual(
'<div class="biblio_label"><span class="title">Revista Mal-Estar Subjetivo</span><span class="year">2011</span><span class="prefix volume">Volume:</span> <span class="value volume">67</span><span class="prefix number">Number:</span> <span class="value number">9</span><span class="prefix supplement">Supplement</span> <span class="value supplement">3</span></div>',
'<div class="biblio_label"><span class="title">Revista Mal-Estar Subjetivo</span><span class="prefix volume">Volume:</span> <span class="value volume">67</span><span class="prefix number">Number:</span> <span class="value number">9</span><span class="prefix supplement">Supplement</span> <span class="value supplement">3</span><span class="prefix published">Published:</span> <span class="value published">2011</span></div>',
result
)

Expand Down

0 comments on commit 676d9b7

Please sign in to comment.