Пример #1
0
    def register_resource(self, resource, settings):
        """ Registers new resource to the domain.

        Under the hood this validates given settings, updates default values
        and adds necessary URL routes (builds api url map).

        If there exists some resource with given name, it is overwritten.

        :param resource: resource name.
        :param settings: settings for given resource.

        .. versionchanged:: 0.6
           Support for 'mongo_indexes'.

        .. versionchanged:: 0.4
           Support for document versioning.


        .. versionadded:: 0.2
        """

        # this line only makes sense when we call this function outside of the
        # standard Eve setup routine, but it doesn't hurt to still call it
        self.config['DOMAIN'][resource] = settings

        # set up resource
        self._set_resource_defaults(resource, settings)
        self._validate_resource_settings(resource, settings)
        self._add_resource_url_rules(resource, settings)

        # add rules for version control collections if appropriate
        if settings['versioning'] is True:
            versioned_resource = resource + self.config['VERSIONS']
            self.config['DOMAIN'][versioned_resource] = \
                copy.deepcopy(self.config['DOMAIN'][resource])
            self.config['DOMAIN'][versioned_resource]['datasource']['source'] \
                += self.config['VERSIONS']
            self.config['SOURCES'][versioned_resource] = \
                copy.deepcopy(self.config['SOURCES'][resource])
            self.config['SOURCES'][versioned_resource]['source'] += \
                self.config['VERSIONS']
            # the new versioned resource also needs URL rules
            self._add_resource_url_rules(
                versioned_resource,
                self.config['DOMAIN'][versioned_resource]
            )

        # create the mongo db indexes
        mongo_indexes = self.config['DOMAIN'][resource]['mongo_indexes']
        if mongo_indexes:
            for name, value in mongo_indexes.items():
                if isinstance(value, tuple):
                    list_of_keys, index_options = value
                else:
                    list_of_keys = value
                    index_options = {}

                create_index(self, resource, name, list_of_keys, index_options)
Пример #2
0
    def init_indexes(self):
        for resource, resource_config in self.config['DOMAIN'].items():
            mongo_indexes = resource_config.get('mongo_indexes__init')
            if not mongo_indexes:
                continue

            # Borrowed https://github.com/pyeve/eve/blob/22ea4bfebc8b633251cd06837893ff699bd07a00/eve/flaskapp.py#L915
            for name, value in mongo_indexes.items():
                if isinstance(value, tuple):
                    list_of_keys, index_options = value
                else:
                    list_of_keys = value
                    index_options = {}

                create_index(self, resource, name, list_of_keys, index_options)
Пример #3
0
    def init_indexes(self):
        for resource, resource_config in self.config['DOMAIN'].items():
            mongo_indexes = resource_config.get('mongo_indexes__init')
            if not mongo_indexes:
                continue

            # Borrowed https://github.com/pyeve/eve/blob/22ea4bfebc8b633251cd06837893ff699bd07a00/eve/flaskapp.py#L915
            for name, value in mongo_indexes.items():
                if isinstance(value, tuple):
                    list_of_keys, index_options = value
                else:
                    list_of_keys = value
                    index_options = {}

                # index creation in background
                index_options['background'] = True
                create_index(self, resource, name, list_of_keys, index_options)