This chapter deals with some of the core concepts of the CubicWeb framework which make it different from other frameworks (and maybe not easy to grasp at a first glance). To be able to do advanced development with CubicWeb you need a good understanding of what is explained below.
This chapter goes deep into details. You don’t have to remember them all but keep it in mind so you can go back there later.
An overview of AppObjects, the VRegistry and Selectors is given in the Registries and application objects chapter.
The VRegistry can be seen as a two-level dictionary. It contains all dynamically loaded objects (subclasses of The AppObject class) to build a CubicWeb application. Basically:
A registry holds a specific kind of application objects. There is for instance a registry for entity classes, another for views, etc...
The VRegistry has two main responsibilities:
On startup, CubicWeb loads application objects defined in its library and in cubes used by the instance. Application objects from the library are loaded first, then those provided by cubes are loaded in dependency order (e.g. if your cube depends on an other, objects from the dependency will be loaded first). The layout of the modules or packages in a cube is explained in Standard structure for a cube.
For each module:
Note
Once the function registration_callback(vreg) is implemented in a module, all the objects from this module have to be explicitly registered as it disables the automatic objects registration.
Here are the registration methods that you can use in the registration_callback to register your objects to the VRegistry instance given as argument (usually named vreg):
register all objects given. Objects which are not from the module modname or which are in butclasses won’t be registered.
Typical usage is:
vreg.register_all(globals().values(), __name__, (ClassIWantToRegisterExplicitly,))
So you get partially automatic registration, keeping manual registration for some object (to use register_and_replace() for instance)
register obj application object into registryname or obj.__registry__ if not specified, with identifier oid or obj.__regid__ if not specified.
If clear is true, all objects with the same identifier will be previously unregistered.
Examples:
# web/views/basecomponents.py
def registration_callback(vreg):
# register everything in the module except SeeAlsoComponent
vreg.register_all(globals().values(), __name__, (SeeAlsoVComponent,))
# conditionally register SeeAlsoVComponent
if 'see_also' in vreg.schema:
vreg.register(SeeAlsoVComponent)
In this example, we register all application object classes defined in the module except SeeAlsoVComponent. This class is then registered only if the ‘see_also’ relation type is defined in the instance’schema.
# goa/appobjects/sessions.py
def registration_callback(vreg):
vreg.register(SessionsCleaner)
# replace AuthenticationManager by GAEAuthenticationManager
vreg.register_and_replace(GAEAuthenticationManager, AuthenticationManager)
# replace PersistentSessionManager by GAEPersistentSessionManager
vreg.register_and_replace(GAEPersistentSessionManager, PersistentSessionManager)
In this example, we explicitly register classes one by one:
If at some point we register a new appobject class in this module, it won’t be registered at all without modification to the registration_callback implementation. The previous example will register it though, thanks to the call to the register_all method.
Now that we have all application objects loaded, the question is : when I want some specific object, for instance the primary view for a given entity, how do I get the proper object ? This is what we call the selection mechanism.
As explained in the The Core Concepts of CubicWeb section:
Note
When no single object has the highest score, an exception is raised in development mode to let you know that the engine was not able to identify the view to apply. This error is silenced in production mode and one of the objects with the highest score is picked.
In such cases you would need to review your design and make sure your selectors or appobjects are properly defined. Such an error is typically caused by either forgetting to change the __regid__ in a derived class, or by having copy-pasted some code.
For instance, if you are selecting the primary (__regid__ = ‘primary’) view (__registry__ = ‘views’) for a result set containing a Card entity, two objects will probably be selectable:
Other primary views specific to other entity types won’t be selectable in this case. Among selectable objects, the is_instance(‘Card’) selector will return a higher score since it’s more specific, so the correct view will be selected as expected.
Here is the selection API you’ll get on every registry. Some of them, as the ‘etypes’ registry, containing entity classes, extend it. In those methods, *args, **kwargs is what we call the context. Those arguments are given to selectors that will inspect their content and return a score accordingly.
return the most specific object among those with the given oid according to the given context.
raise ObjectNotFound if not object with id <oid> in <registry>
raise NoSelectableObject if not object apply
return object with the oid identifier. Only one object is expected to be found.
raise ObjectNotFound if not object with id <oid> in <registry>
raise AssertionError if there is more than one object there
The AppObject class is the base class for all dynamically loaded objects (application objects) accessible through the vregistry.
We can find a certain number of attributes and methods defined in this class and common to all the application objects.
This is the base class for CubicWeb application objects which are selected according to a context (usually at least a request and a result set).
The following attributes should be set on concret appobject classes:
Moreover, the __abstract__ attribute may be set to True to indicate that a class is abstract and should not be registered.
At selection time, the following attributes are set on the instance:
And also the following, only if rset is found in arguments (in which case rset/row/col will be removed from cwextra_kwargs):
Note
do not inherit directly from this class but from a more specific class such as AnyEntity, EntityView, AnyRsetView, Action...
to be recordable, a subclass has to define its registry (attribute __registry__) and its identifier (attribute __regid__). Usually you don’t have to take care of the registry since it’s set by the base class, only the identifier id
application objects are designed to be loaded by the vregistry and should be accessed through it, not by direct instantiation, besides to use it as base classe.
When we inherit from AppObject (even not directly), you always have to use super() to get the methods and attributes of the superclasses, and not use the class identifier.
For example, instead of writting:
class Truc(PrimaryView):
def f(self, arg1):
PrimaryView.f(self, arg1)
You must write:
class Truc(PrimaryView):
def f(self, arg1):
super(Truc, self).f(arg1)
Selectors are scoring functions that are called by the registry to tell whenever an appobject can be selected in a given context. Selector sets are for instance the glue that tie views to the data model. Using them appropriately is an essential part of the construction of well behaved cubes.
Of course you may have to write your own set of selectors as your needs grows and you get familiar with the framework (see CustomSelectors).
Here is a description of generic selectors provided by CubicWeb that should suit most of your needs.
Those selectors are somewhat dumb, which doesn’t mean they’re not (very) useful.
Return the score given as parameter, with a default score of 0.5 so any other selector take precedence.
Usually used for appobjects which can be selected whatever the context, or also sometimes to add arbitrary points to a score.
Take care, yes(0) could be named ‘no’...
Those selectors are looking for a result set in the context (‘rset’ argument or the input context) and match or not according to its shape. Some of these selectors have different behaviour if a particular cell of the result set is specified using ‘row’ and ‘col’ arguments of the input context or not.
Those selectors are looking for either an entity argument in the input context, or entity found in the result set (‘rset’ argument or the input context) and match or not according to entity’s (instance or class) properties.
Those selectors are looking for properties of the user issuing the request.
Those selectors are looking for properties of web request, they can not be used on the data repository side.
You’ll also find some other (very) specific selectors hidden in other modules than cubicweb.selectors.