Skip to main content

View override configuration pattern for Pyramid framework

Project description

cov

Downloads

ci

License

Supported Python versions

Latest Version

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"

Author

Mikko Ohtamaa (blog, Facebook, Twitter)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

websauna.viewconfig-0.1.tar.gz (7.2 kB view details)

Uploaded Source

File details

Details for the file websauna.viewconfig-0.1.tar.gz.

File metadata

File hashes

Hashes for websauna.viewconfig-0.1.tar.gz
Algorithm Hash digest
SHA256 01c4670b56232fd5ede9325fcfaf7d1327a69fb4c37244bdc20cfe5162e713b0
MD5 8a40d071108ecc83e1c0a2d30a86da27
BLAKE2b-256 f3aacb7693e89297e4e7e8a655cba0dbd4067c1a0ce75d7eb234ee1592fb8415

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page