diff --git a/sapl/base/search_indexes.py b/sapl/base/search_indexes.py index 18401b868..1b2944f48 100644 --- a/sapl/base/search_indexes.py +++ b/sapl/base/search_indexes.py @@ -4,6 +4,7 @@ import textract from django.template import Context, loader from haystack import indexes from sapl.materia.models import DocumentoAcessorio, MateriaLegislativa +from sapl.norma.models import NormaJuridica class DocumentoAcessorioIndex(indexes.SearchIndex, indexes.Indexable): @@ -60,3 +61,11 @@ class MateriaLegislativaIndex(DocumentoAcessorioIndex): filename = 'texto_original' model = MateriaLegislativa template_name = 'materia/materialegislativa_text.txt' + + +class NormaJuridicaIndex(DocumentoAcessorioIndex): + text = indexes.CharField(document=True, use_template=True) + + filename = 'texto_integral' + model = NormaJuridica + template_name = 'norma/normajuridica_text.txt' diff --git a/sapl/base/templatetags/common_tags.py b/sapl/base/templatetags/common_tags.py index a908b7396..419899632 100644 --- a/sapl/base/templatetags/common_tags.py +++ b/sapl/base/templatetags/common_tags.py @@ -3,6 +3,7 @@ from django import template from sapl.base.models import AppConfig from sapl.materia.models import DocumentoAcessorio, MateriaLegislativa +from sapl.norma.models import NormaJuridica from sapl.parlamentares.models import Filiacao register = template.Library() @@ -146,5 +147,7 @@ def search_get_model(object): return 'm' elif type(object) == DocumentoAcessorio: return 'd' + elif type(object) == NormaJuridica: + return 'n' return None diff --git a/sapl/norma/apps.py b/sapl/norma/apps.py index effd4f271..c4d55ade0 100644 --- a/sapl/norma/apps.py +++ b/sapl/norma/apps.py @@ -6,3 +6,6 @@ class AppConfig(apps.AppConfig): name = 'sapl.norma' label = 'norma' verbose_name = _('Norma Jurídica') + + def ready(self): + from . import signals diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 24e56dcb9..20b59956f 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -7,7 +7,8 @@ from model_utils import Choices from sapl.compilacao.models import TextoArticulado from sapl.materia.models import MateriaLegislativa -from sapl.utils import RANGE_ANOS, YES_NO_CHOICES, texto_upload_path +from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, texto_upload_path, + restringe_tipos_de_arquivo_txt) @reversion.register() @@ -70,7 +71,8 @@ class NormaJuridica(models.Model): blank=True, null=True, upload_to=texto_upload_path, - verbose_name=_('Texto Integral')) + verbose_name=_('Texto Integral'), + validators=[restringe_tipos_de_arquivo_txt]) tipo = models.ForeignKey( TipoNormaJuridica, on_delete=models.PROTECT, diff --git a/sapl/norma/signals.py b/sapl/norma/signals.py new file mode 100644 index 000000000..fc16f89b4 --- /dev/null +++ b/sapl/norma/signals.py @@ -0,0 +1,27 @@ +from django.db.models.signals import post_delete, post_save +from sapl.settings import PROJECT_DIR +from subprocess import PIPE, call +from threading import Thread + + +from .models import NormaJuridica + + +class UpdateIndexCommand(Thread): + def run(self): + call([PROJECT_DIR.child('manage.py'), 'update_index'], + stdout=PIPE) + + +def save_texto(sender, instance, **kwargs): + update_index = UpdateIndexCommand() + update_index.start() + + +def delete_texto(sender, instance, **kwargs): + update_index = UpdateIndexCommand() + update_index.start() + + +post_save.connect(save_texto, sender=NormaJuridica) +post_delete.connect(delete_texto, sender=NormaJuridica) diff --git a/sapl/templates/search/indexes/norma/normajuridica_text.txt b/sapl/templates/search/indexes/norma/normajuridica_text.txt new file mode 100644 index 000000000..0f9218324 --- /dev/null +++ b/sapl/templates/search/indexes/norma/normajuridica_text.txt @@ -0,0 +1,7 @@ +{% for k, v in extracted.metadata.items %} + {% for val in v %} + {{ k }}: {{ val|safe }} + {% endfor %} +{% endfor %} + +{{ extracted|striptags|safe }} \ No newline at end of file diff --git a/sapl/templates/search/search.html b/sapl/templates/search/search.html index bd925d37c..4efae2cc5 100644 --- a/sapl/templates/search/search.html +++ b/sapl/templates/search/search.html @@ -68,6 +68,16 @@ O texto deste documento foi removido recentemente. Em breve ele sairá desta listagem.
{% endif %}

+ + {% elif result.object|search_get_model == 'n' %} +

+ Norma Jurídica: {{ result.object }}
+ {% if result.object.texto_integral %} + Texto Original: Clique aqui
+ {% else %} + O texto desta norma foi removido recentemente. Em breve ela sairá desta listagem.
+ {% endif %} +

{% endif %}