Custom features¶
New in version 0.5.0.
Architect supports custom features which can be used to add completely new functionality to a model or to redefine some built-in feature’s behaviour. A completely new feature can be created and installed as the following:
from architect.orms.bases import BaseFeature
class CustomFeature(BaseFeature):
orm = 'django'
name = 'my_feature'
decorate = ('bar',)
def foo(self):
"""Does something with a model"""
pass
@staticmethod
def _decorate_bar(method):
"""Decorates model's bar method"""
def wrapper(instance, *args, **kwargs):
# Do something before method invocation
method(instance, *args, **kwargs)
# Do something after method invocation
return wrapper
@architect.install('my_feature')
class Model(object):
pass
To be considered a feature class, a class should inherit from a BaseFeature
or any other
class that itself inherits from a BaseFeature
and implement orm
and name
attributes.
orm
attribute is used to identify to which ORM this feature belongs to and name
attribute
is used by Architect to identify this feature in an internal feature registry. If a feature class
should be considered a base for other feature classes simply don’t set an orm
attribute and
it will defaults to None
which tells Architect that this is a base feature class.
API ¶
BaseFeature
class provides the following methods and attributes:
-
orm
= None¶ which orm this feature belongs to
-
name
= None¶ name that will be used to access this feature
-
decorate
= ()¶ model methods that should be decorated by feature decorators
-
dependencies
= ()¶ features that this feature depends on
-
__init__
(model_obj, model_cls, **options)¶ Parameters: - model_obj (object) – (required). Model instance object to work with.
- model_cls (class) – (required). Model class to work with.
- options (dictionary) – (optional). Feature options if any.