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

View file

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

View file

@ -63,9 +63,10 @@
through a reference widget. Indeed, If field was declared as "addable", we must provide 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> an icon for creating a new linked object (at least if multiplicities allow it).</tal:comment>
<img style="cursor:pointer" tal:condition="showPlusIcon" <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; tal:attributes="src string:$portal_url/skyn/plus.png;
title python: tool.translate('add_ref'); 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> </metal:plusIcon>
<tal:comment replace="nothing"> <tal:comment replace="nothing">

View file

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