Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to workflows #1194

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ CHANGELOG
7.0.1 (unreleased)
------------------

- Nothing changed yet.
- Feat: Add metadata info to workflows
- Fix: Update workflow vocabulary name
- Feat: Update workflow vocabulary title attribute to use metadata
[rboixaderg]


7.0.0 (2023-12-06)
Expand Down
6 changes: 5 additions & 1 deletion guillotina/contrib/workflows/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ async def __call__(self):
return workflow
async for action_name, action in workflow_obj.available_actions(self.request):
workflow["transitions"].append(
{"@id": obj_url + "/@workflow/" + action_name, "title": action["title"]}
{
"@id": obj_url + "/@workflow/" + action_name,
"title": action["title"],
"metadata": action.get("metadata", {}),
}
)

workflow_obj = query_adapter(self.context, IWorkflowBehavior)
Expand Down
4 changes: 4 additions & 0 deletions guillotina/contrib/workflows/base/guillotina_basic.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
initial_state: private
states:
private:
metadata:
title: Private
actions:
publish:
title: Publish
Expand All @@ -18,6 +20,8 @@ states:
role: guillotina.Anonymous
permission: guillotina.SearchContent
public:
metadata:
title: Public
actions:
retire:
title: Retire
Expand Down
4 changes: 1 addition & 3 deletions guillotina/contrib/workflows/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class IWorkflowUtility(IAsyncUtility):


class IWorkflow(Interface):

initial_state = Attribute("Initial state of the workflow")


Expand All @@ -54,14 +53,13 @@ def __call__(self, context: IResource) -> Optional[str]:


class IWorkflowBehavior(Interface):

index_field("review_state", store=True, type="keyword")
review_state = schema.Choice(
readonly=True,
title="Workflow review state",
required=False,
defaultFactory=DefaultReviewState(),
source="worklow_states",
source="workflow_states",
)

history = schema.List(
Expand Down
5 changes: 4 additions & 1 deletion guillotina/contrib/workflows/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from guillotina.interfaces import IResource


@configure.vocabulary(name="worklow_states")
@configure.vocabulary(name="workflow_states")
class WorkflowVocabulary:
def __init__(self, context):
self.context = context
Expand Down Expand Up @@ -35,6 +35,9 @@ def __len__(self):

def getTerm(self, value):
if value in self.states:
metadata = self.states[value].get("metadata", None)
if metadata:
return metadata
return value
else:
raise KeyError("No valid state")
118 changes: 117 additions & 1 deletion guillotina/tests/workflows/test_workflow_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,110 @@

pytestmark = pytest.mark.asyncio

guillotina_basic_with_translations = {
"initial_state": "private",
"states": {
"private": {
"metadata": {
"title": "Private",
"translated_title": {
"en": "Private",
"ca": "Privat",
"es": "Privado",
},
},
"actions": {
"publish": {
"title": "Publish",
"metadata": {
"translated_title": {
"en": "Publish",
"ca": "Publicar",
"es": "Publicar",
},
},
"to": "public",
"check_permission": "guillotina.ReviewContent",
}
},
"set_permission": {
"roleperm": [
{
"setting": "Deny",
"role": "guillotina.Anonymous",
"permission": "guillotina.ViewContent",
},
{
"setting": "Deny",
"role": "guillotina.Anonymous",
"permission": "guillotina.AccessContent",
},
{
"setting": "Deny",
"role": "guillotina.Anonymous",
"permission": "guillotina.SearchContent",
},
]
},
},
"public": {
"metadata": {
"title": "Public",
"translated_title": {
"en": "Public",
"ca": "Públic",
"es": "Público",
},
},
"actions": {
"retire": {
"title": "Retire",
"to": "private",
"check_permission": "guillotina.ReviewContent",
},
},
"set_permission": {
"roleperm": [
{
"setting": "AllowSingle",
"role": "guillotina.Anonymous",
"permission": "guillotina.ViewContent",
},
{
"setting": "AllowSingle",
"role": "guillotina.Anonymous",
"permission": "guillotina.AccessContent",
},
{
"setting": "AllowSingle",
"role": "guillotina.Anonymous",
"permission": "guillotina.SearchContent",
},
]
},
},
},
}


@pytest.mark.app_settings(
{
"applications": ["guillotina", "guillotina.contrib.workflows"],
"workflows_content": {"guillotina.interfaces.IContainer": "guillotina_basic"},
"workflows_content": {"guillotina.interfaces.IContainer": "guillotina_basic_with_translations"},
"workflows": {"guillotina_basic_with_translations": guillotina_basic_with_translations},
}
)
async def test_workflow_basic(container_requester):
async with container_requester as requester:
response, _ = await requester("GET", "/db/guillotina/@workflow")
assert response["transitions"][0]["title"] == "Publish"
assert response["transitions"][0]["metadata"] == {
"translated_title": {
"en": "Publish",
"ca": "Publicar",
"es": "Publicar",
}
}

response, _ = await requester("GET", "/db/guillotina")
assert response["review_state"] == "private"
Expand All @@ -38,3 +131,26 @@ async def test_workflow_basic(container_requester):

response, status = await requester("GET", "/db/guillotina", token=None)
assert status == 200

response, status = await requester("GET", "/db/guillotina/@vocabularies/workflow_states")
assert status == 200
assert response == {
"@id": "http://localhost/db/guillotina/@vocabularies/workflow_states",
"items": [
{
"title": {
"title": "Private",
"translated_title": {"en": "Private", "ca": "Privat", "es": "Privado"},
},
"token": "private",
},
{
"title": {
"title": "Public",
"translated_title": {"en": "Public", "ca": "Públic", "es": "Público"},
},
"token": "public",
},
],
"items_total": 2,
}
Loading