*************
Code Snippets
*************


This document contains code for some of the important classes, listed as
below:

* PgAdminModule_
* NodeView_
* BaseDriver_
* BaseConnection_


.. _PgAdminModule:

PgAdminModule
*************


PgAdminModule is inherited from Flask.Blueprint module.
This module defines a set of methods, properties and attributes,
that every module should implement.


.. code-block:: python

    class PgAdminModule(Blueprint):
        """
        Base class for every PgAdmin Module.

        This class defines a set of method and attributes that
        every module should implement.
        """

        def __init__(self, name, import_name, **kwargs):
            kwargs.setdefault('url_prefix', '/' + name)
            kwargs.setdefault('template_folder', 'templates')
            kwargs.setdefault('static_folder', 'static')
            self.submodules = []
            self.parentmodules = []

            super().__init__(name, import_name, **kwargs)

        def register_preferences(self):
            # To be implemented by child classes
            pass

        def register(self, app, options):
            """
            Override the default register function to automagically register
            sub-modules at once.
            """

            super().register(app, options)

            def create_module_preference():
                # Create preference for each module by default
                if hasattr(self, 'LABEL'):
                    self.preference = Preferences(self.name, self.LABEL)
                else:
                    self.preference = Preferences(self.name, None)

                self.register_preferences()

            # Create and register the module preference object and preferences for
            # it just before starting app
            app.register_before_app_start(create_module_preference)

            for module in self.submodules:
                module.parentmodules.append(self)
                if app.blueprints.get(module.name) is None:
                    app.register_blueprint(module)
                    app.register_logout_hook(module)

        def get_own_messages(self):
            """
            Returns:
                dict: the i18n messages used by this module, not including any
                    messages needed by the submodules.
            """
            return dict()

        def get_own_menuitems(self):
            """
            Returns:
                dict: the menuitems for this module, not including
                    any needed from the submodules.
            """
            return defaultdict(list)

        def get_exposed_url_endpoints(self):
            """
            Returns:
                list: a list of url endpoints exposed to the client.
            """
            return []

        @property
        def messages(self):
            res = self.get_own_messages()

            for module in self.submodules:
                res.update(module.messages)
            return res

        @property
        def menu_items(self):
            menu_items = self.get_own_menuitems()
            for module in self.submodules:
                for key, value in module.menu_items.items():
                    menu_items[key].extend(value)
            menu_items = dict((key, sorted(value, key=attrgetter('priority')))
                              for key, value in menu_items.items())
            return menu_items

        @property
        def exposed_endpoints(self):
            res = self.get_exposed_url_endpoints()

            for module in self.submodules:
                res += module.exposed_endpoints

            return res
