sacrud / ps_crud

Extension for pyramid_sacrud which provides a templates for CRUD.
MIT License
0 stars 1 forks source link

Using UUID as table primary key cause TypeError: UUID() is not JSON serializable #1

Open cjltsod opened 8 years ago

cjltsod commented 8 years ago

I'm using UUID as my tables' primary key with sqlalchemy and pyramid. The UUID is not json serializable. (JSON package doesn't recognize the UUID type)

Here's my traceback:

Traceback (most recent call last):
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_debugtoolbar/toolbar.py", line 215, in toolbar_tween
    response = _handler(request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_debugtoolbar/panels/performance.py", line 57, in resource_timer_handler
    result = handler(request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/tweens.py", line 62, in excview_tween
    reraise(*attrs['exc_info'])
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/compat.py", line 148, in reraise
    raise value
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/tweens.py", line 22, in excview_tween
    response = handler(request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 101, in tm_tween
    reraise(*exc_info)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_tm/compat.py", line 15, in reraise
    raise value
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 83, in tm_tween
    response = handler(request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/router.py", line 158, in handle_request
    view_name
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/view.py", line 547, in _call_view
    response = view_callable(context, request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/viewderivers.py", line 393, in attr_view
    return view(context, request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/viewderivers.py", line 371, in predicate_wrapper
    return view(context, request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/viewderivers.py", line 413, in viewresult_to_response
    result = view(context, request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/viewderivers.py", line 137, in _class_view
    response = getattr(inst, attr)()
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ps_alchemy/views.py", line 94, in list_view
    self.context.renderer, params, request=self.request
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/renderers.py", line 144, in render_to_response
    result = helper.render_to_response(value, None, request=request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/renderers.py", line 455, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid/renderers.py", line 451, in render
    result = renderer(value, system_values)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_jinja2-2.6.2-py3.5.egg/pyramid_jinja2/__init__.py", line 265, in __call__
    return template.render(system)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/Jinja2-2.8-py3.5.egg/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/Jinja2-2.8-py3.5.egg/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/Jinja2-2.8-py3.5.egg/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ps_alchemy/templates/ps_alchemy/crud/list.jinja2", line 1, in top-level template code
    {% extends "/ps_crud/list.jinja2" %}
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ps_crud/templates/ps_crud/list.jinja2", line 20, in top-level template code
    {% set
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_sacrud-0.3.3-py3.5.egg/pyramid_sacrud/templates/sacrud/redefineme.jinja2", line 1, in top-level template code
    {% extends "sacrud/base.jinja2" %}
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyramid_sacrud-0.3.3-py3.5.egg/pyramid_sacrud/templates/sacrud/base.jinja2", line 102, in top-level template code
    {% block body %}{% endblock %}
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ps_crud/templates/ps_crud/list.jinja2", line 78, in block "body"
    {% set id=ps_get_id(row) %}
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ps_alchemy/resources.py", line 65, in _get_id
    return pk_to_list(obj, json)
  File "/.pyenv/versions/3.5.2/lib/python3.5/site-packages/sacrud/common.py", line 145, in pk_to_list
    return json.dumps(pk_list)
  File "/.pyenv/versions/3.5.2/lib/python3.5/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/.pyenv/versions/3.5.2/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/.pyenv/versions/3.5.2/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "/.pyenv/versions/3.5.2/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: UUID() is not JSON serializable

I can add extra parameter json=False at following line to avoid the exception. https://github.com/sacrud/ps_crud/blob/master/ps_crud/templates/ps_crud/list.jinja2#L78 It cause ps_alchemy generate original instance instead of json. I'm not sure if it cause any other side effect then. Is that adding extra parameter a good way or not?

uralbash commented 8 years ago

@cjltsod so it looks like a bug, I'm soon to rewrite ps_crud and ps_alchemy, I hope that this error will disappear. Thanks for the report.