当前位置: 首页>>代码示例>>Python>>正文


Python Mongo.pymongo方法代码示例

本文整理汇总了Python中eve.io.mongo.Mongo.pymongo方法的典型用法代码示例。如果您正苦于以下问题:Python Mongo.pymongo方法的具体用法?Python Mongo.pymongo怎么用?Python Mongo.pymongo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在eve.io.mongo.Mongo的用法示例。


在下文中一共展示了Mongo.pymongo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SuperdeskDataLayer

# 需要导入模块: from eve.io.mongo import Mongo [as 别名]
# 或者: from eve.io.mongo.Mongo import pymongo [as 别名]
class SuperdeskDataLayer(DataLayer):
    """Superdesk Data Layer.

    Implements eve data layer interface, is used to make eve work with superdesk service layer.
    It handles app initialization and later it forwards eve calls to respective service.
    """

    serializers = {}
    serializers.update(Mongo.serializers)
    serializers.update(Elastic.serializers)

    def init_app(self, app):
        app.data = self  # app.data must be set for locks to work
        self.mongo = Mongo(app)
        self.driver = self.mongo.driver
        self.storage = self.driver
        self.elastic = Elastic(app, serializer=SuperdeskJSONEncoder(), skip_index_init=True, retry_on_timeout=True)

    def init_elastic(self, app):
        """Init elastic index.

        It will create index and put mapping. It should run only once so locks are in place.
        Thus mongo must be already setup before running this.
        """
        with app.app_context():
            if lock('elastic', expire=10):
                try:
                    self.elastic.init_index(app)
                finally:
                    unlock('elastic')

    def find(self, resource, req, lookup):
        return superdesk.get_resource_service(resource).get(req=req, lookup=lookup)

    def find_all(self, resource, max_results=1000):
        req = ParsedRequest()
        req.max_results = max_results
        return self._backend(resource).find(resource, req, None)

    def find_one(self, resource, req, **lookup):
        return superdesk.get_resource_service(resource).find_one(req=req, **lookup)

    def find_one_raw(self, resource, _id):
        return self._backend(resource).find_one_raw(resource, _id)

    def find_list_of_ids(self, resource, ids, client_projection=None):
        return self._backend(resource).find_list_of_ids(resource, ids, client_projection)

    def insert(self, resource, docs, **kwargs):
        return superdesk.get_resource_service(resource).create(docs, **kwargs)

    def update(self, resource, id_, updates, original):
        return superdesk.get_resource_service(resource).update(id=id_, updates=updates, original=original)

    def update_all(self, resource, query, updates):
        datasource = self.datasource(resource)
        driver = self._backend(resource).driver
        collection = driver.db[datasource[0]]
        return collection.update(query, {'$set': updates}, multi=True)

    def replace(self, resource, id_, document, original):
        return superdesk.get_resource_service(resource).replace(id=id_, document=document, original=original)

    def remove(self, resource, lookup=None):
        if lookup is None:
            lookup = {}
        return superdesk.get_resource_service(resource).delete(lookup=lookup)

    def is_empty(self, resource):
        return self._backend(resource).is_empty(resource)

    def _search_backend(self, resource):
        if resource.endswith(current_app.config['VERSIONS']):
            return
        datasource = self.datasource(resource)
        backend = config.SOURCES.get(datasource[0], {}).get('search_backend', None)
        return getattr(self, backend) if backend is not None else None

    def _backend(self, resource):
        datasource = self.datasource(resource)
        backend = config.SOURCES.get(datasource[0], {'backend': 'mongo'}).get('backend', 'mongo')
        return getattr(self, backend)

    def get_mongo_collection(self, resource):
        return self.mongo.pymongo('users').db[resource]
开发者ID:hlmnrmr,项目名称:superdesk-core,代码行数:87,代码来源:datalayer.py


注:本文中的eve.io.mongo.Mongo.pymongo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。