Skip to content

Commit

Permalink
Exercício 30
Browse files Browse the repository at this point in the history
  • Loading branch information
jadirj committed Oct 14, 2024
1 parent 17e5974 commit e1d5a03
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 64 deletions.
Empty file.
20 changes: 20 additions & 0 deletions backend/src/trepr/intranet/behaviors/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:plone="http://namespaces.plone.org/plone"
>

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

<plone:behavior
name="trepr.intranet.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/trepr/intranet/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 trepr.intranet import _
from trepr.intranet.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/trepr/intranet/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 trepr.intranet 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="trepr.intranet.vocabulary.estados",
required=False,
)
cep = schema.TextLine(
title=_("CEP"),
required=False,
default="",
)
1 change: 1 addition & 0 deletions backend/src/trepr/intranet/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<include file="permissions.zcml" />

<include package=".controlpanel" />
<include package=".behaviors" />
<include package=".indexers" />
<include package=".serializers" />
<include package=".vocabularies" />
Expand Down
62 changes: 0 additions & 62 deletions backend/src/trepr/intranet/content/area.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,11 @@
from plone.dexterity.content import Container
from plone.schema.email import Email
from plone.supermodel import model
from trepr.intranet import _
from trepr.intranet.utils import validadores
from zope import schema
from zope.interface import implementer


class IArea(model.Schema):
"""Definição de uma Área."""

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"),
required=False,
)

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

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

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

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


@implementer(IArea)
class Area(Container):
Expand Down
2 changes: 1 addition & 1 deletion backend/src/trepr/intranet/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>1003</version>
<version>1004</version>
<dependencies>
<dependency>profile-plone.volto:default</dependency>
</dependencies>
Expand Down
2 changes: 2 additions & 0 deletions backend/src/trepr/intranet/profiles/default/types/Area.xml
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="trepr.intranet.behavior.contato" />
<element value="trepr.intranet.behavior.endereco" />
<element value="volto.blocks" />
<element value="volto.preview_image" />
<element value="plone.constraintypes" />
Expand Down
11 changes: 11 additions & 0 deletions backend/src/trepr/intranet/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@
/>
</genericsetup:upgradeSteps>

<genericsetup:upgradeSteps
profile="trepr.intranet:default"
source="1003"
destination="1004"
>
<genericsetup:upgradeDepends
title="Adiciona behaviors ao tipo Área"
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 trepr.intranet import PACKAGE_NAME

import pytest


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


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
31 changes: 31 additions & 0 deletions backend/tests/behaviors/test_behavior_endereco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from trepr.intranet import PACKAGE_NAME

import pytest


@pytest.fixture
def payload() -> dict:
return {
"endereco": "R. João Parolin, 224",
"complemento": "Prado Velho",
"cidade": "Curitiba",
"estado": "PR",
"cep": "80220-902",
}


class TestBehaviorEndereco:
name: str = f"{PACKAGE_NAME}.behavior.endereco"

@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_content_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def test_factory(self):
"plone.shortname",
"plone.excludefromnavigation",
"plone.versioning",
"trepr.intranet.behavior.contato",
"trepr.intranet.behavior.endereco",
"volto.blocks",
"plone.constraintypes",
"volto.preview_image",
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") == "1003"
assert profile_last_version(f"{PACKAGE_NAME}:default") == "1004"

0 comments on commit e1d5a03

Please sign in to comment.