awesto / django-shop

A Django based shop system
http://www.django-shop.org
BSD 3-Clause "New" or "Revised" License
3.2k stars 1.03k forks source link

Passing 'None' when calling `django.template.loader.select_template` in 'cascade/catalog.py' #708

Closed mliudev closed 6 years ago

mliudev commented 6 years ago

This is the offending piece of code on line 66 of 'cascade/catalog.py':

    def get_render_template(self, context, instance, placeholder):
        template = select_template([
            instance.glossary.get('render_template') or None,
            '{}/catalog/product-add2cart.html'.format(app_settings.APP_LABEL),
            'shop/catalog/product-add2cart.html',
        ])

        return template

If the instance doesn't have a template in its glossary then None is passed to select_template(...) which causes a 500 ISE with the following traceback:

  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/cms/plugin_base.py", line 155, in _get_render_template
    template = self.get_render_template(context, instance, placeholder)
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/shop/cascade/catalog.py", line 68, in get_render_template
    'shop/catalog/product-add2cart.html',
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/loader.py", line 48, in select_template
    return engine.get_template(template_name)
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/backends/django.py", line 39, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/engine.py", line 162, in get_template
    template, origin = self.find_template(template_name)
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/engine.py", line 136, in find_template
    name, template_dirs=dirs, skip=skip,
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/loaders/base.py", line 32, in get_template
    for origin in self.get_template_sources(*args):
  File "/Users/mliu/.virtualenvs/session-aws/lib/python2.7/site-packages/django/template/loaders/eggs.py", line 48, in get_template_sources
    pkg_name = 'templates/' + template_name
TypeError: coercing to Unicode: need string or buffer, NoneType found

None should not be passed select_template(...).

I think the fix here is to create the list before sending it to select_template(...) without any references to None. I can make a PR if this sounds correct. This problem currently affects version 0.11.4 and up.

jrief commented 6 years ago

But if instance.glossary.get('render_template') returns None, then the or ''-clause should catch it. Are you sure this is causing your problem?

mliudev commented 6 years ago

@jrief I'm sorry I copied my locally edited version of the code when I pasted this. I've fixed the comment to reflect what's actually in the repo in this line: https://github.com/awesto/django-shop/blob/0.11.4/shop/cascade/catalog.py#L66. Note that passing '' will also create a TemplateNotFound exception.

jrief commented 6 years ago

@mliudev Thanks for reporting. This will be fixed in 0.11.6

mliudev commented 6 years ago

Thanks!