Skip to content

Commit

Permalink
Exercicio 26: Cria comportamento endereço e econtato
Browse files Browse the repository at this point in the history
  • Loading branch information
samoel-silva committed Sep 17, 2024
1 parent 79a9928 commit 99805a8
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 74 deletions.
Empty file.
22 changes: 22 additions & 0 deletions backend/src/portal/governo/behaviors/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:plone="http://namespaces.plone.org/plone"
>

<plone:behavior
name="portal.governo.behavior.contato"
title="Informações de Contato"
description="Adiciona campos de informações de contato."
provides=".contato.IContato"
/>


<plone:behavior
name="portal.governo.behavior.endereco"
title="Informações de Endereço"
description="Adiciona campos de informações de endereço."
provides=".endereco.IEndereco"
/>


</configure>
33 changes: 33 additions & 0 deletions backend/src/portal/governo/behaviors/contato.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from plone.autoform.interfaces import IFormFieldProvider
from plone.schema.email import Email
from plone.supermodel import model
from portal.governo import _
from portal.governo.utils import validadores
from zope import schema
from zope.interface import provider


@provider(IFormFieldProvider)
class IContato(model.Schema):
"""Provê campos de contato."""

model.fieldset(
"contato",
_("Contato"),
fields=[
"email",
"telefone",
],
)
email = Email(
title=_("Email"),
required=True,
constraint=validadores.is_valid_email,
)

telefone = schema.TextLine(
title=_("Telefone"),
description=_("Informe o telefone de contato"),
required=False,
constraint=validadores.is_valid_telefone,
)
48 changes: 48 additions & 0 deletions backend/src/portal/governo/behaviors/endereco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from plone.autoform.interfaces import IFormFieldProvider
from plone.supermodel import model
from portal.governo import _
from zope import schema
from zope.interface import provider


@provider(IFormFieldProvider)
class IEndereco(model.Schema):
"""Provê campos de endereço."""

model.fieldset(
"endereco",
_("Endereço"),
fields=[
"endereco",
"complemento",
"cidade",
"estado",
"cep",
],
)
endereco = schema.TextLine(
title=_("Endereço"),
required=False,
default="",
)
complemento = schema.TextLine(
title=_("Complemento"),
description=_("Ex. Anexo, Sala"),
required=False,
default="",
)
cidade = schema.TextLine(
title=_("Cidade"),
required=False,
default="",
)
estado = schema.Choice(
title=_("Estado"),
vocabulary="portal.governo.vocabulary.estados",
required=False,
)
cep = schema.TextLine(
title=_("CEP"),
required=False,
default="",
)
3 changes: 2 additions & 1 deletion backend/src/portal/governo/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
<include file="permissions.zcml" />
<include file="profiles.zcml" />

<include package=".behaviors" />
<include package=".controlpanel" />
<include package=".indexers" />
<include package=".serializers" />
<include package=".vocabularies" />
<include package=".subscribers" />
<include package=".vocabularies" />

</configure>
67 changes: 0 additions & 67 deletions backend/src/portal/governo/content/secretaria.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,11 @@
from plone.dexterity.content import Container
from plone.schema.email import Email
from plone.supermodel import model
from portal.governo import _
from portal.governo.utils import validadores
from zope import schema
from zope.interface import implementer


class ISecretaria(model.Schema):
"""Definição de uma Secretaria de governo."""

model.fieldset(
"contato",
_("Contato"),
fields=[
"email",
"telefone",
],
)
email = Email(
title=_("Email"),
required=True,
constraint=validadores.is_valid_email,
)

telefone = schema.TextLine(
title=_("Telefone"),
description=_("Informe o telefone de contato"),
required=False,
constraint=validadores.is_valid_telefone,
)

model.fieldset(
"endereco",
_("Endereço"),
fields=[
"endereco",
"complemento",
"cidade",
"estado",
"cep",
],
)
endereco = schema.TextLine(
title=_("Endereço"),
description=_("Informe o endereço"),
required=False,
default="",
)

complemento = schema.TextLine(
title=_("Complemento"),
description=_("Informe o complemento"),
required=False,
)

cidade = schema.TextLine(
title=_("Cidade"),
description=_("Informe a cidade"),
required=False,
)

estado = schema.Choice(
title=_("Estado"),
vocabulary="portal.governo.vocabulary.estados",
required=False,
)

cep = schema.TextLine(
title=_("CEP"),
description=_("Informe o CEP"),
required=False,
)


@implementer(ISecretaria)
class Secretaria(Container):
Expand Down
2 changes: 1 addition & 1 deletion backend/src/portal/governo/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>1000</version>
<version>1001</version>
<dependencies>
<dependency>profile-plone.volto:default</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<element value="plone.namefromtitle" />
<element value="plone.shortname" />
<element value="plone.excludefromnavigation" />
<element value="portal.governo.behavior.contato" />
<element value="portal.governo.behavior.endereco" />
<element value="volto.blocks" />
<element value="volto.preview_image" />
<element value="plone.constraintypes" />
Expand Down
6 changes: 2 additions & 4 deletions backend/src/portal/governo/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
>

<!-- Example upgrade step
<genericsetup:upgradeSteps
profile="portal.governo:default"
source="1000"
destination="1001"
>
<genericsetup:upgradeDepends
title="Update registry value"
import_steps="plone.app.registry"
title="Altera comportamentos do tipo Secretaria"
import_steps="typeinfo"
/>
</genericsetup:upgradeSteps>
-->

</configure>
80 changes: 80 additions & 0 deletions backend/tests/behaviors/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.dexterity.fti import DexterityFTI
from plone.restapi.testing import RelativeSession
from zope.component.hooks import setSite

import pytest
import transaction


@pytest.fixture()
def request_factory(portal):
def factory():
url = portal.absolute_url()
api_session = RelativeSession(url)
api_session.headers.update({"Accept": "application/json"})
return api_session

return factory


@pytest.fixture()
def anon_request(request_factory):
return request_factory()


@pytest.fixture()
def manager_request(request_factory):
request = request_factory()
request.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
yield request
request.auth = ()


@pytest.fixture
def portal(functional):
return functional["portal"]


@pytest.fixture
def portal_integration(integration):
return integration["portal"]


@pytest.fixture
def portal_factory(functional):
def func(behavior: str):
portal = functional["portal"]
setRoles(portal, TEST_USER_ID, ["Manager"])
fti = DexterityFTI("DummyType")
fti.behaviors = (behavior,)
portal.portal_types._setObject("DummyType", fti)
setSite(portal)
transaction.commit()
return portal

return func


@pytest.fixture
def dummy_type_schema(manager_request):
def func():
url = "/@types/DummyType"
response = manager_request.get(url)
data = response.json()
return data

return func


@pytest.fixture
def create_dummy_content(manager_request):
def func(payload: dict):
payload["@type"] = "DummyType"
response = manager_request.post("/", json=payload)
return response

return func
28 changes: 28 additions & 0 deletions backend/tests/behaviors/test_behavior_contato.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from portal.governo import PACKAGE_NAME

import pytest


@pytest.fixture
def payload() -> dict:
return {
"email": "[email protected]",
"telefone": "51999528312",
}


class TestBehaviorContato:
name: str = f"{PACKAGE_NAME}.behavior.contato"

@pytest.fixture(autouse=True)
def _setup(self, portal_factory, dummy_type_schema):
self.portal = portal_factory(behavior=self.name)
self.schema = dummy_type_schema()

def test_behavior_schema(self, payload):
for key in payload:
assert key in self.schema["properties"]

def test_behavior_data(self, payload, create_dummy_content):
response = create_dummy_content(payload)
assert response.status_code == 201
2 changes: 2 additions & 0 deletions backend/tests/content/test_ct_secretaria.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def test_factory(self):
"plone.constraintypes",
"plone.navigationroot",
"volto.preview_image",
"portal.governo.behavior.contato",
"portal.governo.behavior.endereco",
],
)
def test_has_behavior(self, get_behaviors, behavior):
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/setup/test_setup_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def test_browserlayer(self, browser_layers):

def test_latest_version(self, profile_last_version):
"""Test latest version of default profile."""
assert profile_last_version(f"{PACKAGE_NAME}:default") == "1000"
assert profile_last_version(f"{PACKAGE_NAME}:default") == "1001"

0 comments on commit 99805a8

Please sign in to comment.