Browse Source

Conserta request com range de datas

pull/2601/head
Edward Ribeiro 6 years ago
parent
commit
60280c3924
  1. 33
      sapl/lexml/OAIServer.py
  2. 2
      sapl/lexml/views.py

33
sapl/lexml/OAIServer.py

@ -26,8 +26,9 @@ class OAILEXML:
def __call__(self, element, metadata): def __call__(self, element, metadata):
data = metadata.record data = metadata.record
value = etree.XML(data['metadata']) if data['metadata']:
element.append(value) value = etree.XML(data['metadata'])
element.append(value)
class OAIServer: class OAIServer:
@ -64,22 +65,22 @@ class OAIServer:
metadata.record = record metadata.record = record
return header, metadata return header, metadata
def list_query(self, from_date=None, until_date=None, offset=0, batch_size=10, identifier=None): def list_query(self, from_=None, until=None, offset=0, batch_size=10, identifier=None):
if identifier: if identifier:
identifier = int(identifier.split('/')[-1]) # Get internal id identifier = int(identifier.split('/')[-1]) # Get internal id
else: else:
identifier = '' identifier = ''
until_date = datetime.now() if not until_date or until_date > datetime.now() else until_date until = datetime.now() if not until or until > datetime.now() else until
return self.oai_query(offset=offset, batch_size=batch_size, from_date=from_date, until_date=until_date, return self.oai_query(offset=offset, batch_size=batch_size, from_=from_, until=until,
identifier=identifier) identifier=identifier)
def check_metadata_prefix(self, metadata_prefix): def check_metadata_prefix(self, metadata_prefix):
if not metadata_prefix in self.config['metadata_prefixes']: if not metadata_prefix in self.config['metadata_prefixes']:
raise oaipmh.error.CannotDisseminateFormatError raise oaipmh.error.CannotDisseminateFormatError
def listRecords(self, metadataPrefix, from_date=None, until_date=None, cursor=0, batch_size=10): def listRecords(self, metadataPrefix, from_=None, until=None, cursor=0, batch_size=10):
self.check_metadata_prefix(metadataPrefix) self.check_metadata_prefix(metadataPrefix)
for record in self.list_query(from_date, until_date, cursor, batch_size): for record in self.list_query(from_, until, cursor, batch_size):
header, metadata = self.create_header_and_metadata(record) header, metadata = self.create_header_and_metadata(record)
yield header, metadata, None # None? yield header, metadata, None # None?
@ -98,10 +99,10 @@ class OAIServer:
appconfig = AppConfig.objects.first() appconfig = AppConfig.objects.first()
return appconfig.esfera_federacao return appconfig.esfera_federacao
def recupera_norma(self, offset, batch_size, from_date, until_date, identifier, esfera): def recupera_norma(self, offset, batch_size, from_, until, identifier, esfera):
kwargs = {'data__lte': until_date} kwargs = {'data__lte': until}
if from_date: if from_:
kwargs['data__gte'] = from_date kwargs['data__gte'] = from_
if identifier: if identifier:
kwargs['numero'] = identifier kwargs['numero'] = identifier
if esfera: if esfera:
@ -144,9 +145,9 @@ class OAIServer:
else: else:
urn += '{};'.format(norma.data.isoformat()) urn += '{};'.format(norma.data.isoformat())
if norma.tipo.equivalente_lexml == 'lei.organica' or norma.tipo.equivalente_lexml == 'constituicao': if norma.tipo.equivalente_lexml == 'lei.organica' or norma.tipo.equivalente_lexml == 'constituicao':
urn += norma.ano urn += str(norma.ano)
else: else:
urn += norma.numero urn += str(norma.numero)
if norma.data_vigencia and norma.data_publicacao: if norma.data_vigencia and norma.data_publicacao:
urn += '@{};publicacao;{}'.format(norma.data_vigencia.isoformat(), norma.data_publicacao.isoformat()) urn += '@{};publicacao;{}'.format(norma.data_vigencia.isoformat(), norma.data_publicacao.isoformat())
elif norma.data_publicacao: elif norma.data_publicacao:
@ -213,12 +214,12 @@ class OAIServer:
else: else:
return None return None
def oai_query(self, offset=0, batch_size=10, from_date=None, until_date=None, identifier=None): def oai_query(self, offset=0, batch_size=10, from_=None, until=None, identifier=None):
esfera = self.get_esfera_federacao() esfera = self.get_esfera_federacao()
offset = 0 if offset < 0 else offset offset = 0 if offset < 0 else offset
batch_size = 10 if batch_size < 0 else batch_size batch_size = 10 if batch_size < 0 else batch_size
until_date = datetime.now() if not until_date or until_date > datetime.now() else until_date until = datetime.now() if not until or until > datetime.now() else until
normas = self.recupera_norma(offset, batch_size, from_date, until_date, identifier, esfera) normas = self.recupera_norma(offset, batch_size, from_, until, identifier, esfera)
for norma in normas: for norma in normas:
resultado = {} resultado = {}
identificador = self.monta_id(norma) identificador = self.monta_id(norma)

2
sapl/lexml/views.py

@ -27,4 +27,4 @@ def lexml_request(request):
def request_search(request, keyword): def request_search(request, keyword):
return render(request, "lexml/resultado-pesquisa.html", {"keyword":keyword}) return render(request, "lexml/resultado-pesquisa.html", {"keyword": keyword})

Loading…
Cancel
Save