Behaviors

Usage Notes

Because behaviors are ultimately implemented using a special metaclass, any model inheritance involving behaviors must come before any other parent class which inherits from Django’s built in metaclass.

Example Correct and Incorrect Usage:

class MyBaseModel(models.Model):
    pass

# Incorrect Usage
class MyChildModel(MyBaseModel, Timestampable):
    pass

# Correct Usage
class MyChildModel(Timestampable, MyBaseModel):
    pass

Built in Behaviors

class fusionbox.behaviors.Behavior(*args, **kwargs)[source]

Base class for all Behaviors

Behaviors are implemented through model inheritance, and support multi-inheritance as well. Each behavior adds a set of default fields and/or methods to the model. Field names can be customized like example B.

EXAMPLE A:

class MyModel(FooBehavior):
    pass

MyModel will have whatever fields FooBehavior adds with default field names.

EXAMPLE B:

class MyModel(FooBehavior):
    class FooBehavior:
        bar = 'qux'
        baz = 'quux'

MyModel will have the fields from FooBehavior added, but the field names will be ‘qux’ and ‘quux’ respectively.

EXAMPLE C:

class MyModel(FooBehavior, BarBehavior):
    pass

MyModel will have the fields from both FooBehavior and BarBehavior, each with default field names. To customizing field names can be done just like it was in example B.

classmethod merge_parent_settings()[source]

Every behavior’s settings are stored in an inner class whose name matches its behavior’s name. This method implements inheritance for those inner classes.

classmethod modify_schema()[source]

Hook for behaviors to modify their model class just after it’s created

fusionbox.behaviors.ManagedQuerySet

alias of QuerySetManagerModel

class fusionbox.behaviors.MetaBehavior[source]

Base Metaclass for Behaviors

class fusionbox.behaviors.Publishable(*args, **kwargs)[source]

Base class for adding publishable behavior to a model.

Added Fields:
Field 1:
field: DateTimeField(default=datetime.datetime.now, help_text=’Selecting a future date will automatically publish to the live site on that date.’) description: The date that the model instance will be made available to the PublishableManager’s query set default_name: publish_at
Field 2:
field: DateTimeField(default=datetime.datetime.now, help_text=’Selecting a future date will automatically publish to the live site on that date.’) description: setting to False will automatically draft the instance, making it unavailable to the PublishableManager’s query set default_name: is_published
Added Managers:
PublishableManager:

description: overwritten get_queryset() function to only fetch published instances. name: published usage:

class Blog(Publishable): ...

all_blogs = Blog.objects.all() published_blogs = Blog.published.all()

class fusionbox.behaviors.PublishableManager[source]

Manager for publishable behavior

class fusionbox.behaviors.QuerySetManagerModel(*args, **kwargs)[source]

This behavior is meant to be used in conjunction with fusionbox.db.models.QuerySetManager

A class which inherits from this class will any inner QuerySet classes found in the mro merged into a single class.

Given the following Parent class:

class Parent(models.Model):
    class QuerySet(QuerySet):
        def get_active(self):
            ...

The following two Child classes are equivalent:

class Child(Parent):
    class QuerySet(Parent.QuerySet):
        def get_inactive(self):
            ...

class Child(QuerySetManagerModel, Parent):
    class QuerySet(QuerySet):
        def get_inactive(self):
            ...
classmethod merge_parent_settings()[source]

Automatically merges all parent QuerySet classes to preserve custom defined QuerySet methods

class fusionbox.behaviors.SEO(*args, **kwargs)[source]

Base class for adding seo behavior to a model.

Added Fields:
Field 1:
field: CharField(max_length = 255) description: Char field intended for use in html <title> tag. validation: Max Length 255 Characters default_name: seo_title
Field 2:
field: TextField() description: Text field intended for use in html <meta name=’description’> tag. default_name: seo_description
Field 3:
field: TextField() description: Text field intended for use in html <meta name=’keywords’> tag. validation: comma separated text strings default_name: seo_keywords
formatted_seo_data(title='', description='', keywords='')[source]

A string containing the model’s SEO data marked up and ready for output in HTML.

class fusionbox.behaviors.Timestampable(*args, **kwargs)[source]

Base class for adding timestamping behavior to a model.

Added Fields:
Field 1:
field: DateTimeField(default=now) description: Timestamps set at the creation of the instance default_name: created_at
Field 2:
field: DateTimeField(auto_now=True) description: Timestamps set each time the save method is called on the instance default_name: updated_at
class fusionbox.behaviors.Validation(*args, **kwargs)[source]

Base class for adding complex validation behavior to a model.

By inheriting from Validation, your model can have validate and validate_<field> methods.

validate() is for generic validations, and for NON_FIELD_ERRORS, errors that do not belong to any one field. In this method you can raise a ValidationError that contains a single error message, a list of errors, or - if the messages are associated with a field - a dictionary of field-names to message-list.

You can also write validate_<field> methods for any columns that need custom validation. This is for convience, since it is easier and more intuitive to raise an ‘invalid value’ from within one of these methods, and have it automatically associated with the correct field.

Even if you don’t implement custom validation methods, Validation changes the normal behavior of save so that validation always occurs. This makes it easy to write APIs without having to understand the clean, full_clean, and clean_fields() methods that must called in django. If a validation error occurs, the exception will not be caught, it is up to you to catch it in your view or API.

clean_fields(exclude=None)[source]

Must be manually called in Django.

Calls any validate_<field> methods defined on the Model.

is_valid()[source]

Returns True or False

validation_errors()[source]

Returns a dictionary of errors.

fusionbox.behaviors.now()

[tz] -> new datetime with tz’s local day and time.

class fusionbox.behaviors.MetaBehavior[source]

Base Metaclass for Behaviors