Skip to content

PageTemplate digest cannot be computed for TTW template #254

Closed

Description

@fulv

chameleon.template.BaseTemplate.digest() prepends the pathname (minus extension) to the digest under this condition:

        if self.filename is not BaseTemplate.filename:
            digest = os.path.splitext(self.filename)[0] + '-' + digest

However, when self is <PageTempate None> this throws the following error:

*** AttributeError: 'NoneType' object has no attribute 'rfind'

This happens in the following scenario:

  1. Install a clean Plone 4.3.15 buildout
  2. Add Products.PloneFormGen 1.7.23 to the buildout
  3. Add five.pt to the buildout
  4. Create a new Plone site, activate PloneFormGen
  5. Create a FormFolder
  6. Go to the mailer Mail Adapter which is added by default to a new Form Folder
  7. Edit the mailer
  8. Save it without making any changes

Result: You will not be able to save. The following validation error for the textarea containing the default mail template is displayed:

Compilation Failed

exceptions.AttributeError: 'NoneType' object has no attribute 'rfind'

I'm not sure if there is something that PloneFormGen needs to do differently, but I do know that changing the condition to this fixed the problem:

        if self.filename and self.filename is not BaseTemplate.filename:

Is self = <PageTemplate None> because we are being called from __init__()? Either way, this is a TTW template, so it should not have a filename.

This is the tail of the stack where the error happens:

 /usr/local/plone-4.3/buildout-cache/eggs/Products.CMFFormController-3.0.8-py2.7.egg/Products/CMFFormController/FSControllerValidator.py(58)__call__()
-> result = FSControllerValidator.inheritedAttribute('__call__')(self, *args, **kwargs)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.CMFFormController-3.0.8-py2.7.egg/Products/CMFFormController/Script.py(145)__call__()
-> return BaseFSPythonScript.__call__(self, *args, **kw)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.CMFCore-2.2.10-py2.7.egg/Products/CMFCore/FSPythonScript.py(127)__call__()
-> return Script.__call__(self, *args, **kw)
  /usr/local/plone-4.3/buildout-cache/eggs/Zope2-2.13.26-py2.7.egg/Shared/DC/Scripts/Bindings.py(322)__call__()
-> return self._bindAndExec(args, kw, None)
  /usr/local/plone-4.3/buildout-cache/eggs/Zope2-2.13.26-py2.7.egg/Shared/DC/Scripts/Bindings.py(359)_bindAndExec()
-> return self._exec(bound_data, args, kw)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.PythonScripts-2.13.2-py2.7.egg/Products/PythonScripts/PythonScript.py(344)_exec()
-> result = f(*args, **kw)
  /usr/local/plone-4.3/zeoserver/Script (Python)(6)validate_base()
  /usr/local/plone-4.3/buildout-cache/eggs/Products.Archetypes-1.9.17-py2.7.egg/Products/Archetypes/BaseObject.py(487)validate()
-> errors=errors, data=data, metadata=metadata)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.Archetypes-1.9.17-py2.7.egg/Products/Archetypes/Schema/__init__.py(606)validate()
-> REQUEST=REQUEST)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.Archetypes-1.9.17-py2.7.egg/Products/Archetypes/Field.py(345)validate()
-> res = self.validate_validators(value, instance, errors, **kwargs)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.Archetypes-1.9.17-py2.7.egg/Products/Archetypes/Field.py(358)validate_validators()
-> field=self, **kwargs)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.validation-2.0.2-py2.7.egg/Products/validation/chain.py(136)__call__()
-> result = validator(value, *args, **kwargs)
  /usr/local/plone-4.3/buildout-cache/eggs/Products.TemplateFields-1.2.5-py2.7.egg/Products/TemplateFields/validators.py(98)__call__()
-> pt.write(value)
  /usr/local/plone-4.3/buildout-cache/eggs/zope.pagetemplate-3.6.3-py2.7.egg/zope/pagetemplate/pagetemplate.py(162)write()
-> self._cook()
  /usr/local/plone-4.3/buildout-cache/eggs/zope.pagetemplate-3.6.3-py2.7.egg/zope/pagetemplate/pagetemplate.py(209)_cook()
-> source_file, self._text, pt_engine, self.content_type)
  /usr/local/plone-4.3/buildout-cache/eggs/five.pt-2.2.4-py2.7.egg/five/pt/engine.py(132)cook()
-> encoding='utf-8', extra_builtins=cls.extra_builtins,
  /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/zpt/template.py(185)__init__()
-> super(PageTemplate, self).__init__(body, **config)
  /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/template.py(116)__init__()
-> self.write(body)
  /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/template.py(209)write()
-> self.cook(body)
  /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/template.py(145)cook()
-> digest = self.digest(body, names)
  /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/zpt/template.py(297)digest()
-> hex = super(PageTemplate, self).digest(body, names)
> /usr/local/plone-4.3/buildout-cache/eggs/Chameleon-3.1-py2.7.egg/chameleon/template.py(247)digest()
-> digest = os.path.splitext(self.filename)[0] + '-' + digest
(Pdb) self
<PageTemplate None>
(Pdb) self.filename
(Pdb) os.path.splitext(self.filename)
*** AttributeError: 'NoneType' object has no attribute 'rfind'

See https://community.plone.org/t/cant-save-pfg-mailer-adapter-compilation-failed-exceptions-attributeerror-nonetype-object-has-no-attribute-rfind/5088?u=fulv

Activity

  1. jamadden commented on Oct 31, 2017

    @jamadden
    Contributor

    I ran into the same thing with z3c.ptcompat. We worked around it there.

  2. fulv commented on Nov 5, 2017

    @fulv
    Author

    So I can see three possible solutions for this:

    1. In Products.TemplateFields.validators.ZPTValidator.__call__

    The symbol pt is bound to a new AqPageTemplate, which is a Products.PageTemplates.PageTemplate.PageTemplate:

    pt = AqPageTemplate()
    ...
    pt.write(value)
    

    Before the write call, we could set pt.pt_source_file to a callable that returns any string value, e.g.:

    pt.pt_source_file = lambda: 'nofile'
    

    This seems risky to me, but it could be wrapped in an if not pt.pt_source_file(). Also, I can't find the repository for Products.TemplateFields.

    2. In five.pt.engine.Program.cook:130:

            template = ChameleonPageTemplate(
                text, filename=source_file, keep_body=True,
                expression_types=expression_types,
                encoding='utf-8', extra_builtins=cls.extra_builtins,
                )
    

    Basically do what @jamadden did in z3c.ptcompat, and change filename=source_file to filename=source_file or '<string>'.

    This seems safe.

    3. In chameleon.template.BaseTemplate.digest(), what I suggested above:

    change

    if self.filename is not BaseTemplate.filename:
    

    to

    if self.filename and self.filename is not BaseTemplate.filename:
    

    I think 3 should be done anyway.

  3. fulv commented on Nov 5, 2017

    @fulv
    Author

    @jensens do you know where Products.TemplateFields lives?

  4. jensens commented on Nov 6, 2017

    @jensens

    @fulv if nobody migrated it to git, it should be transferred to GitHub collective from here https://svn.plone.org/svn/archetypes/Products.TemplateFields/

  5. malthe commented on Nov 6, 2017

    @malthe
    Owner

    Can't we do without a change in Products.TemplateFields – ?

  6. malthe commented on Nov 6, 2017

    @malthe
    Owner

    @fulv – wanna make a PR?

  7. fulv commented on Nov 6, 2017

    @fulv
    Author

    @malthe - sure.
    @jensens - would you please remind me what qualifies for the collective GitHub org vs the plone org?

  8. jensens commented on Nov 6, 2017

    @jensens

    @fulv, the code in archetypes svn never required a signed contributor agreement, so it can't go into plone organization, where it is required.

  9. fulv commented on Nov 6, 2017

    @fulv
    Author
  10. rafaelbco commented on Jan 23, 2018

    @rafaelbco

    Any news on this? I've just stumbled upon this bug.

  11. fulv commented on Jan 23, 2018

    @fulv
    Author
  12. fredvd commented on Oct 10, 2019

    @fredvd

    Fix for Plone 4.3 with PloneFormGen / Products.Templatefields and five.pt with newer Chameleon 3.0: https://github.com/zestsoftware/five.pt/tree/2.2

    Cross-ported the fix from z3c.ptcompat from @jamadden found here https://github.com/zopefoundation/z3c.ptcompat/pull/4/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions