Skip to content

Commit

Permalink
Exercício 17: Adiciona tipo de conteúdo Secretaria
Browse files Browse the repository at this point in the history
  • Loading branch information
samoel-silva committed Sep 12, 2024
1 parent 852f8f6 commit dd44f30
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/src/portal/governo/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/>

<include file="dependencies.zcml" />
<include file="permissions.zcml" />
<include file="profiles.zcml" />

<include package=".controlpanel" />
Expand Down
34 changes: 34 additions & 0 deletions backend/src/portal/governo/content/secretaria.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from plone.dexterity.content import Container
from plone.schema.email import Email
from plone.supermodel import model
from portal.governo import _
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,
)

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


@implementer(ISecretaria)
class Secretaria(Container):
"""Uma secretaria de governo."""
16 changes: 16 additions & 0 deletions backend/src/portal/governo/permissions.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:zcml="http://namespaces.zope.org/zcml"
i18n_domain="portal.governo"
>

<configure zcml:condition="installed AccessControl.security">

<permission
id="portal.governo.secretaria.add"
title="portal.governo: Add Secretaria"
/>

</configure>

</configure>
7 changes: 6 additions & 1 deletion backend/src/portal/governo/profiles/default/rolemap.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<rolemap>
<permissions>

<permission acquire="False"
name="portal.governo: Add Secretaria"
>
<role name="Manager" />
<role name="Site Administrator" />
</permission>
</permissions>
</rolemap>
6 changes: 3 additions & 3 deletions backend/src/portal/governo/profiles/default/types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<object meta_type="Plone Types Tool"
name="portal_types"
>
<!--

<object meta_type="Dexterity FTI"
name="MyType"
name="Secretaria"
/>
-->

</object>
44 changes: 44 additions & 0 deletions backend/src/portal/governo/profiles/default/types/Secretaria.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<object xmlns:i18n="http://xml.zope.org/namespaces/i18n"
meta_type="Dexterity FTI"
name="Secretaria"
i18n:domain="portal.governo"
>

<!-- Basic properties -->
<property name="title"
i18n:translate=""
>Secretaria</property>
<property name="description"
i18n:translate=""
>Uma Secretaria dentro da estrutura de governo</property>

<property name="allow_discussion">False</property>
<property name="factory">Secretaria</property>

<!-- Hierarchy control -->
<property name="filter_content_types">False</property>
<property name="allowed_content_types" />
<property name="global_allow">True</property>

<!-- Schema, class and security -->
<property name="add_permission">portal.governo.secretaria.add</property>
<property name="klass">portal.governo.content.secretaria.Secretaria</property>
<property name="schema">portal.governo.content.secretaria.ISecretaria</property>

<!-- Enabled behaviors -->
<property name="behaviors"
purge="false"
>
<element value="plone.basic" />
<element value="plone.namefromtitle" />
<element value="plone.shortname" />
<element value="plone.excludefromnavigation" />
<element value="volto.blocks" />
<element value="volto.preview_image" />
<element value="plone.constraintypes" />
<element value="plone.navigationroot" />
<element value="plone.versioning" />
</property>

</object>
64 changes: 64 additions & 0 deletions backend/tests/content/test_ct_secretaria.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from plone import api
from plone.dexterity.fti import DexterityFTI
from portal.governo.content.secretaria import Secretaria
from zope.component import createObject

import pytest


CONTENT_TYPE = "Secretaria"


@pytest.fixture
def secretaria_payload() -> dict:
"""Return a payload to create a new secretaria."""
return {
"type": "Secretaria",
"id": "casacivil",
"title": "Casa Civil",
"description": (
"A Casa Civil do Governo do Estado foi criada em janeiro de 1954,"
"pelo então governador Ernesto Dornelles"
),
"email": "[email protected]",
"telefone": "(51) 3210.4193",
}


class TestSecretaria:
@pytest.fixture(autouse=True)
def _setup(self, get_fti, portal):
self.fti = get_fti(CONTENT_TYPE)
self.portal = portal

def test_fti(self):
assert isinstance(self.fti, DexterityFTI)

def test_factory(self):
factory = self.fti.factory
obj = createObject(factory)
assert obj is not None
assert isinstance(obj, Secretaria)

@pytest.mark.parametrize(
"behavior",
[
"plone.basic",
"plone.namefromtitle",
"plone.shortname",
"plone.excludefromnavigation",
"plone.versioning",
"volto.blocks",
"plone.constraintypes",
"plone.navigationroot",
"volto.preview_image",
],
)
def test_has_behavior(self, get_behaviors, behavior):
assert behavior in get_behaviors(CONTENT_TYPE)

def test_create(self, secretaria_payload):
with api.env.adopt_roles(["Manager"]):
content = api.content.create(container=self.portal, **secretaria_payload)
assert content.portal_type == CONTENT_TYPE
assert isinstance(content, Secretaria)

0 comments on commit dd44f30

Please sign in to comment.