Implement a custom View Decorator in Pyramid

# -*- coding: utf-8 -*-
from pyramid.view import view_config


def json_api(**params):
    u"""View decorator for JSON API"""
    settings = dict(renderer='json', xhr=True, _depth=1)
    settings.update(params)

    def wrapped(func):
        return view_config(**settings)(func)

    return wrapped

You can use it like this.

@json_api(route_name="api_user_detail")
def user_detail(request):
    return {"id": 1, "name": "junya"}

Note that you need to increment the _depth parameter by the number of nests in the decorator.

Recommended Posts

Implement a custom View Decorator in Pyramid
Implement a Custom User Model in Django
Implement a date setter in Tkinter
Let's implement a custom prefix on discord.py!
Implement DeepChem's GraphConvLayer in PyTorch's custom layer
Implement DeepChem's GraphGatherLayer in PyTorch's custom layer
How to implement Scroll View in pythonista 1
How to implement a gradient picker in Houdini
Get a panoramic image in Google Street View
Implement Custom Authorizer for Firebase Authentication in Chalice
View drug reviews using a list in Python
Create a custom search command in Splunk (Streaming Command)
Implement similar face search in half a day
Use a custom error page in python / tornado
I want to easily implement a timeout in python
I tried to implement a pseudo pachislot in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
Implement Enigma in python
Write decorator in class
View images in Matplotlib
Implement recommendations in Python
Make a function decorator
Implement XENO in python
Decorator pattern in Java
Use a Property Decorator?
dict in dict Makes a dict a dict
Returns json in Pyramid
Custom sort in Python3
Implement sum in Python
Decorate with a decorator
Implement Traceroute in Python 3
zipimporter error in pyramid
I tried to implement a one-dimensional cellular automaton in Python
How to temporarily implement a progress bar in a scripting language
A story about trying to implement a private variable in Python.