Used 'nav' request param instead of session for storing info about the initiator of an object creation.

This commit is contained in:
Gaetan Delannay 2010-09-19 15:04:44 +02:00
parent c2a3551a94
commit 7c2b8fed11
5 changed files with 46 additions and 44 deletions

View file

@ -74,25 +74,26 @@ class AbstractMixin:
'''This method is called when a user wants to create a root object in
the application folder or an object through a reference field.'''
rq = self.REQUEST
if rq.get('initiator', None):
typeName = rq.get('type_name')
# Create the params to add to the URL we will redirect the user to
# create the object.
urlParams = {'mode':'edit', 'page':'main', 'nav':''}
if rq.get('nav', None):
# The object to create will be linked to an initiator object through
# a ref field.
rq.SESSION['initiator'] = rq.get('initiator')
rq.SESSION['initiatorField'] = rq.get('field')
rq.SESSION['initiatorTarget'] = rq.get('type_name')
if self._appy_meta_type == 'Tool':
if rq.get('initiator', None):
# This is the creation of an object linked to the tool
baseUrl = self.absolute_url()
else:
# This is the creation of a root object in the app folder
baseUrl = self.getAppFolder().absolute_url()
else:
baseUrl = self.absolute_url()
objId = self.generateUniqueId(rq.get('type_name'))
urlBack = '%s/portal_factory/%s/%s/skyn/edit' % \
(baseUrl, rq.get('type_name'), objId)
return self.goto(urlBack)
# a ref field. We create here a new navigation string with one more
# item, that will be the currently created item.
splitted = rq.get('nav').split('.')
splitted[-1] = splitted[-2] = str(int(splitted[-1])+1)
urlParams['nav'] = '.'.join(splitted)
# Determine base URL
baseUrl = self.absolute_url()
if (self._appy_meta_type == 'Tool') and not urlParams['nav']:
# This is the creation of a root object in the app folder
baseUrl = self.getAppFolder().absolute_url()
objId = self.generateUniqueId(typeName)
editUrl = '%s/portal_factory/%s/%s/skyn/edit' % \
(baseUrl, typeName, objId)
return self.goto(self.getUrl(editUrl, **urlParams))
def intraFieldValidation(self, errors, values):
'''This method performs field-specific validation for every field from
@ -145,8 +146,15 @@ class AbstractMixin:
# Go back to the consult view if the user clicked on 'Cancel'
if rq.get('buttonCancel.x', None):
if isNew:
# Go back to the Plone site (no better solution at present).
urlBack = self.portal_url.getPortalObject().absolute_url()
if rq.get('nav', ''):
# We can go back to the initiator page.
splitted = rq['nav'].split('.')
initiator = self.getTool().getObject(splitted[1])
initiatorPage = splitted[2].split(':')[1]
urlBack = initiator.getUrl(page=initiatorPage, nav='')
else:
# Go back to the Plone site (no better solution at present).
urlBack = self.portal_url.getPortalObject().absolute_url()
else:
urlBack = self.getUrl()
self.plone_utils.addPortalMessage(
@ -956,22 +964,15 @@ class AbstractMixin:
'''Every time an object is created or updated, this method updates
the Reference fields accordingly.'''
self._appy_manageRefsFromRequest()
# If the creation was initiated by another object, update the
# reference.
if created and hasattr(self.REQUEST, 'SESSION'):
# When used by the test system, no SESSION object is created.
session = self.REQUEST.SESSION
initiatorUid = session.get('initiator', None)
initiator = None
if initiatorUid:
initiatorRes = self.uid_catalog.searchResults(UID=initiatorUid)
if initiatorRes:
initiator = initiatorRes[0].getObject()
if initiator:
fieldName = session.get('initiatorField')
initiator.appy().link(fieldName, self)
# Re-initialise the session
session['initiator'] = None
rq = self.REQUEST
# If the creation was initiated by another object, update the ref.
if created and rq.get('nav', None):
# Get the initiator
splitted = rq['nav'].split('.')
initiator = self.uid_catalog.searchResults(
UID=splitted[1])[0].getObject()
fieldName = splitted[2].split(':')[1]
initiator.appy().link(fieldName, self)
def _appy_manageRefsFromRequest(self):
'''Appy manages itself some Ref fields (with link=True). So here we must
@ -1021,7 +1022,8 @@ class AbstractMixin:
in the request for the corresponding key (if existing; else, the
param will not be included in the URL at all).'''
# Define base URL if ommitted
if not base: base = self.absolute_url()
if not base:
base = '%s/skyn/%s' % (self.absolute_url(), mode)
# Manage default args
if not kwargs: kwargs = self.getUrlDefaults
if 'page' not in kwargs: kwargs['page'] = True
@ -1039,8 +1041,7 @@ class AbstractMixin:
else: params = '?' + params
else:
params = ''
# Return the full constructed URL
return '%s/skyn/%s%s' % (base, mode, params)
return '%s%s' % (base, params)
def translate(self, label, mapping={}, domain=None, default=None):
'''Translates a given p_label into p_domain with p_mapping.'''

View file

@ -52,7 +52,7 @@
tal:attributes="action python: contextObj.absolute_url()+'/skyn/do'">
<input type="hidden" name="action" value="Update"/>
<input type="hidden" name="page" tal:attributes="value page"/>
<input type="hidden" name="nav" tal:attributes="value python:request.get('nav', '')"/>
<input type="hidden" name="nav" tal:attributes="value request/nav|nothing"/>
<input type="hidden" name="is_new" tal:attributes="value contextObj/checkCreationFlag"/>
<metal:show use-macro="here/skyn/page/macros/show"/>
</form>

View file

@ -243,7 +243,6 @@
// Function that allows to generate a document from a pod template.
function generatePodDocument(contextUid, templateUid, fieldName, podFormat) {
var theForm = document.getElementsByName("podTemplateForm")[0];
alert('The form =' + theForm);
theForm.objectUid.value = contextUid;
theForm.templateUid.value = templateUid;
theForm.fieldName.value = fieldName;

View file

@ -63,9 +63,10 @@
through a reference widget. Indeed, If field was declared as "addable", we must provide
an icon for creating a new linked object (at least if multiplicities allow it).</tal:comment>
<img style="cursor:pointer" tal:condition="showPlusIcon"
tal:define="navInfo python:'ref.%s.%s:%s.%d.%d' % (contextObj.UID(), fieldName, appyType['page'], 0, totalNumber);"
tal:attributes="src string:$portal_url/skyn/plus.png;
title python: tool.translate('add_ref');
onClick python: 'href: window.location=\'%s/skyn/do?action=Create&initiator=%s&field=%s&type_name=%s\'' % (folder.absolute_url(), contextObj.UID(), fieldName, linkedPortalType)"/>
onClick python: 'href: window.location=\'%s/skyn/do?action=Create&type_name=%s&nav=%s\'' % (folder.absolute_url(), linkedPortalType, navInfo)"/>
</metal:plusIcon>
<tal:comment replace="nothing">

View file

@ -31,8 +31,9 @@ class ToolWrapper(AbstractWrapper):
'''Retrieves the object that triggered the creation of the object
being currently created (if any).'''
res = None
initiatorUid = self.session['initiator']
if initiatorUid:
rq = self.o.REQUEST
if rq.get('nav', ''):
initiatorUid = rq['nav'].split('.')[1]
res = self.o.uid_catalog(UID=initiatorUid)[0].getObject().appy()
return res