Best model to load ajax pyramid?

I have read through various renderers or overriding the renderer , but I wonder the best way to deal with this pattern.

Right now, in my opinion, I am returning a set of elements to the template:

@view_config( route_name = 'name', permission = 'perm', renderer = 'r.mako' ) def r( request ): items = get_items() return { 'items': items } 

Now I want the ajax version to simply display a subset as well as some data. My current working code is:

 @view_config( route_name = 'name', permission = 'perm', renderer = 'r.mako' ) def r( request ): items = get_items() if ajax: return Response( to_json( { 'data1': 1, 'data2': 2, 'data3': 3, 'html': renderers.render( 'shortr.mako', { 'items': items }, request ) } ) return { 'items': items } 

I assume that specifically I am wondering if there is a cleaner way to override the renderer and then wrap it with something, without explicitly invoking the rendering and ensuring that I get the right or request as a parameter. thanks

+4
source share
2 answers

I would suggest using 2 types that will allow you to apply different โ€œviews and sensationsโ€ (answers) to the same data.

 def get_items(request): return {} # values that you can pick and choose from in each view @view_config(route_name='name', permission='perm', xhr=True, renderer='json') def r_ajax(request): items = get_items(request) return { 'data1': 1, 'data2': 2, 'data3': 3, 'html': renderers.render('shortr.mako', {'items': items}, request), } @view_config(route_name='name', permission='perm', renderer='r.mako') def r_html(request): items = get_items(request) return items 

If you are afraid of repeating things for view configuration, Pyramid 1.3 comes with a great new feature in its classes:

 @view_defaults(route_name='name', permission='perm') class R(object): def __init__(self, request): self.request = request self.items = # ... @view_config(xhr=True, renderer='json') def ajax(request): return { 'data1': 1, 'data2': 2, 'data3': 3, 'html': renderers.render('shortr.mako', {'items': items}, request), } @view_config(renderer='r.mako') def html(request): return self.items 
+8
source

I am not familiar with the pyramid or the โ€œr.makoโ€ link, but you can probably connect to the request before the controller is called using a special function that checks the Accepts headers of the request, text / javascript 'or' application / json 'in as the main accept, and then set the flag in the request object (or this method has been accounted for for use in your r function).

Then do your own renderer to handle either parsing with mako or flushing json string

 # pseudo-code def before_controller(request, response): if 'text/html' in request.headers.accepts: request.is_json = False elif 'application/json' in request.headers.accepts: response.headers.set('Content-type', 'application/json') request.is_json = True # pseudo-code def my_renderer(request, response, result): if 'text/html' in request.headers.accepts: return # render html elif 'application/json' in request.headers.accepts: response.headers.set('Content-type', 'application/json') return json.dumps(result) # def r(request): items = get_items() if request.json: pass # update items with additional data return {'items': items} # one point of return 

This method also means that there is no additional work with legs, if you do not need to perform additional processing of elements, you simply return the result as usual and get a json object on the other end.

If you cannot connect to the pyramid before calling the controller, you can write a convenience function to call is_json (request) and use it in the controller and renderer to determine the output and adjust the content header

+1
source

Source: https://habr.com/ru/post/1387437/


All Articles