Mailing List Archive

updating fields in a zsql method programmatically
Here's the scenario:

1. "insertNew" is a Z SQL Method for creating a new record in pgsql "table1"
Argument list = field1, field2, field3

SQL Query:
INSERT INTO table1 (field1, field2, field3)
VALUES (
<dtml-sqlvar field1 type="string">,
<dtml-sqlvar field2 type="string">,
<dtml-sqlvar field3 type="string">)

2. "call_insertNew" is a python script that is called as an "After
Validation Script" by a pfg form

request = container.REQUEST
RESPONSE = request.RESPONSE
form = request.form

changed_field1 = form.get('field1')
changed_field1 = "a new computed value"
field1 = changed_field1

#create a new record with form field results for field2 & field3, and a
computed value for field1
context.insertNew()

*************
It's easy to insert and update Z SQL Methods directly from form field
results.

It's easy to read form field results (e.g. request.field2) and do
comparisons, etc.

I am struggling to understand how one can change a form field result with a
computed value from a python script.

Either of the following returns an error:
form.get('field1') = changed_field1
request.field1 = changed_field1

and...
field1 = changed_field1
...the value doesn't get posted to the pgsql record. Instead the pgsql
field1 value remains what the request.field1 value was originally.

Any direction is appreciated... Jim

--
View this message in context: http://www.nabble.com/updating-fields-in-a-zsql-method-programmatically-tf4335404.html#a12347343
Sent from the Zope - DB mailing list archive at Nabble.com.

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
> changed_field1 = "a new computed value"
> field1 = changed_field1
>
> #create a new record with form field results for field2 & field3, and a
> computed value for field1
> context.insertNew()
Obvious things are sometimes hardest to find ;)

Try:

field1 = changed_field1
context.insertNew(field1=field1)

or even:

context.insertNew(field1='something', field2=other_variable)

It is more clear than getting values from context I think.

--
Maciej Wisniowski
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
I tried the techniques you suggested and it results in the same error -->
"KeyError: 'form.submitted'"

field1 = changed_field1
context.insertNew(field1=field1) --> I also tried the other technique and
got the same error.

I tried your technique with the following changes to my pfg (ploneformgen)
for and Z SQL Method:
1. with field1, field2, field3 all included in the pfg (ploneformgen) form
2. with these same three fields excluded from the pfg form
3. with my Z SQL method argument as field1
4. with my Z SQL method argument as field1=field1

I just know that there has to be an easy solution to this problem.

I'm assuming the form results are stored as an object represented as a
dictionary. If so, then there must be a way for my python script to change
the key | value pair for field1 before I call my Z SQL Method. Is this
reasonable, or am I just going down a rat-hole?

Thanks... Jim







Maciej Wisniowski wrote:
>
>> changed_field1 = "a new computed value"
>> field1 = changed_field1
>>
>> #create a new record with form field results for field2 & field3, and a
>> computed value for field1
>> context.insertNew()
> Obvious things are sometimes hardest to find ;)
>
> Try:
>
> field1 = changed_field1
> context.insertNew(field1=field1)
>
> or even:
>
> context.insertNew(field1='something', field2=other_variable)
>
> It is more clear than getting values from context I think.
>
> --
> Maciej Wisniowski
> _______________________________________________
> Zope-DB mailing list
> Zope-DB@zope.org
> http://mail.zope.org/mailman/listinfo/zope-db
>
>

--
View this message in context: http://www.nabble.com/updating-fields-in-a-zsql-method-programmatically-tf4335404.html#a12356504
Sent from the Zope - DB mailing list archive at Nabble.com.

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
Am 27.08.2007, 15:59 Uhr, schrieb onsombal <onsombal@yahoo.com>:

> changed_field1 = form.get('field1')
> changed_field1 = "a new computed value"
> field1 = changed_field1
> #create a new record with form field results for field2 & field3, and a
> computed value for field1
> context.insertNew()
^^^^^^^^^^^^^^^^^^^^^

This is really bad as it undoes all your good work of checking the the
values in your script. All ZSQL methods also have access to the REQUEST
object by default so your ZSQL method sees context.REQUEST.field1. Way
back when ZSQL was developed support for request objects was given to ZSQL
to allow funky parameters in the URL as well as the other usual special
methods such as manage and edit. Personally I think this is a bad idea and
always pass my parameters in as named arguments as Maciej suggests or as a
dictionary for neatness if there is a lot of them.

Charlie

--
Charlie Clark
eGenix.com

Professional Python Services directly from the Source
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::

eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
Am 27.08.2007, 21:59 Uhr, schrieb onsombal <onsombal@yahoo.com>:

> I'm assuming the form results are stored as an object represented as a
> dictionary. If so, then there must be a way for my python script to
> change
> the key | value pair for field1 before I call my Z SQL Method. Is this
> reasonable, or am I just going down a rat-hole?

All form values are in the context.REQUEST.form dictionary which can
overwrite to your heart's content.

Charlie
--
Charlie Clark
eGenix.com

Professional Python Services directly from the Source
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::

eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
onsombal wrote at 2007-8-27 06:59 -0700:
> ...
> ...
>It's easy to insert and update Z SQL Methods directly from form field
>results.
>
>It's easy to read form field results (e.g. request.field2) and do
>comparisons, etc.
>
>I am struggling to understand how one can change a form field result with a
>computed value from a python script.

You use keyword parameters in the call of your "ZSQL Method".

The signature of a "ZSQL Method"s "__call__" method ("__call__" is the method
called when you call an object) is:

def __call__(self, REQUEST=None, __ick__=None, src__=0, test__=0, **kw):
"""Call the database method

The arguments to the method should be passed via keyword
arguments, or in a single mapping object. If no arguments are
given, and if the method was invoked through the Web, then the
method will try to acquire and use the Web REQUEST object as
the argument mapping.

The returned value is a sequence of record objects.
"""

When you do not pass (unknown) keyword arguments (more precisely, if "kw"
is empty), then the ZSQL method uses "REQUEST" as default.
That the usual way, how a ZSQL method gets values from the request.

When you have changed something, then you can change the value
in "REQUEST" (less advicable) or pass *all* parameters via keyword
arguments (much better because more explicit).



--
Dieter
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
onsombal napisa³(a):
> I tried the techniques you suggested and it results in the same error -->
> "KeyError: 'form.submitted'"
You didn't tell about this kind of error before.
Post a full traceback, please.
I suppose that error is somewhere else...

--
Maciej Wisniowski
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: updating fields in a zsql method programmatically [ In reply to ]
So, my script now works!! Thanks to everyone for your input. It is greatly
appreciated. Below is a quick summary of what I did and a relevant excerpt
of the working script.

1) I never was able to get passing the arguments via the context.Statement
to work. I'm sure it's something I've done wrong.

2) I successfully updated the context.REQUEST.form dictionary key:value pair
with my computed value. The computed value was to replace the blank
(hidden) 'fdbkurl' pfg form field with the url of a form object that was
copied from a standard-template to another folder, and then renamed. After
renaming, I re-indexed the object, and then captured its absolute url --
which I then used to update the dictionary key:value.

Code follows:

#copy, paste, rename std-survey to Folder
from Products.CMFCore.utils import getToolByName

urltool = getToolByName(context, "portal_url")
portal = urltool.getPortalObject()
mydoc_folder = getToolByName(context, "my-documents")
crm_folder = getToolByName(context, "crm")
survey_folder = getToolByName(context, "std-forms")
listing_folder = getattr(mydoc_folder, "listings")

#manage_copyObjects(self, ids=None, REQUEST=None, RESPONSE=None)
cb_copy_data = survey_folder.manage_copyObjects(["std-survey"])
dest = getattr(listing_folder, tls_id)

#manage_pasteObjects(self, cb_copy_data=None, REQUEST=None)
#Paste previously copied objects into the current object.
#If calling manage_pasteObjects from python code,
#pass the result of a previous call to manage_cutObjects or
manage_copyObjects as the first argument.
dest.manage_pasteObjects(cb_copy_data)

#manage_renameObjects(self, ids=[], new_ids=[], REQUEST=None)
tls_folder = getattr(listing_folder, tls_id)
survey_id = "survey" + form.get('tls')
survey_title = "Survey" + form.get('tls')
survey_obj = getattr(tls_folder, "std-survey")
tls_folder.manage_renameObjects(ids=["std-survey"], new_ids=[survey_id])
survey_obj.setTitle(survey_title)
survey_obj.reindexObject()
fbkurl = survey_obj.absolute_url()
formresult['fbkurl'] = fbkurl

#create record in usdb listing table
context.listingsCreateRow()

And it works!




Charlie Clark-2 wrote:
>
> Am 27.08.2007, 21:59 Uhr, schrieb onsombal <onsombal@yahoo.com>:
>
>> I'm assuming the form results are stored as an object represented as a
>> dictionary. If so, then there must be a way for my python script to
>> change
>> the key | value pair for field1 before I call my Z SQL Method. Is this
>> reasonable, or am I just going down a rat-hole?
>
> All form values are in the context.REQUEST.form dictionary which can
> overwrite to your heart's content.
>
> Charlie
> --
> Charlie Clark
> eGenix.com
>
> Professional Python Services directly from the Source
>>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
> ________________________________________________________________________
>
> :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
>
> eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
> Registered at Amtsgericht Duesseldorf: HRB 46611
> _______________________________________________
> Zope-DB mailing list
> Zope-DB@zope.org
> http://mail.zope.org/mailman/listinfo/zope-db
>
>

--
View this message in context: http://www.nabble.com/updating-fields-in-a-zsql-method-programmatically-tf4335404.html#a12359442
Sent from the Zope - DB mailing list archive at Nabble.com.

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db