Browse Source

Cria LexmlProvedorForm (#3309)

Adiciona validação do XML
pull/3316/head
Vinícius Cantuária 4 years ago
committed by GitHub
parent
commit
3789b3a375
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      sapl/lexml/forms.py
  2. 8
      sapl/lexml/views.py
  3. 106
      sapl/templates/lexml/schema.xsd

57
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}"))

8
sapl/lexml/views.py

@ -7,6 +7,8 @@ from sapl.rules import RP_DETAIL, RP_LIST
from .models import LexmlProvedor, LexmlPublicador from .models import LexmlProvedor, LexmlPublicador
from .forms import LexmlProvedorForm
LexmlPublicadorCrud = CrudAux.build(LexmlPublicador, 'lexml_publicador') LexmlPublicadorCrud = CrudAux.build(LexmlPublicador, 'lexml_publicador')
@ -15,6 +17,12 @@ class LexmlProvedorCrud(Crud):
help_topic = 'lexml_provedor' help_topic = 'lexml_provedor'
public = [RP_LIST, RP_DETAIL] public = [RP_LIST, RP_DETAIL]
class CreateView(Crud.CreateView):
form_class = LexmlProvedorForm
class UpdateView(Crud.UpdateView):
form_class = LexmlProvedorForm
class DetailView(Crud.DetailView): class DetailView(Crud.DetailView):
layout_key = 'LexmlProvedorDetail' layout_key = 'LexmlProvedorDetail'

106
sapl/templates/lexml/schema.xsd

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:profile="http://www.lexml.gov.br/profile_lexml" xmlns:xml="http://www.w3.org/XML/1998/namespace" targetNamespace="http://www.lexml.gov.br/profile_lexml" elementFormDefault="qualified" attributeFormDefault="unqualified" xml:lang="PT">
<import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
<element name="ConfiguracaoProvedor" type="profile:ConfiguracaoProvedorType"/>
<complexType name="ConfiguracaoProvedorType">
<sequence>
<element name="Provedor" type="profile:ProvedorType" maxOccurs="unbounded"/>
</sequence>
<attribute name="dataGeracao" type="dateTime" use="required"/>
</complexType>
<complexType name="ProvedorType">
<sequence>
<element name="Administrador" type="profile:ResponsavelType"/>
<element name="Publicador" type="profile:PublicadorType" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="idProvedor" type="integer" use="required"/>
<attribute name="nome" use="optional">
<simpleType>
<restriction base="string">
<maxLength value="255"/>
<whiteSpace value="preserve"/>
<minLength value="1"/>
</restriction>
</simpleType>
</attribute>
<attribute name="tipo" use="optional" default="Provedor">
<simpleType>
<restriction base="string">
<enumeration value="Provedor"/>
<enumeration value="Agregador"/>
</restriction>
</simpleType>
</attribute>
<attribute name="baseURL" type="anyURI"/>
</complexType>
<complexType name="PublicadorType">
<sequence>
<element name="Responsavel" type="profile:ResponsavelType"/>
<element name="Perfil" type="profile:PerfilType" maxOccurs="unbounded"/>
</sequence>
<attribute name="idPublicador" type="integer" use="required"/>
<attribute name="nome" use="required">
<simpleType>
<restriction base="string">
<maxLength value="255"/>
<whiteSpace value="preserve"/>
</restriction>
</simpleType>
</attribute>
<attribute name="sigla" use="optional">
<simpleType>
<restriction base="string">
<maxLength value="25"/>
<whiteSpace value="preserve"/>
</restriction>
</simpleType>
</attribute>
</complexType>
<complexType name="ResponsavelType">
<attribute name="idResponsavel" type="integer" use="required"/>
<attribute name="email" use="optional">
<simpleType>
<restriction base="string">
<whiteSpace value="replace"/>
<pattern value="[^@]+@[^\.]+(\.[^@]+)+"/>
</restriction>
</simpleType>
</attribute>
<attribute name="senha" type="string" use="optional"/>
</complexType>
<complexType name="RepositorioOAILexMLType">
<attribute name="baseURL" type="anyURI" use="required"/>
</complexType>
<complexType name="PerfilType">
<attribute name="localidade" use="required">
<simpleType>
<restriction base="string">
<whiteSpace value="replace"/>
</restriction>
</simpleType>
</attribute>
<attribute name="autoridade" use="required">
<simpleType>
<restriction base="string">
<whiteSpace value="replace"/>
</restriction>
</simpleType>
</attribute>
<attribute name="tipoDocumento" use="required">
<simpleType>
<restriction base="string">
<whiteSpace value="replace"/>
</restriction>
</simpleType>
</attribute>
<attribute name="tipoPerfil" use="optional" default="T">
<simpleType>
<restriction base="string">
<enumeration value="T"/>
<enumeration value="R"/>
<enumeration value="D"/>
</restriction>
</simpleType>
</attribute>
</complexType>
</schema>
Loading…
Cancel
Save