Chris Withers wrote at 2005-5-23 20:02 +0100: >Dieter Maurer wrote: >>>Here is the problem. I want to use a form to upload a file (with a specific >>>name) but first I want to check if another file with the same name already >>>exist in that folder. How do I use a if or dtml-if to solve this problem?? >> >> <dtml-if expr="hasattr(the_folder.aq_inner.aq_explicit, the_id)"> >> "the_id" exists >> <dtml-else> >> "the_id" does not exist >> </dtml-if> > >Dieter, I'm shocked ;-) > >Why in DTML? Even though Allen mentions DTML, this never belongs in DTML.
We are already familiar with the fact that we often disagree. This is another case. The test whether an object exists is sufficiently trivial (and often needed) that it can be in DTML without a problem. An incredibly long time ago, I filed a feature request for "hasattr_unacquired" -- together with patch, unit tests and documentation update. I am convinced that such a function in the DTML namespace (and therefore always available in restricted code) would be much clearer than the "aq_inner.aq_explicit" dance. But, unfortunately, the Zope developers decided not to accept my patch or the "hasattr_unacquired" idea and instead made "aq_inner" accessible by untrusted code. A bad decision! As a consequence, you see the nasty code. By the way, it is as nasty in Python as it is in DTML. >Worse still, you use hasattr with ZODB... hastattr swallows all >exceptions, including ConflictErrors, and especially in this example >that would be a bad thing... Again, the correct way to approach this problem is not to ban "hasattr" from thousands of places but to monkey patch "__builtin__.hasattr" such that it behaves in a Zope compatible way. I have seen several severely broken trial to work around the use of "hasattr" -- by Zope experts not Zope newbies. I strongly argue against it. Fix "hasattr" in the Zope context, instead! >if getattr(thefolder.aq_inner.aq_explicit,the_id,None): You are aware that this is in general *NOT* an emulation of "hasattr". It may fail e.g. for properties. Such half faithful "hasattr" emulations will make more problems then even the unfixed "hasattr" use. And it would be so easy to fix "hasattr" ;-) -- Dieter _______________________________________________ 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 )