View override configuration pattern for Pyramid framework
Project description
Features
websauna.viewconfig provides a class decorator @view_overrides which allows subclasses to partially override parent class view configuration. The primary use case is to have generic class views which you can subclass for more specific use cases. The pattern is very powerful when combined with Pyramid’s travesing contexts.
Example:
There is a generic edit view for all of your models called GenericEdit
But for a specific model, let’s say Car, you want to override parts of GenericEdit e.g. to include a widget to handle car colour selection. Other models, like House and Person, still use GenericEdit.
The code would be:
from websauna.viewconfig import view_overrides class BaseModel: pass class House(BaseModel): pass class Car(BaseModel): pass class GenericEdit: widgets = ["name", "price"] def __init__(self, context, request): self.context = context self.request = request @view_config(name="edit", context=BaseModel) def edit(self): # Lot's of edit logic code goes here which # we don't want to repeat.... pass @view_overrides(context=Car) class CarEdit(GenericEdit): widgets = ["name", "price", "color", "year] class Root: """Pyramid's traversing root.""" def __init__(self, request): pass def __getitem__(self, name): if is_car(name): return Car(name) else: return House(name):
Now one could traverse edit views like:
/my_house/edit /my_car/edit
… and the latter would serve car-specific edit form.
The @view_overrides pattern can be also used with routing based views to override e.g route_name and renderer (the template of subclass view). For those examples, please see testing source code.
The implementation is based on venusian.lift() function with the overriding bits added in.
Development and tests
To run tests:
pip install -e ".[test]" py.test websauna/viewconfig
To run coverage:
py.test --cov websauna.viewconfig --cov-report xml
All the testing on drone.io:
pip install -U pytest pip install -e ".[test]" py.test --cov websauna.viewconfig --cov-report xml codecov --token="xxx"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.