Table des matières

Sujet précédent

5. Modules (openPLM.plmapp package)

Sujet suivant

5.1.5. controllers.base — Base class for controllers

Cette page

Autres langues

5.1. controllers — Controllers

5.1.1. Introduction

This module contains utilities to manage a PLMObject. It provides a new class, PLMObjectController, which can be used to modify its attributes, promote/demote/revise it...

All modifications are recorded in a history.

5.1.2. How to use this module

The controller for a PLMObject is PLMObjectController. All subclasses of PLMObject may have their own controller to add functionalities or redefined default behaviors.

To get a suitable controller for a PLMObject instances use get_controller(). For example, get_controller(‘Part’) returns PartController.

If you have a PLMObject and an User, you can instanciate a controller. For example:

>>> # obj is a PLMObject and user an User
>>> controller_cls = get_controller(obj.type)
>>> controller = controller_cls(obj, user)

Then you can modify/access the attributes of the PLMObject and save the modifications:

>>> controller.name = "New Name"
>>> "type" in controller.attributes
True
>>> controller.owner = user
>>> # as with django models, you should call *save* to register modifications
>>> controller.save()

You can also promote/demote the PLMObject:

>>> controller.state.name
'draft'
>>> controller.promote()
>>> controller.state.name
'official'
>>> controller.demote()
>>> controller.state.name
'draft'

There are also two classmethods which can help to create a new PLMobject:

  • create()
  • create_from_form()

This two methods return an instance of PLMObjectController (or one of its subclasses).

Moreover, the method create_from_form() can be used to update informations from a form created with plmapp.forms.get_modification_form().

5.1.3. How to add a controller

If you add a new model which inherits from PLMObject or one of its subclasses, you may want to add your own controller.

You just have to declare a class which inherits (directly or not) from PLMObjectController. To associate this class with your models, there are two possibilities:

  • if your class has an attribute MANAGED_TYPE, its value (a class) will be used. For example:

    class MyController(PLMObjectController):
        MANAGED_TYPE = MyPart
        ...
    

    MyController will be associated to MyPart and get_controller("MyPart") will return MyController.

  • if MANAGED_TYPE is not defined, the name class will be used: for example, MyPartController will be associated to MyPart. The rule is simple, it is just the name of the model followed by “Controller”. The model may not be defined when the controller is written.

If a controller exploits none of theses possibilities, it will still work but it will not be associated to a type and will not be used by existing views.

Note

This association is possible without any registration because PLMObjectController metaclass is MetaController.

If your controller has its own attributes, you must redefine the variable __slots__ and add its attributes:

class MyController(PLMObjectController):

    __slots__ = PLMObjectController.__slots__ + ("my_attr", )

    def __init__(self, object, user):
        super(MyController, self).__init__(object, user)
        self.my_attr = "value"

By default, a controller inherits its __slots__ attribute from its parent (this is set by MetaController).

5.1.4. Classes and functions

This module defines several classes, here is a summary: