I run across inexplicable behaviour: DTML method "ads_save" has a file type from previous method's <input type=file>, and it calls Python Script. This Python script should check if that file is already present, delete it, and save the new file. This works fine if the file is not present. But if it is present, Python script throws up with Attribute Error: filename at the line number 1. DTML Method "index_html": <form name=form1 action="ads_save" method="post" enctype="multipart/form-data"> <input type="file" name="ad_title"> URL: <input name="ad_title_url" type=text> ... DTML Method "ads_save": <dtml-var "ad_save('ad_title',ad_title,ad_title_url)"> Python Script "ad_save" (parameters id,ad,url): if ad.filename: try: context.manage_delObjects([id]) except: pass context.manage_addImage(id=id, file=ad, title=url) return This is difficult to comprehend, because the offending line "if ad.filename" is testing a REQUEST form variable (a parameter to Python script, strictly speaking). It is not testing a ZODB file. Or is it? The only solution I can imagine is modifying "ads_save" to have three lines: <dtml-if "ad_title.filename"> <dtml-var "ad_save('ad_title',ad_title,ad_title_url)"> </dtml-if> But that's not exactly "putting logic to python and presentation to DTML... :-( -- Milos Prudek
[Milos Prudek]
I run across inexplicable behaviour:
DTML method "ads_save" has a file type from previous method's <input type=file>, and it calls Python Script. This Python script should check if that file is already present, delete it, and save the new file. This works fine if the file is not present. But if it is present, Python script throws up with Attribute Error: filename at the line number 1.
DTML Method "index_html": <form name=form1 action="ads_save" method="post" enctype="multipart/form-data"> <input type="file" name="ad_title"> URL: <input name="ad_title_url" type=text> ...
DTML Method "ads_save": <dtml-var "ad_save('ad_title',ad_title,ad_title_url)">
Here it looks like you send the value of ad_title to the script as the second parameter
Python Script "ad_save" (parameters id,ad,url):
# Here the parameter called "ad" gets the value of ad_title
if ad.filename: #It's a string, not an object, so if ad exists, there is no ad.filename
try: context.manage_delObjects([id]) except: pass context.manage_addImage(id=id, file=ad, title=url) return
This is difficult to comprehend, because the offending line "if ad.filename" is testing a REQUEST form variable (a parameter to Python script, strictly speaking). It is not testing a ZODB file. Or is it?
A REQUEST variable which is a string, not an object, as best as I can see. Cheers, Tom P
Here it looks like you send the value of ad_title to the script as the second parameter
Yes. ad_title is HTTPUpload instance. It is NOT a string.
Python Script "ad_save" (parameters id,ad,url):
# Here the parameter called "ad" gets the value of ad_title
if ad.filename:
#It's a string, not an object, so if ad exists, there is no ad.filename
No no, it is not a string. It is HTTP Upload instance. I can prove it: As long as there is no "ad_title" image file in the current folder, the "ad_save" is able to "manage_addImage" succesfully. And if you look at manage_addImage parameter you'll see that I'm using file=ad to get to image data. Furthermore, the "if ad.filename" was originally designed to take care of situation when the original form did NOT have any filename entered. It works very well. If no image was entered on the form, condition skips. If there was an image entered, condition is valid.
A REQUEST variable which is a string, not an object, as best as I can see.
If it were string it would never work. It would never add an image. But it refuses to add an image only if there is already an image file "ad_title". -- Milos Prudek
If it were string it would never work. It would never add an image. But it refuses to add an image only if there is already an image file "ad_title".
Now I'm a little bit further to understanding this problem. It's about namespace order of search. Since the same name "ad_title" is used for both form variable and resulting image, it means a clash. When the image is already there, the "if ad.filename" actually queries the image file in ZODB - because the namespace search order says that ZODB comes before REQUEST.form. And the image file has no attribute called filename. I know two ways to solve it. Change form name, or put images in different folder. I also know how to solve it without renaming of moving images but in pure DTML with Python Script dropped. <dtml-with REQUEST>. However I would be interested to know (for the sake of the argument) if there is a way to solve it without renaming of moving images, and in Python Script. I was trying to qualify ad_title in Python like this: if context.REQUEST.ad_title.filename: or if context.REQUEST['ad_title.filename']: but it would not work. Why? -- Milos Prudek
Try this instead: Python Script "ad_save" (parameters id,ad,url): if hasattr(ad, 'filename'): try: context.manage_delObjects([id]) except: pass context.manage_addImage(id=id, file=ad, title=url) return At 20:33 2002-03-06 +0100, Milos Prudek wrote:
I run across inexplicable behaviour:
DTML method "ads_save" has a file type from previous method's <input type=file>, and it calls Python Script. This Python script should check if that file is already present, delete it, and save the new file. This works fine if the file is not present. But if it is present, Python script throws up with Attribute Error: filename at the line number 1.
DTML Method "index_html": <form name=form1 action="ads_save" method="post" enctype="multipart/form-data"> <input type="file" name="ad_title"> URL: <input name="ad_title_url" type=text> ...
DTML Method "ads_save": <dtml-var "ad_save('ad_title',ad_title,ad_title_url)">
Python Script "ad_save" (parameters id,ad,url): if ad.filename: try: context.manage_delObjects([id]) except: pass context.manage_addImage(id=id, file=ad, title=url) return
This is difficult to comprehend, because the offending line "if ad.filename" is testing a REQUEST form variable (a parameter to Python script, strictly speaking). It is not testing a ZODB file. Or is it?
The only solution I can imagine is modifying "ads_save" to have three lines: <dtml-if "ad_title.filename"> <dtml-var "ad_save('ad_title',ad_title,ad_title_url)"> </dtml-if>
But that's not exactly "putting logic to python and presentation to DTML... :-(
-- Milos Prudek
_______________________________________________ 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 (4)
-
Milos Prudek -
Milos Prudek -
Peter Bengtsson -
Thomas B. Passin