Changeset 336 in main


Ignore:
Timestamp:
09/27/11 09:19:00 (8 years ago)
Author:
pcosquer
Message:

move MetaController? and get_controller to a new module

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/controllers.rst

    r335 r336  
    44:mod:`controllers` --- Controllers 
    55==================================== 
    6  
    7 .. toctree:: 
    8     :maxdepth: 2 
    9     
    10     controllers_plmobject 
    11     controllers_part 
    12     controllers_document 
    13     controllers_user 
    14  
    156 
    167 
     
    2011    :undoc-members: 
    2112 
     13.. toctree:: 
     14    :maxdepth: 2 
     15    
     16    controllers_base 
     17    controllers_plmobject 
     18    controllers_part 
     19    controllers_document 
     20    controllers_user 
    2221 
  • trunk/openPLM/plmapp/controllers/__init__.py

    r335 r336  
    4141 
    4242To get a suitable controller for a ``PLMObject`` instances use  
    43 :func:`~plmapp.controllers.plmobject.get_controller`. 
     43:func:`~plmapp.controllers.base.get_controller`. 
    4444For example, `get_controller('Part')` returns :class:`.PartController`. 
    4545 
     
    142142     
    143143    * functions: 
    144         :func:`~plmapp.controllers.plmobject.get_controller` 
     144        :func:`~plmapp.controllers.base.get_controller` 
    145145 
    146146""" 
    147 from openPLM.plmapp.controllers.plmobject import get_controller, PLMObjectController, \ 
     147from openPLM.plmapp.controllers.base import MetaController, get_controller 
     148from openPLM.plmapp.controllers.plmobject import PLMObjectController, \ 
    148149    rx_bad_ref 
    149150from openPLM.plmapp.controllers.part import PartController 
  • trunk/openPLM/plmapp/controllers/plmobject.py

    r335 r336  
    3737    PromotionError 
    3838from openPLM.plmapp.utils import level_to_sign_str 
    39  
    40 _controller_rx = re.compile(r"(?P<type>\w+)Controller") 
    41  
    42 class MetaController(type): 
    43     """ 
    44     Metaclass used to register a controller and get a controller associated 
    45     to a type (see :meth:`get_controller`). 
    46  
    47     See `how-to-add-a-controller`_ for more explanations. 
    48     """ 
    49     #: dict<type_name(str) : Controller(like :class:`PLMObjectController`)> 
    50     controllers_dict = {} 
    51  
    52     def __new__(mcs, name, bases, attrs): 
    53         cls = type.__new__(mcs, name, bases, attrs) 
    54         if "MANAGED_TYPE" in attrs: 
    55             managed = attrs["MANAGED_TYPE"].__name__ 
    56         else: 
    57             m = _controller_rx.match(name) 
    58             if m: 
    59                 managed = m.group("type") 
    60             else: 
    61                 # the controller is not interresting 
    62                 return cls 
    63         mcs.controllers_dict[managed] = cls 
    64         return cls 
    65  
    66     @classmethod 
    67     def get_controller(cls, type_name): 
    68         """ 
    69         Returns the a controller (subclass of :class:`PLMObjectController`)  
    70         associated to *type_name* (a string). 
    71  
    72         For example, ``get_controller("Part")`` will return the class  
    73         :class:`.PartController`. 
    74         """ 
    75         if type_name in cls.controllers_dict: 
    76             return cls.controllers_dict[type_name] 
    77         else: 
    78             # get his model and return his parent controller 
    79             if type_name == "PLMObject": 
    80                 # just a security to prevent an infinite recursion 
    81                 return PLMObjectController 
    82             else: 
    83                 model = models.get_all_plmobjects()[type_name] 
    84                 parents = [p for p in model.__bases__  
    85                                 if issubclass(p, models.PLMObject)] 
    86                 return cls.get_controller(parents[0].__name__) 
    87  
    88 #: shortcut for :meth:`MetaController.get_controller` 
    89 get_controller = MetaController.get_controller 
     39from openPLM.plmapp.controllers.base import MetaController 
    9040 
    9141def permission_required(func=None, role="owner"): 
  • trunk/openPLM/plmapp/controllers/user.py

    r333 r336  
    3535import openPLM.plmapp.models as models 
    3636from openPLM.plmapp.exceptions import PermissionError 
     37from openPLM.plmapp.controllers.base import MetaController 
    3738 
    3839class UserController(object): 
     
    5556 
    5657    """ 
     58 
     59    __metaclass__ = MetaController 
    5760 
    5861    def __init__(self, obj, user): 
Note: See TracChangeset for help on using the changeset viewer.