예제 #1
0
class AModule(object):
    """
        Abstract class for routing modules.
    """

    def __init__(self, api, name, description=None, status=None,
                 index_endpoint=None):
        """

        :type api: flask_restful.Api
        """
        super(AModule, self).__init__()
        self.api = api
        self.name = name
        self.description = description
        self.status = status
        self.index_endpoint = index_endpoint
        self.module_resources_manager = ModuleResourcesManager(self)

    def setup(self):
        raise NotImplementedError('setup() MUST be override')

    def add_resource(self, resource, *urls, **kwargs):
        """
        Inject namespace into route and endpoint before adding resource
        :param resource:
        :type resource: ModuleResource
        :param urls:
        :type urls: list
        :param kwargs:
        :type kwargs:
        """
        if 'endpoint' in kwargs:
            #on python2 we got a str ascii and in pytho 3 a str unicode, it's what we want!
            kwargs['endpoint'] = str(self.name + '.' + kwargs['endpoint'])

        urls_list = list()
        for url in urls:
            urls_list.append('/' + self.name + url)
        if isinstance(resource, ModuleResource):
            self.module_resources_manager.register_resource(resource)
        self.api.add_resource(resource, *urls_list, **kwargs)

    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        """
        Inject namespace in rules and endpoints before adding the url rule
        :param rule:
        :type rule: str
        :param endpoint:
        :type endpoint: str
        :param view_func:
        :type view_func:
        :param options:
        :type options:
        """
        rule = '/' + self.name + rule
        if endpoint and (isinstance(endpoint, unicode) or isinstance(endpoint, str)):
            endpoint = self.name + '.' + endpoint
        self.api.app.add_url_rule(rule, endpoint, view_func, **options)
예제 #2
0
    def __init__(self, api, name, description=None, status=None,
                 index_endpoint=None):
        """

        :type api: flask_restful.Api
        """
        super(AModule, self).__init__()
        self.api = api
        self.name = name
        self.description = description
        self.status = status
        self.index_endpoint = index_endpoint
        self.module_resources_manager = ModuleResourcesManager(self)