ckan/ckanext-pdfview

'str_GET' exception (flask related)

Opened this issue · 4 comments

I see this exception:
[2019-06-10 19:34:12,149] ERROR in flask_app: 'flask.wrappers.Request object' has no attribute 'str_GET'
with CKAN master (to be v2.9)

It's because we've move from a pylons request to flask request. We could fix it for flask like this:

diff --git a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html
index f6281af..3465c58 100644
--- a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html
+++ b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html
@@ -34,7 +34,7 @@
         </p>
       </div>
       {% if not to_preview %}
-        {% set current_filters = request.str_GET.get('filters') %}
+        {% set current_filters = request.args.get('filters') %}
         {% if current_filters %}
           {% set src = h.url(qualified=true, controller='package',
                              action='resource_view', id=package['name'],

However this will not work with earlier CKAN versions. Maybe we need a helper function, or maybe the template can switch on whether we're pylons/flask?

Full exception:

[2019-06-10 19:34:12,149] ERROR in flask_app: 'flask.wrappers.Request object' has no attribute 'str_GET'
Traceback (most recent call last):
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/lib/ckan/default/src/ckan/ckan/views/resource.py", line 144, in read
    return base.render(template, extra_vars)
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/base.py", line 148, in render
    return flask_render_template(template_name, **extra_vars)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/ckan/default/src/ckan/ckanext/datastore/templates/package/resource_read.html", line 1, in top-level template code
    {% ckan_extends %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 3, in top-level template code
    {% set res = resource %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/base.html", line 3, in top-level template code
    {% set pkg = pkg_dict %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/page.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/usr/lib/ckan/default/src/ckanext-geoview/ckanext/geoview/templates/base.html", line 1, in top-level template code
    {% ckan_extends %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/base.html", line 106, in top-level template code
    {%- block page %}{% endblock -%}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/page.html", line 19, in block "page"
    {%- block content %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/page.html", line 22, in block "content"
    {% block main_content %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/page.html", line 53, in block "main_content"
    {% block pre_primary %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 22, in block "pre_primary"
    {% block resource %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 24, in block "resource"
    {% block resource_inner %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 104, in block "resource_inner"
    {% block data_preview %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 105, in block "data_preview"
    {% block resource_view %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 118, in block "resource_view"
    {% block resource_view_content %}
  File "/usr/lib/ckan/default/src/ckan/ckan/templates/package/resource_read.html", line 128, in block "resource_view_content"
    {% snippet 'package/snippets/resource_view.html',
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/jinja_extensions.py", line 269, in _call
    return base.render_snippet(*args, **kwargs)
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/base.py", line 96, in render_snippet
    output = render(template_name, extra_vars=kw)
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/base.py", line 148, in render
    return flask_render_template(template_name, **extra_vars)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/ckan/default/src/ckanext-pdfview/ckanext/pdfview/theme/templates/package/snippets/resource_view.html", line 37, in top-level template code
    {% set current_filters = request.str_GET.get('filters') %}
  File "/usr/lib/ckan/default/lib/python2.7/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
UndefinedError: 'flask.wrappers.Request object' has no attribute 'str_GET'

I think the simplest is to use a helper function that does some attribute checks (params if Pylons request, args if a Flask one).

Helper function sounds good.

I'm not in a convenient position to do/test this fix, so will leave this open for when I do, or if someone else gets there first.

Maybe h.get_request_param?

Any updates?