Skip to content

Commit

Permalink
When creating an OiRa tool from a template, reset the UIDs to avoid c…
Browse files Browse the repository at this point in the history
…omplaints from the catalog
  • Loading branch information
ale-rt committed May 21, 2024
1 parent cf3b983 commit f963140
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Changelog
- CSV download of similar title details.
Ref: scrum-2198

- When creating an OiRa tool from a template, reset the UIDs to avoid complaints from the catalog
[ale-rt]

- Add registry setting `euphorie.notifications__allow_user_settings` to allow users to change their notification settings.
The default is set to `True` to allow users to do changes on their own.
This can be prevented if internal policies require so by changing this setting to `False`.
Expand Down
38 changes: 31 additions & 7 deletions src/euphorie/content/browser/surveygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
from plone.dexterity.browser.add import DefaultAddForm
from plone.dexterity.browser.add import DefaultAddView
from plone.dexterity.utils import createContentInContainer
from plone.uuid.handlers import addAttributeUUID
from Products.CMFCore.interfaces import ISiteRoot
from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from urllib.parse import urlencode
from ZODB.POSException import ConflictError
from zope.component import getUtility
from zope.event import notify
from zope.lifecycleevent import ObjectCopiedEvent

import datetime
import logging
Expand Down Expand Up @@ -122,21 +124,43 @@ def buildSurveyTree(self):
countries = [c for c in countries if c["sectors"]]
return countries

def get_template_copy(self, container, template):
"""Get's the copy for the template
The template is a survey that will be copied inside the newly
created survey group.
"""
copy = template._getCopy(container)

# Reset the copied tree UIDs
# We use a fake event to reuse the subscriber from plone.uuid that
# resets the UIDs on copy
fake_copy_event = ObjectCopiedEvent(None, None)
addAttributeUUID(copy, fake_copy_event)
for id, item in copy.ZopeFind(copy, search_sub=1):
addAttributeUUID(item, fake_copy_event)

# Set a time based id
today = datetime.date.today()
copy.id = today.isoformat()

# Set the title to the copy
title = self.request.locale.dates.getFormatter("date", length="long").format(
datetime.date.today()
)
copy.title = title

return copy

def copyTemplate(self, source, target):
target = self.context[target.id] # Acquisition-wrap
try:
source._notifyOfCopyTo(target, op=0)
except ConflictError:
raise

copy = source._getCopy(target)
copy = self.get_template_copy(target, source)

today = datetime.date.today()
title = self.request.locale.dates.getFormatter("date", length="long").format(
datetime.date.today()
)
copy.id = today.isoformat()
copy.title = title
source_algorithm = aq_parent(source).evaluation_algorithm
target_algorithm = self.request.form.get(
"form.widgets.evaluation_algorithm", [source_algorithm]
Expand Down
12 changes: 12 additions & 0 deletions src/euphorie/content/tests/test_functional_surveygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ def createModule(self):
module = self._create(survey, "euphorie.module", "module")
return module

def testCopyResetsUIDs(self):
module = self.createModule()
self._create(module, "euphorie.risk", "one", title="one")
request = module.REQUEST
survey = aq_parent(module)
container = self.portal.sectors.nl.sector
target = self._create(container, "euphorie.surveygroup", "target")
copy = AddForm(container, request).copyTemplate(survey, target)
self.assertNotEqual(survey.UID(), copy.UID())
self.assertNotEqual(survey.module.UID(), copy.module.UID())
self.assertNotEqual(survey.module.one.UID(), copy.module.one.UID())

def testCopyPreservesOrder(self):
original_order = [
"one",
Expand Down

0 comments on commit f963140

Please sign in to comment.