From db74e7ad451558375d87587ee4222ec82437a2d6 Mon Sep 17 00:00:00 2001 From: Edward Ribeiro Date: Tue, 21 Mar 2017 16:19:00 -0300 Subject: [PATCH] =?UTF-8?q?Cria=20configura=C3=A7=C3=A3o=20no=20Django?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + requirements/requirements.txt | 5 +++ sapl/materia/search_indexes.py | 31 ++++++++++++++ sapl/materia/urls.py | 2 + sapl/settings.py | 19 +++++++++ .../materia/documentoacessorio_text.txt | 7 ++++ sapl/templates/search/search.html | 42 +++++++++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 sapl/materia/search_indexes.py create mode 100644 sapl/templates/search/indexes/materia/documentoacessorio_text.txt create mode 100644 sapl/templates/search/search.html diff --git a/.gitignore b/.gitignore index 38805b350..66613ff74 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,8 @@ var/ .installed.cfg *.egg +whoosh_index/ + # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 36ed3844b..3ca7cf215 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -13,6 +13,7 @@ django-extensions==1.6.7 django-extra-views==0.8.0 django-filter==0.15.3 django-floppyforms==1.6.2 +django-haystack django-model-utils==2.5 django-sass-processor==0.4.6 djangorestframework @@ -29,3 +30,7 @@ unipath==1.1 python-magic==0.4.12 gunicorn==19.6.0 django-reversion==2.0.8 +whoosh +pysolr +elasticsearch +textract diff --git a/sapl/materia/search_indexes.py b/sapl/materia/search_indexes.py new file mode 100644 index 000000000..f85437abc --- /dev/null +++ b/sapl/materia/search_indexes.py @@ -0,0 +1,31 @@ + +from haystack import indexes +from sapl.materia.models import DocumentoAcessorio + +class DocumentoAcessorioIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + nome = indexes.CharField(model_attr='nome') + autor = indexes.CharField(model_attr='autor') + +# def prepare(self, obj): +# data = super(DocumentoAcessorioIndex, self).prepare(obj) +# if obj.arquivo is not None: +# file_data = self._get_backend(None).extract_file_contents( +# obj.arquivo, +# ) +# template = loader.select_template( +# ("search/indexes/materia/documentoacessorio_text.txt", ), +# ) +# +# data["text"] = template.render(Context({ +# "object": obj, +# "file_data": file_data, +# })) +# +# return data + + def get_model(self): + return DocumentoAcessorio + + def index_queryset(self, using=None): + return self.get_model().objects.all() diff --git a/sapl/materia/urls.py b/sapl/materia/urls.py index 9f774a3d3..004b20335 100644 --- a/sapl/materia/urls.py +++ b/sapl/materia/urls.py @@ -67,6 +67,8 @@ urlpatterns_materia = [ name='primeira_tramitacao_em_lote'), url(r'^materia/tramitacao-em-lote', TramitacaoEmLoteView.as_view(), name='tramitacao_em_lote'), + + url(r'^materia/acessorio/search/', include('haystack.urls')), ] diff --git a/sapl/settings.py b/sapl/settings.py index 37646e675..e70829dd9 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -14,6 +14,7 @@ See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ """ import logging +import os from decouple import config from dj_database_url import parse as db_url @@ -77,6 +78,7 @@ INSTALLED_APPS = ( 'easy_thumbnails', 'floppyforms', 'sass_processor', + 'haystack', 'rest_framework', 'reversion', @@ -173,6 +175,23 @@ SERVER_EMAIL = config('SERVER_EMAIL', cast=str, default='') MAX_DOC_UPLOAD_SIZE = 5 * 1024 * 1024 # 5MB MAX_IMAGE_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine', + 'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'), + }, +# 'default': { +# 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', +# 'URL': 'http://127.0.0.1:9200/', +# 'INDEX_NAME': 'products', +# }, +# 'default': { +# 'ENGINE': 'haystack.backends.solr_backend.SolrEngine', +# 'URL': 'http://127.0.0.1:8983/', +# 'INDEX_NAME': 'products', +# }, +} + # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = 'pt-br' diff --git a/sapl/templates/search/indexes/materia/documentoacessorio_text.txt b/sapl/templates/search/indexes/materia/documentoacessorio_text.txt new file mode 100644 index 000000000..b406694bd --- /dev/null +++ b/sapl/templates/search/indexes/materia/documentoacessorio_text.txt @@ -0,0 +1,7 @@ +{{ object.tipo.sigla }} +{{ object.tipo.descricao }} +{{ object.materia.numero}} +{{ object.materia.ano}} +{{ object.nome }} +{{ object.data }} +{{ object.ementa }} diff --git a/sapl/templates/search/search.html b/sapl/templates/search/search.html new file mode 100644 index 000000000..8fc32da6d --- /dev/null +++ b/sapl/templates/search/search.html @@ -0,0 +1,42 @@ +{% extends "crud/detail.html" %} +{% block detail_content %} +

Search

+ +
+ + {{ form.as_table }} + + + + +
  + +
+ + {% if query %} +

Results

+ + {% for result in page.object_list %} +

+ + {{ result.object.materia.numero}}/ + {{ result.object.materia.ano }} - + {{ result.object.ementa }} + +

+ {% empty %} +

No results found.

+ {% endfor %} + + {% if page.has_previous or page.has_next %} +
+ {% if page.has_previous %}{% endif %}« Anterior{% if page.has_previous %}{% endif %} + | + {% if page.has_next %}{% endif %}Próximo »{% if page.has_next %}{% endif %} +
+ {% endif %} + {% else %} + {# Show some example queries to run, maybe query syntax, something else? #} + {% endif %} +
+{% endblock %}