I am creating an external method that makes a folder, creates a dtml method in that folder, and then sends an email message. If any of these operations fail, I'd like to "roll back" to the state before the method was called. I think I heard that zope has transactions of some kind, and I'm making mods to the ZODB in my external method, so is there a magic piece of code that would roll everything back? Thanks!
Bruce Eckel wrote:
I am creating an external method that makes a folder, creates a dtml method in that folder, and then sends an email message. If any of these operations fail, I'd like to "roll back" to the state before the method was called. I think I heard that zope has transactions of some kind, and I'm making mods to the ZODB in my external method, so is there a magic piece of code that would roll everything back?
The following long and lengthy piece of code should do it: raise "oh dear, something bad happened" ...now mind you get that magic string right ;-) Chris
Bruce Eckel writes:
I am creating an external method that makes a folder, creates a dtml method in that folder, and then sends an email message. If any of these operations fail, I'd like to "roll back" to the state before the method was called. I think I heard that zope has transactions of some kind, and I'm making mods to the ZODB in my external method, so is there a magic piece of code that would roll everything back? As far as I know, sending mail is not yet transaction controlled.
There is a proposal to change this and a long thread in the (I think zope-dev) mailing list archives. Dieter
All you need to do is raise an exception. Zope rolls back changes to the point at which the transaction was started (the beginning of the request). ----- Original Message ----- From: "Bruce Eckel" <Bruce@EckelObjects.com> To: <zope@zope.org> Cc: <ecofsky@theUNIXman.com>; <chrism@digicool.com> Sent: Saturday, November 24, 2001 10:59 AM Subject: Transactions in Zope?
I am creating an external method that makes a folder, creates a dtml method in that folder, and then sends an email message. If any of these operations fail, I'd like to "roll back" to the state before the method was called. I think I heard that zope has transactions of some kind, and I'm making mods to the ZODB in my external method, so is there a magic piece of code that would roll everything back?
Thanks!
I see the problem -- I was catching my own exceptions, so the normal Zope mechanism wasn't being used. But this brings up a bit of a quandary: try: self.MailHost.send("This is a test", 'Bruce@EckelObjects.com', form_data['email'], "%s\n\n" % form_data['seminar-id']) except smtplib.SMTPSenderRefused: return "Invalid email address: please press your browser's 'back' key and correct it" I'd like to both * catch the exception and produce an error message for the user * use the rollback mechanism in Zope Can I have my cake and eat it too? *********** REPLY SEPARATOR *********** On 11/24/01 at 6:32 PM Chris McDonough wrote:
All you need to do is raise an exception. Zope rolls back changes to the point at which the transaction was started (the beginning of the request).
----- Original Message ----- From: "Bruce Eckel" <Bruce@EckelObjects.com> To: <zope@zope.org> Cc: <ecofsky@theUNIXman.com>; <chrism@digicool.com> Sent: Saturday, November 24, 2001 10:59 AM Subject: Transactions in Zope?
I am creating an external method that makes a folder, creates a dtml method in that folder, and then sends an email message. If any of these operations fail, I'd like to "roll back" to the state before the method was called. I think I heard that zope has transactions of some kind, and I'm making mods to the ZODB in my external method, so is there a magic piece of code that would roll everything back?
Thanks!
Bruce Eckel wrote:
I'd like to both * catch the exception and produce an error message for the user * use the rollback mechanism in Zope
try this: except smtplib.SMTPSenderRefused: get_transaction().abort() return "Invalid email address: please press your" "browser's 'back' key and correct it"
Can I have my cake and eat it too?
Tell me what it tastes like :-) Chris
Inside an external method, I'm doing this: self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return '''<dtml-var standard_html_header> <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1> <dtml-var standard_html_footer> ''' I'm not really surprised that standard_html_header and standard_html_footer aren't being expanded in the result, but I wonder if there's a way to do it? Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
On Sun, Nov 25, 2001 at 11:29:03AM -0800, Bruce Eckel wrote:
return '''<dtml-var standard_html_header>
I'm not really surprised that standard_html_header and standard_html_footer aren't being expanded in the result, but I wonder if there's a way to do it?
Expand it yourself: header = self.standard_html_header # now we have the address of the object header_html = header(self, self.REQUEST) # call it to render it The same for the standard_html_footer. Oleg. -- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
Excellent, thanks! I just combined them: self.standard_html_header(self, self.REQUEST) Worked like a charm. I'm still getting used to the idea that 'self' has all these attributes. Is there a way to display all the attributes and methods available for the current 'self'? That would be a good exploration tool, I think. *********** REPLY SEPARATOR *********** On 11/25/01 at 11:45 PM Oleg Broytmann wrote:
On Sun, Nov 25, 2001 at 11:29:03AM -0800, Bruce Eckel wrote:
return '''<dtml-var standard_html_header>
I'm not really surprised that standard_html_header and standard_html_footer aren't being expanded in the result, but I wonder if there's a way to do it?
Expand it yourself:
header = self.standard_html_header # now we have the address of the object header_html = header(self, self.REQUEST) # call it to render it
The same for the standard_html_footer.
Oleg. -- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
Bruce Eckel wrote:
Inside an external method, I'm doing this:
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return '''<dtml-var standard_html_header> <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1> <dtml-var standard_html_footer> '''
I'm not really surprised that standard_html_header and standard_html_footer aren't being expanded in the result, but I wonder if there's a way to do it?
Just guessing, but it would probably work if you did self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return <dtml-var standard_html_header> + ''' <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1>''' + <dtml-var standard_html_footer> i.e. quote only the <h1>...</h1> part but not the two <dtml-var..>'s. I am guessing the quoting is preventing it from rendering. I am guessing again that the <dtml-var ...> will render inside a return statement just as it does outside it in a plain DTML document or method. Of course a Python script that does the needful ( ie invoke context.standard_html_header() + <h1> ...</h1> + context.standard_html_footer() ) could be written and then invoked from inside the return statement. This way it will render inside the Python script. If you want to parametrise the method, pass in the <h1>...</h1> stuff as a string parameter. This will effectively give you a Python script that puts the standard header/footer around any message you want. Nitin Borwankar. nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Thanks, but python doesn't grok <dtml-var standard_html_header> That looks like a couple of identifiers surrounded by less-than and greater than... *********** REPLY SEPARATOR *********** On 11/25/01 at 4:46 PM Nitin Borwankar wrote:
Just guessing, but it would probably work if you did
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return <dtml-var standard_html_header> + ''' <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1>''' + <dtml-var standard_html_footer>
i.e. quote only the <h1>...</h1> part but not the two <dtml-var..>'s. I am guessing the quoting is preventing it from rendering.
I am guessing again that the <dtml-var ...> will render inside a return statement just as it does outside it in a plain DTML document or method.
Of course a Python script that does the needful ( ie invoke context.standard_html_header() + <h1> ...</h1> + context.standard_html_footer() ) could be written and then invoked from inside the return statement. This way it will render inside the Python script. If you want to parametrise the method, pass in the <h1>...</h1> stuff as a string parameter. This will effectively give you a Python script that puts the standard header/footer around any message you want.
Nitin Borwankar. nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" &
"Thinking
in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
Bruce Eckel wrote:
Thanks, but python doesn't grok <dtml-var standard_html_header> That looks like a couple of identifiers surrounded by less-than and greater than...
Bruce, Sorry for some reason I thought you were in DTML. In Python do .... .... return context.standard_html_header() + '''foo''' + context.standard_html_footer() as suggested at the end of my last message. I tried this in a Python script which I then invoked via the "test" tab - renders as needed. Fails without the "()". Nitin Borwankar
*********** REPLY SEPARATOR ***********
On 11/25/01 at 4:46 PM Nitin Borwankar wrote:
Just guessing, but it would probably work if you did
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return <dtml-var standard_html_header> + ''' <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1>''' + <dtml-var standard_html_footer>
i.e. quote only the <h1>...</h1> part but not the two <dtml-var..>'s. I am guessing the quoting is preventing it from rendering.
I am guessing again that the <dtml-var ...> will render inside a return statement just as it does outside it in a plain DTML document or method.
Of course a Python script that does the needful ( ie invoke context.standard_html_header() + <h1> ...</h1> + context.standard_html_footer() ) could be written and then invoked from inside the return statement. This way it will render inside the Python script. If you want to parametrise the method, pass in the <h1>...</h1> stuff as a string parameter. This will effectively give you a Python script that puts the standard header/footer around any message you want.
Nitin Borwankar. nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" &
"Thinking
in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
-- -- Nitin Borwankar nitin@borwankar.com
Actually, you have to include arguments: self.standard_html_header(self, self.REQUEST) *********** REPLY SEPARATOR *********** On 11/25/01 at 11:31 PM Nitin Borwankar wrote:
Bruce Eckel wrote:
Thanks, but python doesn't grok <dtml-var standard_html_header> That looks like a couple of identifiers surrounded by less-than
and
greater than...
Bruce,
Sorry for some reason I thought you were in DTML.
In Python do
.... ....
return context.standard_html_header() + '''foo''' + context.standard_html_footer()
as suggested at the end of my last message.
I tried this in a Python script which I then invoked via the "test" tab - renders as needed. Fails without the "()".
Nitin Borwankar
*********** REPLY SEPARATOR ***********
On 11/25/01 at 4:46 PM Nitin Borwankar wrote:
Just guessing, but it would probably work if you did
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return <dtml-var standard_html_header> + ''' <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1>''' + <dtml-var standard_html_footer>
i.e. quote only the <h1>...</h1> part but not the two <dtml-var..>'s. I am guessing the quoting is preventing it from rendering.
I am guessing again that the <dtml-var ...> will render inside
a
return
statement just as it does outside it in a plain DTML document or method.
Of course a Python script that does the needful ( ie invoke context.standard_html_header() + <h1> ...</h1> + context.standard_html_footer() ) could be written and then invoked from inside the return statement. This way it will render inside the Python script. If you want to parametrise the method, pass in the <h1>...</h1> stuff as a string parameter. This will effectively give you a Python script that puts the standard header/footer around any message you want.
Nitin Borwankar. nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" &
"Thinking
in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
Bruce Eckel wrote:
Actually, you have to include arguments: self.standard_html_header(self, self.REQUEST)
*********** REPLY SEPARATOR ***********
Hmm... not if you use "context" rather than "self" you don't. Assuming Python Script and not External Method. Arguably return context.standard_html_header() + '''foo''' + context.standard_html_footer() is also cleaner and easier to read than return self.standard_html_header(self, self.REQUEST) + '''foo''' + self.standard_html_footer(self, self.REQUEST) See Zope Book chapter 8 for some examples of use of context inside Python Scripts. Nitin Borwankar
On 11/25/01 at 11:31 PM Nitin Borwankar wrote:
Bruce Eckel wrote:
Thanks, but python doesn't grok <dtml-var standard_html_header> That looks like a couple of identifiers surrounded by less-than
and
greater than...
Bruce,
Sorry for some reason I thought you were in DTML.
In Python do
.... ....
return context.standard_html_header() + '''foo''' + context.standard_html_footer()
as suggested at the end of my last message.
I tried this in a Python script which I then invoked via the "test" tab - renders as needed. Fails without the "()".
Nitin Borwankar
*********** REPLY SEPARATOR ***********
On 11/25/01 at 4:46 PM Nitin Borwankar wrote:
Just guessing, but it would probably work if you did
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return <dtml-var standard_html_header> + ''' <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1>''' + <dtml-var standard_html_footer>
i.e. quote only the <h1>...</h1> part but not the two <dtml-var..>'s. I am guessing the quoting is preventing it from rendering.
I am guessing again that the <dtml-var ...> will render inside
a
return
statement just as it does outside it in a plain DTML document or method.
Of course a Python script that does the needful ( ie invoke context.standard_html_header() + <h1> ...</h1> + context.standard_html_footer() ) could be written and then invoked from inside the return statement. This way it will render inside the Python script. If you want to parametrise the method, pass in the <h1>...</h1> stuff as a string parameter. This will effectively give you a Python script that puts the standard header/footer around any message you want.
Nitin Borwankar. nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" &
"Thinking
in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
-- -- Nitin Borwankar nitin@borwankar.com
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- -- Nitin Borwankar nitin@borwankar.com
Bruce, I've not done exactly this but you might try: from Globals import HTML def foo(self): self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return HTML('''<dtml-var standard_html_header> <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1> <dtml-var standard_html_footer> ''')(None, REQUEST) Good Luck! -steve On Sunday, November 25, 2001, at 02:29 PM, Bruce Eckel wrote:
Inside an external method, I'm doing this:
self.REQUEST.RESPONSE.setHeader('content-type', 'text/html') return '''<dtml-var standard_html_header> <h1 color="red">Invalid email address: please press your browser's 'back' key and correct it</h1> <dtml-var standard_html_footer> '''
I'm not really surprised that standard_html_header and standard_html_footer aren't being expanded in the result, but I wonder if there's a way to do it?
Most current information can be found at: http://www.mindview.net/Etc/notes.html =================== Bruce Eckel http://www.BruceEckel.com Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++ 2e" Please subscribe to my free newsletter -- just send any email to: join-eckel-oo-programming@earth.lyris.net My schedule can be found at: http://www.mindview.net/Calendar ===================
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (7)
-
Bruce Eckel -
Chris McDonough -
Chris Withers -
Dieter Maurer -
Nitin Borwankar -
Oleg Broytmann -
Steve Spicklemire