Re: [Modeling-users] EditingContext and Zope Sessions
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2003-08-31 23:10:48
|
Hi, frobni barka <fro...@ya...> wrote: > Hello, >=20 > According to the documentation > ZEditingContextSessioning allows to lazily create an > EditingContext on a per-session basis. >=20 > Is it possible to somehow (automatically) bind the > EditingContext to a Zope Transaction (e.g. a Web > Request). Yes it is --see below. > What is the advantage of binding the EditingContext to > a Session instead of a transaction? - e.g. Zope SQL > Methods commit the (SQL) Transaction on the end of the > Zope Transaction and I never had any problems with > them. The EC is delivered as part of the session. This allows you to decide where you want to save the changes. This may --or may not-- be at the end of a zope transaction. A typical situation where both are decorrelated is when you needs two (html/zope) pages or more to make changes.=20 With an example this is clearer: suppose you have an address book application, and that Persons objects must have an valid Address (i.e. the relationship between Person and Address is mandatory). Suppose now that you want to have one page for filling the person's properties (first & last name, etc.), then a second one to fill his/her address fields. Page 1: Person -> validate -> Page 2: Address -> validate -> save changes Here you don't want to save your changes when page 1 validates its changes (this is the end of a zope transaction), but after page 2 validates. Hence, in this case, you'll design your product to saveChanges() when page 2 is validated. Moreover, if you send a saveChanges() when page 1 validates, you'll get an error, because the person inserted in the EC has no address while this is required by the model: the EC won't let you save objects in an inconsistent state. Back to your problem now: what you're asking for is not in-the-box, because I never needed it :) I've quickly made a patch that you'll find at the end of the message, which enables automatic saveChanges(). Apply it to ZEditingContextSessioning/__init__.py patch -p0 < __init__.py.patch and then all your SESSION.defaultEditingContext() will saveChanges() when a zope transaction ends (just like ZSQL methods do). I'd be glad to hear from you if you use it successfully --I'll then probably add this as an option in the product itself. Regards, -- S=E9bastien. Here is the patch __init__.py.patch: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/modeling/ZModeling/ZEditingContextSessioning/__init__.py= ,v retrieving revision 1.3 diff -u -r1.3 __init__.py --- __init__.py 22 Apr 2003 09:36:50 -0000 1.3 +++ __init__.py 31 Aug 2003 22:52:33 -0000 @@ -36,6 +36,31 @@ from Products.Transience.TransientObject import TransientObject import zLOG =20 +from Shared.DC.ZRDB.TM import TM +class ECProxy(TM): + def __init__(self, ec): + zLOG.LOG("ECProxy", zLOG.INFO, '__init__') + self.ec=3Dec + self._register() +=20=20=20=20 + def _abort(self): + zLOG.LOG("ECProxy", zLOG.INFO, '_abort') + pass + + def _finish(self): + zLOG.LOG("ECProxy", zLOG.INFO, '_finish') + self.ec.saveChanges() + + def _begin(self): + zLOG.LOG("ECProxy", zLOG.INFO, '_begin') + pass + + def __getattr__(self, n): + zLOG.LOG("ECProxy", zLOG.INFO, "__getattr__") + if hasattr(self.ec, n): + return getattr(self.ec, n) + raise ValueError +=20=20 ## Implementation note: ## session.token was formerly used instead of session.id ## --> BAD IDEA @@ -64,7 +89,8 @@ """ Returns the EditingContext bound to 'session' """ - return EditingContextSessioning.getEditingContext(session.id) + zLOG.LOG("Products.ZEditingContextSessioning", zLOG.DEBUG, 'defaultEC()') + return ECProxy(EditingContextSessioning.getEditingContext(session.id)) =20 =20 def initialize(context): |