I am trying to make a z3c form in Plone that returns a PDF depending on the
input. So far this is more or less what I have, but it seems sub-optimal.
For example, I can't just check self.form.results_data after calling the
default template.__on__(self)(self), because it isn't there, which I found
confusing.

Where is the default template? There must be a better place to override the
entire response?

Thanks,

Daniel Holth

class F(form.Form):
    ignoreContext = True
    fields = field.Fields(IMyForm)
    results_template = ViewPageTemplateFile('results.pt')

    @button.buttonAndHandler(_(u"Submit"))
    def locate(self, action):
        data, errors = self.extractData()
        try:
            locator = getUtility(ISL)
            joint = "Any"
            self.results_data = locator.locate(data['zip'])
            self.template = self.results_template

            from cStringIO import StringIO
            sio = StringIO()
            reportlabstuff(sio, data['zip'], self.results_data)

            self.request.response.setHeader('Content-Type',
'application/pdf')
            self.request.response.setBody(sio.getvalue())
        except Exception, e:
            print e, type(e), e.__class__
            raise form.interfaces.WidgetActionExecutionError(u'zip',
interface.Invalid(_(u'Unknown zip code. Please try another zip code.')))

class SLF(base.FormWrapper):
    form = F
    label = _(u"Form Label")
    description = _(u"Enter your zip code to find the nearest
results.")
    def inx(self):
        template = zope.component.getMultiAdapter((self, self.request),
IPageTemplate)
        formresult = template.__of__(self)(self)
        if self.request.response.getHeader('Content-type') ==
"application/pdf":
            return self.request.response
        return formresult

    index = inx
_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers

Reply via email to