diff --git a/sapl/lexml/forms.py b/sapl/lexml/forms.py new file mode 100644 index 000000000..7904508af --- /dev/null +++ b/sapl/lexml/forms.py @@ -0,0 +1,57 @@ +from django.core.exceptions import ValidationError +from django.forms import ModelForm +from sapl.settings import PROJECT_DIR +from django.utils.translation import ugettext_lazy as _ + +from io import StringIO +from lxml import etree +import os +import re +import xml.dom.minidom as dom + +from .models import LexmlProvedor + + +class LexmlProvedorForm(ModelForm): + class Meta: + model = LexmlProvedor + fields = [ + "id_provedor", + "nome", + "id_responsavel", + "nome_responsavel", + "email_responsavel", + "xml" + ] + + def clean(self): + cd = super().clean() + + if not self.is_valid(): + return cd + + if cd["xml"]: + xml = re.sub("\n|\t", "", cd["xml"].strip()) + + validar_xml(xml) + validar_schema(xml) + + return cd + + +def validar_xml(xml): + xml = StringIO(xml) + try: + dom.parse(xml) + except Exception as e: + raise ValidationError(_(F"XML mal formatado. Error: {e}")) + +def validar_schema(xml): + xml_schema = open(os.path.join(PROJECT_DIR, 'sapl/templates/lexml/schema.xsd'), 'rb').read() + schema_root = etree.XML(xml_schema) + schema = etree.XMLSchema(schema_root) + parser = etree.XMLParser(schema=schema) + try: + root = etree.fromstring(xml.encode(), parser) + except Exception as e: + raise ValidationError(_(F"XML mal formatado. Error: {e}")) diff --git a/sapl/lexml/views.py b/sapl/lexml/views.py index 92fee8310..e9b53a177 100644 --- a/sapl/lexml/views.py +++ b/sapl/lexml/views.py @@ -7,6 +7,8 @@ from sapl.rules import RP_DETAIL, RP_LIST from .models import LexmlProvedor, LexmlPublicador +from .forms import LexmlProvedorForm + LexmlPublicadorCrud = CrudAux.build(LexmlPublicador, 'lexml_publicador') @@ -15,6 +17,12 @@ class LexmlProvedorCrud(Crud): help_topic = 'lexml_provedor' public = [RP_LIST, RP_DETAIL] + class CreateView(Crud.CreateView): + form_class = LexmlProvedorForm + + class UpdateView(Crud.UpdateView): + form_class = LexmlProvedorForm + class DetailView(Crud.DetailView): layout_key = 'LexmlProvedorDetail' diff --git a/sapl/templates/lexml/schema.xsd b/sapl/templates/lexml/schema.xsd new file mode 100644 index 000000000..f85fc1077 --- /dev/null +++ b/sapl/templates/lexml/schema.xsd @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +