In Zope 2.7.5-final, python 2.3.5, freebsd6 with Transient Object Container settings: Data timeout: 20 Timeout resolution: 20 Maximum subobjects: 1000 and a python script that does this: req = context.REQUEST prev_order=(req.SESSION.get('order')) if prev_order == None: order=[] else: order = prev_order order.append(req.form) req.SESSION.set('order',order) my data ends up looking like this: order [{}, {}, {'foo': '1', 'bar': 'a'}, {}, {'foo': '2', 'bar': 'b'}, {'foo': '6', 'bar': 'z'}, {'foo': '1', 'bar': 'a'}] I've seen http://mail.zope.org/pipermail/zope-dev/2006-July/027890.html and am aware that related bug existed prior to 2.7.1. I've used sessions without problems before, this is the first time I attempt to store variables in containers. What am I doing wrong? Norbert
--On 18. März 2007 16:22:29 -0400 Norbert Marrale <norbert@vsmpro.com> wrote:
In Zope 2.7.5-final, python 2.3.5, freebsd6 with Transient Object Container settings: Data timeout: 20 Timeout resolution: 20 Maximum subobjects: 1000
Zope 2.7 is completely outdated and unsupported. Consider upgrading. -aj
my data ends up looking like this:
order [{}, {}, {'foo': '1', 'bar': 'a'}, {}, {'foo': '2', 'bar': 'b'}, {'foo': '6', 'bar': 'z'}, {'foo': '1', 'bar': 'a'}] What is wrong here? I mean what output is expected?
I've seen http://mail.zope.org/pipermail/zope-dev/2006-July/027890.html and am aware that related bug existed prior to 2.7.1. There is 'faster' that is alternative sessioning machinery but I don't know if it works with 2.7
I've used sessions without problems before, this is the first time I attempt to store variables in containers. What am I doing wrong? Maybe it is because of req.form assignment. Try to create copy of its values and append this to 'order' list like:
session = context.REQUEST.SESSION order = session.get('order', []) order.append({'foo':req.form['foo'], 'bar':req.form['bar']}) session.set('order',order) and see what happens now. -- Maciej Wisniowski
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Maciej Wisniowski wrote:
my data ends up looking like this:
order [{}, {}, {'foo': '1', 'bar': 'a'}, {}, {'foo': '2', 'bar': 'b'}, {'foo': '6', 'bar': 'z'}, {'foo': '1', 'bar': 'a'}] What is wrong here? I mean what output is expected?
I've seen http://mail.zope.org/pipermail/zope-dev/2006-July/027890.html and am aware that related bug existed prior to 2.7.1. There is 'faster' that is alternative sessioning machinery but I don't know if it works with 2.7
It depends on Five / Zope3: I haven't used the appropriate versions (Five 1.0.x, ZopeX3 3.0.x) with it, and don't know whether the faster product is compatible with them.
I've used sessions without problems before, this is the first time I attempt to store variables in containers. What am I doing wrong? Maybe it is because of req.form assignment. Try to create copy of its values and append this to 'order' list like:
session = context.REQUEST.SESSION order = session.get('order', []) order.append({'foo':req.form['foo'], 'bar':req.form['bar']}) session.set('order',order)
and see what happens now.
Note as well that using "mutable" Python primitives (dicts, lists) is tricky, as they don't notify *their* container (the persistent SessionDataObject) when they are mutatated. If you are using them, rather than some persistent variant, then you need to rebind them into the container. E.g.: mapping = SESSION.get('mapping') if mapping is None: mapping = {} mapping['foo'] = REQUEST.form('foo') SESSION['mapping'] = mapping # triggers persistence Tres. - -- =================================================================== Tres Seaver +1 540-429-0999 tseaver@palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF/h5I+gerLs4ltQ4RAk/VAJ9aBGGAMygmxkWtqQnG6tg6lJUzxgCgmtFh KxFeFo8D77JkP9s/V++qzhk= =HDWu -----END PGP SIGNATURE-----
Tres Seaver wrote:
Note as well that using "mutable" Python primitives (dicts, lists) is tricky, as they don't notify *their* container (the persistent SessionDataObject) when they are mutatated. If you are using them, rather than some persistent variant, then you need to rebind them into the container. E.g.:
mapping = SESSION.get('mapping') if mapping is None: mapping = {} mapping['foo'] = REQUEST.form('foo') SESSION['mapping'] = mapping # triggers persistence
Thanks Tres & Maciej! This does the trick: order = [] new_order={} prev_order=req.SESSION.get('order') if prev_order != None: for orders in prev_order: for item in orders.keys(): new_order[item]=orders[item] order.append(new_order) new_order ={} for val in req.form.keys(): new_order[val]=req.form[val] order.append(new_order) req.SESSION['order'] = order With the expected output: order [{'foo': '1', 'bar': 'a'}, {'foo': '2', 'bar': 'b'}, {'foo': '7', 'bar': 'e'}, {'foo': '6', 'bar': 'z'}, {'foo': '1', 'bar': 'a'}]
This does the trick:
order = [] new_order={} prev_order=req.SESSION.get('order')
if prev_order != None: for orders in prev_order: for item in orders.keys(): new_order[item]=orders[item] order.append(new_order) new_order ={} This seems to be hardcore ;) I mean that it should not be necessary to do such rewrite of all keys and values for dictionaries or lists taken from session. I never had to do something like that...
Isn't it working without these assignments? Just: order=req.SESSION.get('order', []) # if there is no 'order' in session # you'll simply get empty list here new_order = {} for val in req.form.keys(): new_order[val]=req.form[val] order.append(new_order) req.SESSION['order'] = order BTW. req.SESSION.set(..., ...) method is also persistence aware (according to zope book) -- Maciej Wisniowski
Maciej Wisniowski wrote:
This seems to be hardcore ;) I mean that it should not be necessary to do such rewrite of all keys and values for dictionaries or lists taken from session. I never had to do something like that...
Isn't it working without these assignments? Just:
order=req.SESSION.get('order', []) # if there is no 'order' in session # you'll simply get empty list here new_order = {} for val in req.form.keys(): new_order[val]=req.form[val] order.append(new_order) req.SESSION['order'] = order
BTW. req.SESSION.set(..., ...) method is also persistence aware (according to zope book)
I discovered this too, after posting. You are absolutely correct! Thanks again, Norbert
participants (4)
-
Andreas Jung -
Maciej Wisniowski -
Norbert Marrale -
Tres Seaver