问题
I have created a custom form to add fields to the @@personal-information form. I found that the login name can be changed by using set_own_login_name()
which is provided by Products.CMFPlone.utils
In my form adapter, I have something like this:
def get_username(self):
prop = self.context.getUserName()
return prop
def set_username(self, value):
id = self.context.getProperty('id', '')
value = id if (value == None) else value
return utils.set_own_login_name(self.context, value)
username = property(get_username, set_username)
The login_name is being changed, but after clicking the "Save" button on the form, the form still shows the original value. Upon refreshing the page (without resubmitting the form) it then shows the correct value.
So how can I force get_username()
to re-index the property or cause the new login_name to be returned without a page refresh?
UPDATE: I discovered that I can redirect and it fixes the issue, but I am hoping someone has a more elegant solution. It seems like there ought to be a more straight-forward way of updating the property. Here is my workaround:
data = {}
def get_username(self):
prop = self.context.getUserName()
self.data = {'old': prop, 'new': prop}
return prop
def set_username(self, value):
id = self.context.getProperty('id', '')
value = id if (value == None) else value
self.data['new'] = value
return utils.set_own_login_name(self.context, value)
username = property(get_username, set_username)
def __del__(self):
if (self.data['new'] != self.data['old']):
portalUrl = getToolByName(self.context, 'portal_url')()
self.context.REQUEST.response.redirect(("%s/@@personal-information") % portalUrl)
来源:https://stackoverflow.com/questions/8734872/plone-4-changing-the-login-name-on-the-personal-information-form