websauna.system.core.viewconfig module

websauna.system.core.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.system.core.viewconfig import view_overrides

# We define some model structure for which we are going to create edit views
class BaseModel:
     pass


class House(BaseModel):
     pass


class Car(BaseModel):
     pass


# This is the base edit view class. You can use it as is or subclass it to override parts of it.
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


# This overrides edit() method from GenericEdit.edit() with a different @view_config(context) parameters.
# Otherwise @view_config() parameters are copied as is.
@view_overrides(context=Car)
class CarEdit(GenericEdit):
    widgets = ["name", "price", "color", "year]


 # Some dummy traversing which shows how view are mapped to traversing context
 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.

class websauna.system.core.viewconfig.view_overrides(categories=None, **kwargs)[source]

Bases: object

A class decorator which overrides chosen view_config arguments from the parent class.