I have an interface called IReportSettings for a registry key that has a tuple, which stores PersistantObjects that use an interface IUserSetting, which is implemented by an object type called UserSetting. A factory adaptor for IUserSetting and UserSetting is registered with registerFactoryAdapter. When I try to set the registry key's IReportSettings with a tuple of UserSettings, I get an error:
WrongContainedType: ([WrongContainedType([WrongType('uname', <type 'unicode'>,'user_name')],'')],'value')
Here is some of my code:
class PersistentObject(PersistentField, schema.Object):
pass
class IUserSetting(Interface):
user_name = schema.TextLine(title=u"User",
required=True,
default=u"",
)
field_a= schema.List(title=u"Field A",
value_type=schema.Choice(vocabulary=u'my.product.vocabularies.SomeVocabulary'),
required=False,
default=None
)
field_b = schema.TextLine(title=u"Field B",
required=False,
default = u"",
)
.
.
class UserSetting(object):
implements(IUserSetting)
def __init__(self, user_name=u'', field_a=None, field_b=u'', ..):
self.user_name = user_name
self.field_a = field_a
if field_a=None:
self.field_a = []
self.field_b = field_b
..
registerFactoryAdapter(IUserSetting, UserSetting)
class IReportSettings
settings = schema.Tuple(
title=u"User settings for a Report",
value_type=PersistentObject(
IUserSetting,
title=u"User Setting",
description=u"a Report Setting"
),
required=False,
default=(),
missing_value=(),
)
In a form class:
def saveUI(self, data):
user_name = api.user.get_current().id
field_a = data['field_a']
field_b = data['field_b']
.
.
registry_util = queryUtility(IRegistry)
user_settings = registry_util.forInterface(IReportSettings,check=False)
already_exists = False
#Iterate through user_settings.settings and check if a setting for the user already exists, update if it exists or add new if it doesn't exist
if already_exists == False:
#Add new UserSetting - Example Data
new_setting = UserSetting(user_name=user_name, field_a= field_a, field_b=field_b)
user_settings.settings += (new_setting,)
new_setting is the correct type from when I printed it. What data am I passing in could possibly be wrong?
In my registry.xml file, I added:
<records interface="my.package.user_settings.IReportSettings" purge="False"/>
Also, My code is based on this article: http://blog.redturtle.it/plone.app.registry-how-to-use-it-and-love-it
I'm the author of the article but I think that your problem is not realted to it.
Data stored inside your user_name
must be a unicode but calling api.user.get_current().id
will (probably) returns a 8bit string.
So try to change the line to:
user_name = api.user.get_current().id.decode('utf-8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With