opal.core.discoverable module contans base discoverable classes for creating
features which can be re-used and subcalssd.
The base discoverable class which provides functionality to all discoverables.
The name of the python module in which Opal will look for instances of this
discoverable. For instance in the following feature, Opal would look in every
installed app at
app/ciao.py for subclasses of
Greeting, even if that module
had not been directly imported already.
from opal.core import discoverable class Greeting(discoverable.DiscoverableFeature): module_name = 'ciao'
The human readable name for an instance of a discoverable. Use this in the UI when showing the discoverable to users.
The slug for this discoverable used in URLs. Note that Opal internally useses
get_slug method below to allow more dynamic slugs.
Stub function that is called at class definition to determine whether an instance is valid. Useful for enforcing business logic and expectations for third party features. Expected to raise an exception if something is wrong.
Return the slug for this feature. Defaults to returning the
slug property if
it is set, or running a slugify function on the
display_name. If neither is
set it will raise a ValueError.
MyFeature.get_slug() # -> "my-feature"
Return a generator that yields all implementations of a discoverable.
for g in Greeting.list(): print g.display_name "Bonjour" "Hola" "Namaste" "Salaam"
Find instances of a feature based on the value of their attributes. You may pass attributes of features as keyword arguments. Returns a list of matching features.
Greeting.filter(module_name='ciao') # Essentially the same as .list() Greeting.filter(slug="bonjour", display_name="Bonjour") # -> [BonjourGreeting]
A mixin class that provides ordering for features which will be respected
An integer which is used to sort features.
Returns instances of the feature orderd by
class Greeting(discoverable.DiscoverableFeature, discoverable.SortableFeature): module_name='ciao' class Namaste(Greeting): order = 2 class Hello(Greeting): order = 32098239021 class Bonjour(Greeting): order = 1 for f in Greeting.list(): print f, f.order # <class '*.*.Bonjour'>, 1 # <class '*.*.Namaste'>, 2 # <class '*.*.Hello'>, 3
A mixin class that provides an interface for restricting access to features based on user.
class Greeting(discoverable.DiscoverableFeature, discoverable.RestrictableFeature): module_name='ciao'
Generator method that yields all instances of this discoverable which are visible to a given user.
for f in Greeting.for_user(user): print f # <class '*.*.Bonjour'> # <class '*.*.Namaste'> # <class '*.*.Hello'>
Predicate function used to determine whehter in individual feature should be visible to a given user. Defaults to simply returning True.
Features may override this to provide restricted access as required.
Hello.visible_to(user) # -> True