ucam-department-of-psychiatry / camcops

Cambridge Cognitive and Psychiatric Test Kit (CamCOPS)
Other
12 stars 8 forks source link

APEQ CPFT Perinatal Report: Internal Server Error #203

Closed martinburchell closed 2 years ago

martinburchell commented 3 years ago
Traceback (most recent call last):
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_pyramid.py", line 546, in __call__
    result = template.render_unicode(**system)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/template.py", line 479, in render_unicode
    self, self.callable_, args, data, as_unicode=True
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/runtime.py", line 885, in _render
    return context._pop_buffer().getvalue()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/util.py", line 169, in getvalue
    return self.delim.join(self.data)
TypeError: sequence item 350: expected str instance, int found

Traceback (most recent call last):
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/tweens.py", line 13, in _error_handler
    response = request.invoke_exception_view(exc_info)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/view.py", line 779, in invoke_exception_view
    raise HTTPNotFound
pyramid.httpexceptions.HTTPNotFound: The resource could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/cheroot/server.py", line 1287, in communicate
    req.respond()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/cheroot/server.py", line 1077, in respond
    self.server.gateway(self).respond()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/cheroot/wsgi.py", line 140, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/cherrypy/_cptree.py", line 302, in __call__
    return app(environ, start_response)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/cardinal_pythonlib/wsgi/reverse_proxied_mw.py", line 555, in __call__
    return self.app(environ, start_response)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/router.py", line 270, in __call__
    response = self.execution_policy(environ, self)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/router.py", line 279, in default_execution_policy
    return request.invoke_exception_view(reraise=True)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/view.py", line 778, in invoke_exception_view
    reraise_(*exc_info)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/compat.py", line 179, in reraise
    raise value
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/router.py", line 277, in default_execution_policy
    return router.invoke_request(request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/router.py", line 249, in invoke_request
    response = handle_request(request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/tweens.py", line 43, in excview_tween
    response = _error_handler(request, exc)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/tweens.py", line 17, in _error_handler
    reraise(*exc_info)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/compat.py", line 179, in reraise
    raise value
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/tweens.py", line 41, in excview_tween
    response = handler(request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/router.py", line 148, in handle_request
    registry, request, context, context_iface, view_name
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/view.py", line 667, in _call_view
    response = view_callable(context, request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/viewderivers.py", line 325, in secured_view
    return view(context, request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/viewderivers.py", line 275, in wrapper
    response = view(context, request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/viewderivers.py", line 401, in viewresult_to_response
    result = view(context, request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/viewderivers.py", line 144, in _requestonly_view
    response = view(request)
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/webview.py", line 1643, in serve_report
    return report.get_response(req)
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_report.py", line 553, in get_response
    return super().get_response(req)
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_report.py", line 310, in get_response
    return self.render_html(req=req)
  File "/home/martinb/workspace/camcops/server/camcops_server/tasks/apeq_cpft_perinatal.py", line 246, in render_html
    request=req
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/renderers.py", line 138, in render_to_response
    result = helper.render_to_response(value, None, request=request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/renderers.py", line 474, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid/renderers.py", line 470, in render
    result = renderer(value, system_values)
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_pyramid.py", line 552, in __call__
    reraise(MakoRenderingException(errtext), None, exc_info[2])
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/pyramid_mako/compat.py", line 74, in reraise
    raise value.with_traceback(tb)
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_pyramid.py", line 546, in __call__
    result = template.render_unicode(**system)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/template.py", line 479, in render_unicode
    self, self.callable_, args, data, as_unicode=True
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/runtime.py", line 885, in _render
    return context._pop_buffer().getvalue()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/util.py", line 169, in getvalue
    return self.delim.join(self.data)
pyramid_mako.MakoRenderingException:

Traceback (most recent call last):
  File "/home/martinb/workspace/camcops/server/camcops_server/cc_modules/cc_pyramid.py", line 546, in __call__
    result = template.render_unicode(**system)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/template.py", line 479, in render_unicode
    self, self.callable_, args, data, as_unicode=True
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/runtime.py", line 885, in _render
    return context._pop_buffer().getvalue()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/util.py", line 169, in getvalue
    return self.delim.join(self.data)
TypeError: sequence item 350: expected str instance, int found

The problem seems to be that in disabling the HTML escaping (h filter) with n, we are also disabling conversion of int to str.

https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/templates/snippets/table.mako#L42 https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/templates/snippets/table.mako#L46

We don't need to disable filtering of ${col_index} but my understanding from https://docs.makotemplates.org/en/latest/filtering.html is that n should disable the default filters. In our case that the default filters are just ["h"] https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/cc_modules/cc_pyramid.py#L329. However, If I change | n to | n,h (just disable h) in table.mako the problem goes away.

RudolfCardinal commented 3 years ago

Thanks! In this case, in table.mako, col_index is guaranteed to be an int. So if that's the problem, we could use ${str(col_index) | n } (string conversion in Python, disable default filtering) as the likely fastest option. But I'm a bit puzzled -- when Mako renders an integer in a template, what else can it do except convert it to a string (since everything is being converted to a string)? Is it col_index that's causing the problem, or table content further down (via val), or both?

martinburchell commented 3 years ago

I think we can just use ${col_index}.

There's a problem with ${ val | n } as well. An integer value ends up in one of the cells.

RudolfCardinal commented 3 years ago

OK. So ${ str(val) | n } for the unescaped version?

martinburchell commented 3 years ago

Yes or ${ val | n,h } and I'd like to investigate if this is a bug in Mako.

martinburchell commented 3 years ago

I meant ${ val | n,str } above and in the original description

martinburchell commented 3 years ago

Upstream tickets:

https://github.com/sqlalchemy/mako/issues/272 https://github.com/sqlalchemy/mako/issues/171

martinburchell commented 3 years ago
>>> from mako.template import Template
>>> t = Template("${ 0 | n}", default_filters=["h"])
>>> t.render()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/template.py", line 473, in render
    return runtime._render(self, self.callable_, args, data)
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/runtime.py", line 885, in _render
    return context._pop_buffer().getvalue()
  File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/util.py", line 169, in getvalue
    return self.delim.join(self.data)
TypeError: sequence item 0: expected str instance, int found
>>> t = Template("${ 0 | n,str}", default_filters=["h"])
>>> t.render()
'0'