Skip to content

Commit

Permalink
Merge pull request #265 from euphorie/maurits-rest-api-service-tool-v…
Browse files Browse the repository at this point in the history
…ersions

Add REST API service @tool-versions.
  • Loading branch information
ale-rt authored Jul 22, 2024
2 parents cd0b759 + d56f3d9 commit 2abef93
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/osha/oira/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<include package=".ploneintranet" />
<include package=".upgrade" />
<include package=".statistics" />
<include package=".services" />

<!-- Vocabularies -->
<utility
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions src/osha/oira/services/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:plone="http://namespaces.plone.org/plone"
>

<plone:service
method="GET"
factory=".surveys.ToolVersionsGet"
for="plone.dexterity.interfaces.IDexterityContainer"
permission="zope2.View"
layer="osha.oira.interfaces.IOSHAContentSkinLayer"
name="@tool-versions"
/>

</configure>
83 changes: 83 additions & 0 deletions src/osha/oira/services/surveys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from Acquisition import aq_chain
from Acquisition import aq_inner
from plone import api
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.services import Service


class ToolVersionsGet(Service):
"""Get info from the containing tool and its versions.
From the Quaive side we request this.
Then we know if we are on a path that is within a survey group,
and we can show info about the versions, if we want.
This is meant to show information about versions like on the left
in the prototype:
https://proto.quaivecloud.com/tools/audio-visual-productions/edit
We need this information in the sidebar.
And we don't yet have a way of knowing what the "remote" portal_type
is of the path we are viewing in Quaive, so we don't know if we are
currently viewing a country or survey group or survey or risk.
So from the Quaive side we have no idea if we should show this version
information. An extra REST API service then seems a good way to me.
"""

def get_survey_info(self, survey):
return {
"@id": survey.absolute_url(),
"id": survey.id,
"title": survey.Title(),
"review_state": api.content.get_state(obj=survey),
"created": json_compatible(survey.created()),
"modified": json_compatible(survey.modified()),
"published": json_compatible(survey.published),
}

def reply(self):
obj = aq_inner(self.context)
# First gather basic info about the context and request.
result = {
"@id": f"{obj.absolute_url()}/@tool-versions",
# "id": obj.id,
"@type": obj.portal_type,
}
survey = None
surveygroup = None
# Find nearest survey and survey group.
# One of these might be the current object as well.
for parent in aq_chain(obj):
portal_type = getattr(parent, "portal_type", "")
if not portal_type:
break
if portal_type == "euphorie.survey":
survey = parent
elif portal_type == "euphorie.surveygroup":
surveygroup = parent

if not surveygroup:
# We are outside of a survey group, nothing to see here.
return result

result["surveygroup"] = {
"@id": surveygroup.absolute_url(),
"title": surveygroup.Title(),
}
if survey:
current_survey_id = survey.id
result["survey"] = self.get_survey_info(survey)
else:
current_survey_id = None
items = []
# TODO Can we know which survey is the main currently published one?
# When publishing a survey you get this question: "Are you sure you want
# to publish this OiRA Tool version? This will replace the current version."
# But afterwards both the old and new version have review_state 'published'.
for survey in surveygroup.contentValues():
if survey.id == current_survey_id:
# This is the survey on which path we are.
continue
items.append(self.get_survey_info(survey))
result["items"] = items
return result

0 comments on commit 2abef93

Please sign in to comment.