Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Adiciona checagem de HTML mal formado #3394

Open
wants to merge 1 commit into
base: 3.1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions sapl/compilacao/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ def get_queryset(self):

class TextEditView(CompMixin, TemplateView):
template_name = 'compilacao/text_edit.html'
logger = logging.getLogger(__name__)

def has_permission(self):
self.object = self.ta
Expand All @@ -1112,6 +1113,13 @@ def get(self, request, *args, **kwargs):
# TODO - implementar logging de ação de usuário
self.object.editing_locked = False
self.object.privacidade = STATUS_TA_EDITION
valid, error_msg = valida_html(self.object.ementa)
if not valid:
messages.error(
request, _('Texto mal formado %s', error_msg))
return redirect(to=reverse_lazy(
'sapl.compilacao:ta_text', kwargs={
'ta_id': self.object.id}))
self.object.save()
messages.success(request, _(
'Texto Articulado desbloqueado com sucesso.'))
Expand Down Expand Up @@ -1142,11 +1150,25 @@ def get(self, request, *args, **kwargs):
if 'lock' in request.GET:
self.object.editing_locked = True
self.object.privacidade = STATUS_TA_PUBLIC
valid, error_msg = valida_html(self.object.ementa)
if not valid:
messages.error(
request, _('Texto mal formado %s', error_msg))
return redirect(to=reverse_lazy(
'sapl.compilacao:ta_text_notificacoes', kwargs={
'ta_id': self.object.id}))
self.object.save()
messages.success(request, _(
'Texto Articulado publicado com sucesso.'))
else:
self.object.temp_check_migrations = True
valid, error_msg = valida_html(self.object.ementa)
if not valid:
messages.error(
request, _('Texto mal formado %s', error_msg))
return redirect(to=reverse_lazy(
'sapl.compilacao:ta_text_notificacoes', kwargs={
'ta_id': self.object.id}))
self.object.save()
messages.success(request, _(
'Texto Articulado Checado...'))
Expand Down Expand Up @@ -1369,6 +1391,9 @@ def runBase(self):
e.inicio_vigencia = ta.data
e.inicio_eficacia = ta.data
e.texto = ta.ementa
valid, error_msg = valida_html(d.texto)
if not valid:
pass # TODO: mensagem de erro?
e.dispositivo_pai = a
e.save()

Expand Down Expand Up @@ -3024,6 +3049,9 @@ def post(self, request, *args, **kwargs):

d_texto = d.texto
d.texto = texto.strip()
valid, error_msg = valida_html(d.texto)
if not valid:
pass # TODO: mensagem de erro?
d.texto_atualizador = texto_atualizador.strip()

d.visibilidade = not visibilidade or visibilidade == 'True'
Expand Down Expand Up @@ -3521,3 +3549,17 @@ def get_queryset(self):
type_notificacoes = [type_notificacoes, ]

return self.get_notificacoes(result, type_notificacoes)


def valida_html(html):
import logging
from lxml import etree
from io import StringIO
logger = logging.getLogger(__name__)
try:
if len(html.strip()) > 0:
etree.parse(StringIO(html), etree.HTMLParser(recover=False))
return True, None
except Exception as e:
logger.error("HTML mal formado %s (%s)", html, str(e))
return False, str(e)