Пример #1
0
    def get_content(self, page, language, ctype, language_fallback=False):
        """Gets the latest :class:`Content <pages.models.Content>`
        for a particular page and language. Falls back to another
        language if wanted.

        :param page: the concerned page object.
        :param language: the wanted language.
        :param ctype: the content type.
        :param language_fallback: fallback to another language if ``True``.
        """
        PAGE_CONTENT_DICT_KEY = "page_content_dict_%s_%s"
        if not language:
            language = settings.PAGE_DEFAULT_LANGUAGE

        content_dict = cache.get(PAGE_CONTENT_DICT_KEY % (str(page.id), ctype))
        # content_dict = None

        if not content_dict:
            content_dict = {}
            for lang in settings.PAGE_LANGUAGES:
                try:
                    content = self.filter(language=lang[0], type=ctype, page=page).latest()
                    content_dict[lang[0]] = content.body
                except self.model.DoesNotExist:
                    content_dict[lang[0]] = ""
            cache.set(PAGE_CONTENT_DICT_KEY % (page.id, ctype), content_dict)

        if language in content_dict and content_dict[language]:
            return filter_link(content_dict[language], page, language, ctype)

        if language_fallback:
            for lang in settings.PAGE_LANGUAGES:
                if lang[0] in content_dict and content_dict[lang[0]]:
                    return filter_link(content_dict[lang[0]], page, lang[0], ctype)
        return ""
Пример #2
0
    def get_content(self, page, language, ctype, language_fallback=False):
        """Gets the latest :class:`Content <pages.models.Content>`
        for a particular page and language. Falls back to another
        language if wanted.

        :param page: the concerned page object.
        :param language: the wanted language.
        :param ctype: the content type.
        :param language_fallback: fallback to another language if ``True``.
        """
        if not language:
            language = settings.PAGE_DEFAULT_LANGUAGE

        frozen = int(bool(page.freeze_date))
        key = self.PAGE_CONTENT_DICT_KEY % (page.id, ctype, frozen)
        
        if page._content_dict is None:
            page._content_dict = dict()
        if page._content_dict.get(key, None):
            content_dict = page._content_dict.get(key)
        else:
            content_dict = cache.get(key)

        # fill a dict object for each language
        if not content_dict:
            content_dict = {}
            for lang in settings.PAGE_LANGUAGES:
                params = {
                    'language':lang[0],
                    'type':ctype,
                    'page':page
                }
                if page.freeze_date:
                    params['creation_date__lte'] = page.freeze_date
                # using the same variable name "language" introduce nasty bugs.
                lang = lang[0]
                try:
                    content = self.filter(**params).latest()
                    content_dict[lang] = content.body
                except self.model.DoesNotExist:
                    content_dict[lang] = ''
            page._content_dict[key] = content_dict
            cache.set(key, content_dict)

        if language in content_dict and content_dict[language]:
            return filter_link(content_dict[language], page, language, ctype)

        if language_fallback:
            for lang in settings.PAGE_LANGUAGES:
                if lang[0] in content_dict and content_dict[lang[0]]:
                    return filter_link(content_dict[lang[0]], page, lang[0],
                        ctype)
        return ''
Пример #3
0
    def get_content(self, page, language, ctype, language_fallback=False):
        """Gets the latest content string for a particular page, language and
        placeholder.

        :param page: the concerned page object.
        :param language: the wanted language.
        :param ctype: the content type.
        :param language_fallback: fallback to another language if ``True``.
        """
        if not language:
            language = settings.PAGE_DEFAULT_LANGUAGE

        frozen = int(bool(page.freeze_date))
        key = self.PAGE_CONTENT_DICT_KEY % (page.id, ctype, frozen)

        if page._content_dict is None:
            page._content_dict = dict()
        if page._content_dict.get(key, None):
            content_dict = page._content_dict.get(key)
        else:
            content_dict = cache.get(key)

        # fill a dict object for each language, that will create
        # P * L queries.
        # L == number of language, P == number of placeholder in the page.
        # Once generated the result is cached.
        if not content_dict:
            content_dict = {}
            for lang in settings.PAGE_LANGUAGES:
                try:
                    content = self.get_content_object(page, lang[0], ctype)
                    content_dict[lang[0]] = content.body
                except self.model.DoesNotExist:
                    content_dict[lang[0]] = ''
            page._content_dict[key] = content_dict
            cache.set(key, content_dict)

        if language in content_dict and content_dict[language]:
            return filter_link(content_dict[language], page, language, ctype)

        if language_fallback:
            for lang in settings.PAGE_LANGUAGES:
                if lang[0] in content_dict and content_dict[lang[0]]:
                    return filter_link(content_dict[lang[0]], page, lang[0],
                        ctype)
        return ''
Пример #4
0
    def get_content(self, page, language, ctype, language_fallback=False):
        """Gets the latest :class:`Content <pages.models.Content>`
        for a particular page and language. Falls back to another
        language if wanted.

        :param page: the concerned page object.
        :param language: the wanted language.
        :param ctype: the content type.
        :param language_fallback: fallback to another language if ``True``.
        """
        if not language:
            language = settings.PAGE_DEFAULT_LANGUAGE

        frozen = int(bool(page.freeze_date))
        content_dict = cache.get(self.PAGE_CONTENT_DICT_KEY % (page.id, ctype, frozen))

        # fill a dict object for each language
        if not content_dict:
            content_dict = {}
            for lang in settings.PAGE_LANGUAGES:
                params = {"language": lang[0], "type": ctype, "page": page}
                if page.freeze_date:
                    params["creation_date__lte"] = page.freeze_date
                language = lang[0]
                try:
                    content = self.filter(**params).latest()
                    content_dict[language] = content.body
                except self.model.DoesNotExist:
                    content_dict[language] = ""
            cache.set(self.PAGE_CONTENT_DICT_KEY % (page.id, ctype, frozen), content_dict)

        if language in content_dict and content_dict[language]:
            return filter_link(content_dict[language], page, language, ctype)

        if language_fallback:
            for lang in settings.PAGE_LANGUAGES:
                if lang[0] in content_dict and content_dict[lang[0]]:
                    return filter_link(content_dict[lang[0]], page, lang[0], ctype)
        return ""