Search code examples
pyramid

Dynamically translating Pyramid's view arguments


Let's say that I have a view:

my_view(request: Request, uuid: UUID):
     pass

I'd like to automatically translate all uuid objects to base64-based strings, so that the framework user doesn't need to manually call slug_to_uuid() and uuid_to_slug. This would apply to all views and is based on Python 3 argument signature type hinting (if it hints it's UUID object then you want to translate it to a string and back).

  • route_url('viewname', uuid=my_uuid) would encode UUID arguments as base64 string

  • Routing machinery would read Python 3 signature of a view function and translate string back to UUID object before calling the view

What hooks and approaches I can take this in Pyramid?

  • Hooks to route_url

  • Hooks in router to translate incoming view arguments using custom predicates, tweens, etc.


Solution

  • You're asking about 2 workflows. 1) translate incoming data. 2) translate outgoing data in urls.

    In Pyramid the translation of incoming data should be done by decorators, view mappers or possibly predicates. You'll have to decide which fits your use-case best. Tweens don't really make sense because they happen prior to creation of the matchdict.

    As far as url generation, the only hook available for route_url is a pregenerator on the route which can take the kwargs and translate them.