Browse Source

separa extrações em funções individuais

pull/1594/head
LeandroRoberto 8 years ago
parent
commit
ed9be33964
  1. 57
      sapl/base/search_indexes.py

57
sapl/base/search_indexes.py

@ -62,46 +62,35 @@ class TextExtractField(CharField):
print(msg)
logger.error(msg)
def extract_data(self, obj):
def file_extraction(self, arquivo):
r = []
if not os.path.exists(arquivo.path):
return r
data = []
for attr in self.model_attr:
if not hasattr(obj, attr):
raise Exception
value = getattr(obj, attr)
if not value:
continue
if isinstance(value, FieldFile):
if not os.path.exists(value.path):
continue
if not os.path.splitext(value.path)[1][:1]:
continue
if not os.path.splitext(arquivo.path)[1][:1]:
return r
# Em ambiente de produção utiliza-se o SOLR
if SOLR_URL:
try:
data.append(self.solr_extraction(value))
r.append(self.solr_extraction(arquivo))
except Exception:
self.print_error(value)
self.print_error(arquivo)
# Em ambiente de DEV utiliza-se o Whoosh
# Como ele não possui extração, faz-se uso do textract
else:
try:
data.append(self.whoosh_extraction(value))
r.apend(self.whoosh_extraction(arquivo))
except ExtensionNotSupported as e:
print(str(e))
logger.error(str(e))
except Exception:
self.print_error(value)
elif hasattr(value, 'model') and value.model == TextoArticulado:
self.print_error(arquivo)
return r
def ta_extraction(self, value):
r = []
for ta in value.filter(privacidade__in=[
STATUS_TA_PUBLIC,
STATUS_TA_IMMUTABLE_PUBLIC]):
@ -116,7 +105,25 @@ class TextExtractField(CharField):
)
).values_list(
'rotulo_texto', flat=True)
data += list(filter(lambda x: x.strip(), dispositivos))
r += list(filter(lambda x: x.strip(), dispositivos))
return r
def extract_data(self, obj):
data = []
for attr in self.model_attr:
if not hasattr(obj, attr):
raise Exception
value = getattr(obj, attr)
if not value:
continue
if isinstance(value, FieldFile):
data.append(self.file_extraction(value))
elif hasattr(value, 'model') and value.model == TextoArticulado:
data += self.ta_extraction(value)
return ' '.join(data)

Loading…
Cancel
Save