diff --git a/Brie/brie/config/plugins_config.py b/Brie/brie/config/plugins_config.py index a8fb176..fd4f5fc 100644 --- a/Brie/brie/config/plugins_config.py +++ b/Brie/brie/config/plugins_config.py @@ -1,13 +1,13 @@ -from brie.plugins.wifi.controller import * -from brie.plugins.unix.controller import * -from brie.plugins.macauth.controller import * +import brie.lib.plugins_loader as plugins_loader + +plugins = plugins_loader.load() mappings = { "brie.controllers.show.member" : [ - ("wifi", Wifi.show), - ("unix", Unix.show) + plugins.wifi.Wifi.show, + plugins.unix.Unix.show ], "brie.controllers.edit.machine.post" : [ - ("macauth", Mac_auth.add_machine) + plugins.macauth.Macauth.add_machine ] } diff --git a/Brie/brie/controllers/plugins.py b/Brie/brie/controllers/plugins.py new file mode 100644 index 0000000..c604541 --- /dev/null +++ b/Brie/brie/controllers/plugins.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +from tg import session +from tg.controllers import redirect +from tg.decorators import expose, validate +from brie.config import ldap_config, plugins_config +from brie.lib.ldap_helper import * +from brie.lib.aurore_helper import * +from brie.lib.plugins import * +from brie.model.ldap import * + +from brie.controllers import auth +from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController + + +class PluginsController(AuthenticatedBaseController): + + def __init__(self): + for name, controller_module in plugins_config.plugins.__dict__.iteritems(): + direct_controller_name = "DirectController" + + if direct_controller_name in controller_module.__dict__: + self.__dict__[name] = controller_module.__dict__[direct_controller_name]() + #end for + #end def +#end class diff --git a/Brie/brie/controllers/root.py b/Brie/brie/controllers/root.py index ee49d56..777ec6a 100644 --- a/Brie/brie/controllers/root.py +++ b/Brie/brie/controllers/root.py @@ -22,6 +22,7 @@ from brie.controllers.administration import AdministrationController from brie.controllers.error import ErrorController from brie.controllers.registration import RegistrationController from brie.controllers.treasury import TreasuryController +from brie.controllers.plugins import PluginsController __all__ = ['RootController'] @@ -53,6 +54,7 @@ class RootController(BaseController): search = SearchController() registration = RegistrationController(edit) treasury = TreasuryController() + plugins = PluginsController() @expose('brie.templates.index') def index(self): diff --git a/Brie/brie/lib/plugins.py b/Brie/brie/lib/plugins.py index a8c6f84..b9a891d 100644 --- a/Brie/brie/lib/plugins.py +++ b/Brie/brie/lib/plugins.py @@ -20,6 +20,11 @@ class PluginVars: #end class +def plugin_name_from_function(function, suffix = ".controller", prefix = "brie.plugins."): + return function.__module__[len(prefix):-len(suffix)] +#end def + + """ Decorateur plugin, execute toutes les fonctions du scope donné """ def plugins(scope): def plugin(f, *args, **kw): @@ -35,7 +40,9 @@ def plugins(scope): scope_mappings = plugins_config.mappings[scope] - for plugin_name, function in scope_mappings: + for function in scope_mappings: + plugin_name = plugin_name_from_function(function) + plugin_activated = Plugins.get_by_name(user, residence_dn, plugin_name) if plugin_activated is None: @@ -99,7 +106,8 @@ def plugin_action(scope): scope_mappings = plugins_config.mappings[scope] - for plugin_name, function in scope_mappings: + for function in scope_mappings: + plugin_name = plugin_name_from_function(function) plugin_activated = Plugins.get_by_name(user, residence_dn, plugin_name) if plugin_activated is None: diff --git a/Brie/brie/lib/plugins_loader.py b/Brie/brie/lib/plugins_loader.py new file mode 100644 index 0000000..78aee24 --- /dev/null +++ b/Brie/brie/lib/plugins_loader.py @@ -0,0 +1,17 @@ + +class PluginsPackage: + def __init__(self, controllers_dict): + self.__dict__ = controllers_dict +#end class + +def load(): + plugins_import = __import__("brie.plugins") + + controllers_dict = dict() + for sub_package in plugins_import.plugins.__all__: + controller_import = __import__("brie.plugins." + sub_package + ".controller") + controllers_dict[sub_package] = controller_import.plugins.__dict__[sub_package].controller + #end for + + return PluginsPackage(controllers_dict) +#end def diff --git a/Brie/brie/plugins/__init__.py b/Brie/brie/plugins/__init__.py index e69de29..3d4f6de 100644 --- a/Brie/brie/plugins/__init__.py +++ b/Brie/brie/plugins/__init__.py @@ -0,0 +1,2 @@ + +__all__ = ["wifi", "unix", "macauth"] diff --git a/Brie/brie/plugins/macauth/controller.py b/Brie/brie/plugins/macauth/controller.py index b959bc2..bfa7f5b 100644 --- a/Brie/brie/plugins/macauth/controller.py +++ b/Brie/brie/plugins/macauth/controller.py @@ -2,7 +2,7 @@ from tg.decorators import expose from brie.model.ldap import Machine -class Mac_auth: +class Macauth: @staticmethod @expose("") diff --git a/Brie/brie/plugins/wifi/controller.py b/Brie/brie/plugins/wifi/controller.py index 3e619d6..ee7e3ba 100644 --- a/Brie/brie/plugins/wifi/controller.py +++ b/Brie/brie/plugins/wifi/controller.py @@ -1,4 +1,14 @@ from tg.decorators import expose +from tg.controllers import redirect + +from brie.config import ldap_config +from brie.lib.ldap_helper import * +from brie.lib.aurore_helper import * +from brie.model.ldap import * + +from brie.controllers import auth +from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController + class Wifi: @@ -12,3 +22,15 @@ class Wifi: #end def #end class + +class DirectController(AuthenticatedRestController): + @expose("") + def get(self, residence, member_uid): + redirect("/show/member/" + residence + "/" + member_uid) + #end def + + @expose("") + def post(self, residence, member_uid): + redirect("/show/member/" + residence + "/" + member_uid) + #end def +#end class