Skip to content

Commit

Permalink
Add the @@survey-group-image view
Browse files Browse the repository at this point in the history
This view is necessary to retrieve the image for the survey group in the
best possible way, given the constraints.

Survey groups are assigned via a script across all survey versions (see
syslabcom/scrum#2552 for details).

The view redirects to the first meaningful image it can find. While this
approach helps achieve the desired functionality, it is more of a
workaround and isn't highly efficient. One downside is that browser
caching may retain the image longer than intended, potentially affecting
freshness.

This solution is a trade-off between performance and practicality.
  • Loading branch information
ale-rt committed Sep 6, 2024
1 parent dd6450a commit 8ac3319
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/osha/oira/content/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
xmlns:browser="http://namespaces.zope.org/browser"
>

<browser:resourceDirectory
name="osha.oira.content"
directory="resources"
layer="osha.oira.interfaces.IOSHAContentSkinLayer"
/>

<browser:page
name="manage-ldap-users"
for="euphorie.content.country.ICountry"
Expand Down Expand Up @@ -65,6 +71,14 @@
/>
</configure>

<browser:page
name="survey-group-image"
for="euphorie.content.surveygroup.ISurveyGroup"
class=".surveygroup.SurveyGroupImage"
permission="zope2.View"
layer="osha.oira.interfaces.IOSHAContentSkinLayer"
/>

<!-- Sector -->
<adapter
name="euphorie.sector"
Expand Down
6 changes: 6 additions & 0 deletions src/osha/oira/content/browser/resources/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions src/osha/oira/content/browser/surveygroup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from Acquisition import aq_base
from Acquisition import aq_parent
from euphorie.content import MessageFactory as _
from plone import api
from plonetheme.nuplone.skin import actions
from Products.Five import BrowserView


class Delete(actions.Delete):
Expand Down Expand Up @@ -37,3 +39,49 @@ def verify(self, container, context):
self.request.response.redirect(context.absolute_url())
return False
return True


class SurveyGroupImage(BrowserView):
"""Return the image of the surveygroup."""

@property
def surveys(self):
"""Iterator over the surveys contained in the surveygroup.
The published survey (if it exists) comes first.
"""
published = self.context.published
if published:
published_obj = self.context.get(published)
if published_obj:
yield published_obj

for obj in self.context.listFolderContents({"portal_type": "Survey"}):
if obj.getId() != published:
yield obj

def get_obj_image_url(self, obj):
"""Return the URL of the image of the given object.
The image is set by a cron script,
see e.g. https://github.com/syslabcom/scrum/issues/2552.
This might change in the future.
"""
if obj and aq_base(obj.image):
return f"{obj.absolute_url()}/@@images/image"

def __call__(self):
"""Check all the surveys, if they have an image, redirect to that image.
Return a fallback if it does not exist.
"""
for survey in self.surveys:
image_url = self.get_obj_image_url(survey)
if image_url:
return self.request.response.redirect(image_url)

self.request.response.redirect(
f"{api.portal.get().absolute_url()}"
f"/++resource++osha.oira.content/clipboard.svg"
)

0 comments on commit 8ac3319

Please sign in to comment.