On Monday 24 November 2008 14:13:37 Vortexmind wrote:
> > Where is what stored? Are the fields in the DB prefixed with a u?
>
> ** model.py
> class Profile(DeclarativeBase):
> ...
> profile_data = Column(UnicodeText)
>
> ** postgres
> CREATE TABLE profile
> (
> ...
> profile_data text
> )
>
> SAMPLE RECORD (as is):
> {u'tempCounter': 1, u'profile_name': u'DefaultProfile', u'containers':
> [{u'tempCounter': 1, u'desktops': [{u'tempCounter': 0, u'panels':
> []}]}]}
>
> ** controller
> @expose('json')
> def load_profile(self,id):
> db_profile = DBSession.query(Profile.profile_data) ...
> pprint.pprint(db_profile.profile_data)
>
> pprint.pprint OUTPUT FOR SAMPLE RECORD:
> u"{u'tempCounter': 1, u'profile_name': u'DefaultProfile',
> u'containers': [{u'tempCounter': 1, u'desktops': [{u'tempCounter': 0,
> u'panels': []}]}]}"
>
> It seems that at store time the string is not being treated correctly
> as unicode, and stored like a normal string with u everywhere ...
> isn't it?
> From now on, I can return dict(db_profile.profile_data) to my
> javascript application ...
>
> This is how I populate the database (another controller metod)
>
> new_profile = Profile()
> DBSession.add(new_profile)
> new_profile.user = query.user_id
> new_profile.profile_name = item[u'profile_name']
> new_profile.profile_data = item
>
> Where item is the same JSON string as above, but without the u' ...
>
> Maybe I can strip them out with a string replace or regexp
> substitution ... but still I'm garbled up on this :(
Well, this is a classical garbage-in-garbage-out-problem.
You already *store* json-like data in the database, complete with
unicode-literal-prefixes.
The jsonification in your controller only sees the whole record as one
string - and all it does is to enclose it into quotation marks.
So whatever is responsible for storing the profile data is the culprit.
There are several solutions to your problem. The easiest imho would be to
store the data as pickle. That would make reading it return a proper python
dictionary, that gets jsonified without a hitch.
The more elaborated and "nicer" solution might be to make the profile data an
actualy SQL object hierarchy, of keys and values and such, storing the data
structured instead of one binary lump.
What you *shouldn't* do is to use eval to make the stored string a
python-object. Because then one could potentially attack your system by
passing in evaluable python expressions that format your harddrive or some
such.
Bottomline: the json-decorator isn't the problem.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---