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


Python DBSession.expunge方法代码示例

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


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

示例1: _get_document

# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import expunge [as 别名]
    def _get_document(self, clazz, id, clazz_locale=None, lang=None):
        """Get a document with either a single locale (if `lang is given)
        or with all locales.
        If no document exists for the given id, a `HTTPNotFound` exception is
        raised.
        """
        if not lang:
            document_query = DBSession. \
                query(clazz). \
                filter(getattr(clazz, 'document_id') == id). \
                options(joinedload('geometry'))
            document_query = add_load_for_locales(
                document_query, clazz, clazz_locale)
            document_query = add_load_for_profiles(document_query, clazz)
            document = document_query.first()
        else:
            locales_type = with_polymorphic(DocumentLocale, clazz_locale) \
                if clazz_locale else DocumentLocale
            locales_attr = getattr(clazz, 'locales')
            locales_type_eager = locales_attr.of_type(clazz_locale) \
                if clazz_locale else locales_attr

            document_query = DBSession. \
                query(clazz). \
                join(locales_type). \
                filter(getattr(clazz, 'document_id') == id). \
                filter(DocumentLocale.lang == lang). \
                options(joinedload('geometry')).\
                options(contains_eager(locales_type_eager, alias=locales_type))
            document_query = add_load_for_profiles(document_query, clazz)
            document = document_query.first()

            if not document:
                # the requested locale might not be available, try to get the
                # document without locales
                document_query = DBSession. \
                    query(clazz). \
                    filter(getattr(clazz, 'document_id') == id). \
                    options(joinedload('geometry'))
                document_query = add_load_for_profiles(document_query, clazz)
                document = document_query.first()

                if document:
                    # explicitly set `locales` to an empty list so that they
                    # are no lazy loaded
                    document.locales = []
                    # also detach the document from the session, so that the
                    # empty list is not persisted
                    DBSession.expunge(document)

        if not document:
            raise HTTPNotFound('document not found')

        return document
开发者ID:c2corg,项目名称:v6_api,代码行数:56,代码来源:document.py

示例2: _get_document

# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import expunge [as 别名]
    def _get_document(self, clazz, id, culture=None):
        """Get a document with either a single locale (if `culture is given)
        or with all locales.
        If no document exists for the given id, a `HTTPNotFound` exception is
        raised.
        """
        if not culture:
            document = DBSession. \
                query(clazz). \
                filter(getattr(clazz, 'document_id') == id). \
                options(joinedload(getattr(clazz, 'locales'))). \
                options(joinedload('geometry')). \
                first()
        else:
            document = DBSession. \
                query(clazz). \
                join(getattr(clazz, 'locales')). \
                filter(getattr(clazz, 'document_id') == id). \
                options(contains_eager(getattr(clazz, 'locales'))). \
                filter(DocumentLocale.culture == culture). \
                options(joinedload('geometry')). \
                first()
            if not document:
                # the requested locale might not be available, try to get the
                # document without locales
                document = DBSession. \
                    query(clazz). \
                    filter(getattr(clazz, 'document_id') == id). \
                    options(joinedload('geometry')). \
                    first()
                if document:
                    # explicitly set `locales` to an empty list so that they
                    # are no lazy loaded
                    document.locales = []
                    # also detach the document from the session, so that the
                    # empty list is not persisted
                    DBSession.expunge(document)

        if not document:
            raise HTTPNotFound('document not found')

        return document
开发者ID:mfournier,项目名称:v6_api,代码行数:44,代码来源:document.py

示例3: set_best_locale

# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import expunge [as 别名]
def set_best_locale(documents, preferred_lang, expunge=True):
    """Sets the "best" locale on the given documents. The "best" locale is
    the locale in the given "preferred language" if available. Otherwise
    it is the "most relevant" translation according to `langs_priority`.
    """
    if preferred_lang is None:
        return

    for document in documents:
        # need to detach the document from the session, so that the
        # following change to `document.locales` is not persisted
        if expunge and not inspect(document).detached:
            DBSession.expunge(document)

        if document.locales:
            available_locales = {
                locale.lang: locale for locale in document.locales}
            best_locale = get_best_locale(available_locales, preferred_lang)
            if best_locale:
                document.locales = [best_locale]
开发者ID:arnaud-morvan,项目名称:v6_api,代码行数:22,代码来源:__init__.py

示例4: set_best_locale

# 需要导入模块: from c2corg_api.models import DBSession [as 别名]
# 或者: from c2corg_api.models.DBSession import expunge [as 别名]
def set_best_locale(documents, preferred_lang):
    """Sets the "best" locale on the given documents. The "best" locale is
    the locale in the given "preferred language" if available. Otherwise
    it is the "most relevant" translation according to `cultures_priority`.
    """
    if preferred_lang is None:
        return

    for document in documents:
        # need to detach the document from the session, so that the
        # following change to `document.locales` is not persisted
        DBSession.expunge(document)

        if document.locales:
            available_locales = {
                locale.culture: locale for locale in document.locales}
            if preferred_lang in available_locales:
                document.locales = [available_locales[preferred_lang]]
            else:
                best_locale = next(
                    (available_locales[lang] for lang in cultures_priority
                        if lang in available_locales), None)
                if best_locale:
                    document.locales = [best_locale]
开发者ID:mfournier,项目名称:v6_api,代码行数:26,代码来源:document.py


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