[Zope] - Next problem - looking for dtml file in the wrong place...

Jim Fulton jim@Digicool.com
Tue, 08 Dec 1998 20:29:25 +0000


skip@calendar.com wrote:
> 
> I'm inching along.  Thanks to Michel Pelletier and Andy Kuchling, I now know
> my calendar module is a Product and what most (all?) of the necessary
> scaffolding is.  My Calendar class supports a show method:
> 
>     def show(self, year, month=0):
>         """return plain ASCII calendar
> 
>         month == 0 ==> display calendar for entire year
>         """
> 
>         if year < 1 or year > 9999:
>             raise ValueError, ("year out of range: %d" % year)
>         if month < 0 or month > 12:
>             raise ValueError, ("month out of range: %d" % month)
> 
>         if month: cal = os.popen("cal %s %s" % (month, year)).read()
>         else: cal = os.popen("cal %s" % year).read()
> 
>         return HTMLFile('cal', month=month, year=year, cal=cal)

Is this Globals.HTMLFile or DocumentTemplate.HTMLFile?
 
> When I access
> 
>     http://localhost:8080/Calendar/show?month:int=0&year:int=1998
> 
> I get the following traceback:
> 
>     Traceback (innermost last):
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/ZPublisher/Publish.py, line 861, in publish_module
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/ZPublisher/Publish.py, line 585, in publish
>         (Info: /Calendar/show)
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/ZPublisher/Response.py, line 284, in setBody
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/DocumentTemplate/DT_HTML.py, line 217, in __str__
>         (Object: /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/cal.dtml)
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/DocumentTemplate/DT_HTML.py, line 209, in quotedHTML
>         (Object: /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/cal.dtml)
>       File /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/DocumentTemplate/DT_String.py, line 532, in read_raw
>         (Object: /home/dolphin/skip/src/Zope-1.9b1-src/lib/python/cal.dtml)
>     IOError: (2, 'No such file or directory')
> 
> which suggests to me that my cal.dtml file is not where Zope expects to find
> it.  All files in my Products/Calendar directory are in that directory:
> 
>     % ls -F lib/python/Products/Calendar/
>     __init__.py            cal.dtml               vcal.gif
>     __init__.pyc           cal.py
>     addCalendar_form.dtml  cal.pyc
> 
> Why is it looking up two levels in the directory hierarchy for the DTML
> file? 

I bet that you are running Globals.HTMLFile, which assumes that
DTML files are in SOFTWARE_HOME.

> As far as I can tell, I'm using the same calling conventions for
> HTMLFile objects as the MailHost and sample Products do.

Not quite. If you look closely, you'll see that HTMLFile is
used to create methods and that it's usually called with 
globals() passed in. This allows it to figure out where
the calling package is located so that it can find the DTML
file. I think you want:

    _show=HTMLFile('cal',globals())
    def show(self, year, month=0):
        """return plain ASCII calendar

        month == 0 ==> display calendar for entire year
        """

        if year < 1 or year > 9999:
            raise ValueError, ("year out of range: %d" % year)
        if month < 0 or month > 12:
            raise ValueError, ("month out of range: %d" % month)

        if month: cal = os.popen("cal %s %s" % (month, year)).read()
        else: cal = os.popen("cal %s" % year).read()

        return self._show(self, month=month, year=year, cal=cal)

I assume that you want to return the rendered template, right?
In your version you were just calling the constructor.  It's
better to call the constructor in the class definition so that
you don't pay construction and compilation costs on each hit.

Jim

--
Jim Fulton           mailto:jim@digicool.com
Technical Director   (540) 371-6909              Python Powered!
Digital Creations    http://www.digicool.com     http://www.python.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.