[Zope] Re: losing random session data

2007-03-19 Thread Norbert Marrale

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

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Re: losing random session data

2007-03-19 Thread Maciej Wisniowski
> 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
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] Re: losing random session data

2007-03-19 Thread Norbert Marrale

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'}]


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] Re: losing random session data

2007-03-18 Thread Tres Seaver
-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  [EMAIL PROTECTED]
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-

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )