def render(self, context, instance, placeholder): request = context.get("request", None) if context.has_key("request"): lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE # Get all FaqEntryPlugins on this page and in this language plugins = get_cmsplugin_queryset(request).filter( plugin_type="CMSFaqEntryPlugin", placeholder__page=instance.page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True ).order_by("position").select_related() faqentry_plugins = [] # Make a list of the FaqEntry plugin objects for plugin in plugins: # Truncate the entry's body if instance.truncate_body: plugin.faqentry.body = truncate_words(plugin.faqentry.body, instance.truncate_body) # Show the entry's body or not if not instance.show_body: plugin.faqentry.body = '' faqentry_plugins.append(plugin.faqentry) context.update({ "faq_list": faqentry_plugins, "css": instance.get_css_display(), "placeholder": placeholder, }) return context
def render(self, context, instance, placeholder): template_vars = { 'placeholder': placeholder, } template_vars['object'] = instance lang = instance.from_language request = None if not lang: if context.has_key('request'): request = context['request'] lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE if instance.from_page: page = instance.from_page else: page = instance.page if not instance.page.publisher_is_draft and page.publisher_is_draft: page = page.publisher_public plugins = get_cmsplugin_queryset(request).filter(page=page, language=lang, placeholder__iexact=placeholder, parent__isnull=True).order_by('position').select_related() plugin_output = [] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) template_vars['parent_output'] = plugin_output context.update(template_vars) return context
def render(self, context): if context.get('display_placeholder_names_only'): return "<!-- PlaceholderNode: %s -->" % self.name if not 'request' in context: return '' l = get_language_from_request(context['request']) request = context['request'] page = request.current_page if page == "dummy": return "" plugins = get_cmsplugin_queryset(request).filter(page=page, language=l, placeholder__iexact=self.name, parent__isnull=True).order_by('position').select_related() if settings.CMS_PLACEHOLDER_CONF and self.name in settings.CMS_PLACEHOLDER_CONF: if "extra_context" in settings.CMS_PLACEHOLDER_CONF[self.name]: context.update(settings.CMS_PLACEHOLDER_CONF[self.name]["extra_context"]) if self.theme: # this may overwrite previously defined key [theme] from settings.CMS_PLACEHOLDER_CONF context.update({'theme': self.theme,}) c = "" lstOutput = [] for plugin in plugins: renderOutput = plugin.render_plugin(context, self.name) if renderOutput <> '': c += renderOutput lstOutput.append(renderOutput) if self.dst <> None: context[self.dst] = lstOutput return '' return c
def show_placeholder_by_id(context, placeholder_name, reverse_id, lang=None, site=None): """ Show the content of a page with a placeholder name and a reverse id in the right language This is mostly used if you want to have static content in a template of a page (like a footer) """ request = context.get('request', False) site_id = get_site_id(site) if not request: return {'content':''} if lang is None: lang = get_language_from_request(request) key = 'show_placeholder_by_id_pid:'+reverse_id+'_placeholder:'+placeholder_name+'_site:'+str(site_id)+'_l:'+str(lang) content = cache.get(key) if not content: try: page = get_page_queryset(request).get(reverse_id=reverse_id, site=site_id) except: send_missing_mail(reverse_id, request) return {'content':''} plugins = get_cmsplugin_queryset(request).filter(page=page, language=lang, placeholder__iexact=placeholder_name, parent__isnull=True).order_by('position').select_related() content = "" for plugin in plugins: content += plugin.render_plugin(context, placeholder_name) cache.set(key, content, settings.CMS_CONTENT_CACHE_DURATION) if content: return {'content':mark_safe(content)} return {'content':''}
def render(self, context, instance, placeholder): context['object'] = instance context['placeholder'] = placeholder lang = instance.from_language request = context.get('request', None) if not lang: if 'request' in context: lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE page = instance.placeholder.page from_page = instance.from_page if page.publisher_is_draft: # If the plugin is being rendered in draft # then show only the draft content of the linked page from_page = from_page.get_draft_object() else: # Otherwise show the live content from_page = from_page.get_public_object() plugins = get_cmsplugin_queryset(request).filter( placeholder__page=from_page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True ).order_by('position').select_related() context['parent_plugins'] = plugins context['parent_output'] = self._render_plugins(plugins, context) return context
def render(self, context, instance, placeholder): request = context.get("request", None) if context.has_key("request"): lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE # Get all FaqEntryPlugins on this page and in this language plugins = get_cmsplugin_queryset(request).filter( plugin_type="CMSFaqEntryPlugin", placeholder__page=instance.page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True).order_by("position").select_related() faqentry_plugins = [] # Make a list of the FaqEntry plugin objects for plugin in plugins: # Truncate the entry's body if instance.truncate_body: plugin.faqentry.body = truncate_words(plugin.faqentry.body, instance.truncate_body) # Show the entry's body or not if not instance.show_body: plugin.faqentry.body = '' faqentry_plugins.append(plugin.faqentry) context.update({ "faq_list": faqentry_plugins, "css": instance.get_css_display(), "placeholder": placeholder, }) return context
def render(self, context, instance, placeholder): template_vars = { 'placeholder': placeholder, } template_vars['object'] = instance lang = instance.from_language request = context.get('request', None) if not lang: if context.has_key('request'): lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE if instance.from_page: page = instance.from_page else: page = instance.page if not instance.page.publisher_is_draft and page.publisher_is_draft: page = page.publisher_public plugins = get_cmsplugin_queryset(request).filter( placeholder__page=page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True).order_by('position').select_related() plugin_output = [] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) template_vars['parent_output'] = plugin_output context.update(template_vars) return context
def assign_plugins(request, placeholders, lang=None): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ placeholders = list(placeholders) if not placeholders: return lang = lang or get_language_from_request(request) request_lang = lang if hasattr(request, "current_page") and request.current_page is not None: languages = request.current_page.get_languages() if not lang in languages and not get_redirect_on_fallback(lang): fallbacks = get_fallback_languages(lang) for fallback in fallbacks: if fallback in languages: request_lang = fallback break # get all plugins for the given placeholders qs = get_cmsplugin_queryset(request).filter( placeholder__in=placeholders, language=request_lang).order_by('placeholder', 'tree_id', 'lft') plugin_list = downcast_plugins(qs) # split the plugins up by placeholder groups = dict((key, list(plugins)) for key, plugins in groupby( plugin_list, operator.attrgetter('placeholder_id'))) for group in groups: groups[group] = build_plugin_tree(groups[group]) for placeholder in placeholders: setattr(placeholder, '_%s_plugins_cache' % lang, list(groups.get(placeholder.pk, [])))
def render(self, context): if context.get('display_placeholder_names_only'): return "<!-- PlaceholderNode: %s -->" % self.name if not 'request' in context: return '' l = get_language_from_request(context['request']) request = context['request'] page = request.current_page if page == "dummy": return "" plugins = get_cmsplugin_queryset(request).filter( page=page, language=l, placeholder__iexact=self.name, parent__isnull=True).order_by('position').select_related() if settings.CMS_PLACEHOLDER_CONF and self.name in settings.CMS_PLACEHOLDER_CONF: if "extra_context" in settings.CMS_PLACEHOLDER_CONF[self.name]: context.update( settings.CMS_PLACEHOLDER_CONF[self.name]["extra_context"]) if self.theme: # this may overwrite previously defined key [theme] from settings.CMS_PLACEHOLDER_CONF context.update({ 'theme': self.theme, }) c = [] for index, plugin in enumerate(plugins): context['plugin_index'] = index c.append(plugin.render_plugin(context, self.name)) return "".join(c)
def get_footnotes_for_page(request, page): ''' Gets the Footnote instances for `page`, with the correct order. ''' plugins = get_cmsplugin_queryset(request) cache_key = get_cache_key(page, plugins) footnote_ids = cache.get(cache_key) if footnote_ids is None: root_footnote_and_text_plugins = plugins.filter( placeholder__page=page, plugin_type__in=('FootnotePlugin', 'TextPlugin'), parent=None ).order_by('position') footnote_plugins = [] footnote_plugins__append = footnote_plugins.append for p in root_footnote_and_text_plugins: if plugin_is_footnote(p): footnote_plugins__append(p) else: try: text = downcast_plugins((p,))[0] except IndexError: continue plugin_iterator = plugin_iterator_from_text_plugin(text) for plugin in plugin_iterator: if plugin_is_footnote(plugin): footnote_plugins__append(plugin) footnote_ids = tuple(f.pk for f in downcast_plugins(footnote_plugins)) cache.set(cache_key, footnote_ids ) return Footnote.objects.filter(pk__in=footnote_ids)
def assign_plugins(request, placeholders, lang=None): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ placeholders = list(placeholders) if not placeholders: return lang = lang or get_language_from_request(request) request_lang = lang if hasattr(request, "current_page") and request.current_page is not None: languages = request.current_page.get_languages() if not lang in languages and not get_redirect_on_fallback(lang): fallbacks = get_fallback_languages(lang) for fallback in fallbacks: if fallback in languages: request_lang = fallback break # get all plugins for the given placeholders qs = get_cmsplugin_queryset(request).filter(placeholder__in=placeholders, language=request_lang).order_by( 'placeholder', 'tree_id', 'lft') plugin_list = downcast_plugins(qs) # split the plugins up by placeholder groups = dict((key, list(plugins)) for key, plugins in groupby(plugin_list, operator.attrgetter('placeholder_id'))) for group in groups: groups[group] = build_plugin_tree(groups[group]) for placeholder in placeholders: setattr(placeholder, '_%s_plugins_cache' % lang, list(groups.get(placeholder.pk, [])))
def assign_plugins(request, placeholders, template=None, lang=None, is_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ if not placeholders: return placeholders = tuple(placeholders) lang = lang or get_language_from_request(request) qs = get_cmsplugin_queryset(request) qs = qs.filter(placeholder__in=placeholders, language=lang) plugins = list(qs.order_by('placeholder', 'path')) fallbacks = defaultdict(list) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if (not is_fallback and not (hasattr(request, 'toolbar') and request.toolbar.edit_mode_active)): disjoint_placeholders = (ph for ph in placeholders if all( ph.pk != p.placeholder_id for p in plugins)) for placeholder in disjoint_placeholders: if get_placeholder_conf("language_fallback", placeholder.slot, template, True): for fallback_language in get_fallback_languages(lang): assign_plugins(request, (placeholder, ), template, fallback_language, is_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: fallbacks[placeholder.pk] += fallback_plugins break # These placeholders have no fallback non_fallback_phs = [ph for ph in placeholders if ph.pk not in fallbacks] # If no plugin is present in non fallback placeholders, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, non_fallback_phs, template, lang) plugins = downcast_plugins(plugins, non_fallback_phs, request=request) # split the plugins up by placeholder # Plugins should still be sorted by placeholder plugin_groups = dict( (key, list(plugins)) for key, plugins in groupby(plugins, attrgetter('placeholder_id'))) all_plugins_groups = plugin_groups.copy() for group in plugin_groups: plugin_groups[group] = build_plugin_tree(plugin_groups[group]) groups = fallbacks.copy() groups.update(plugin_groups) for placeholder in placeholders: # This is all the plugins. setattr(placeholder, '_all_plugins_cache', all_plugins_groups.get(placeholder.pk, [])) # This one is only the root plugins. setattr(placeholder, '_plugins_cache', groups.get(placeholder.pk, []))
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None, site=None, cache_result=True): """ Shows the content of a page with a placeholder name and given lookup arguments in the given language. This is useful if you want to have some more or less static content that is shared among many pages, such as a footer. See _get_page_by_untyped_arg() for detailed information on the allowed types and their interpretation for the page_lookup argument. """ validate_placeholder_name(placeholder_name) request = context.get('request', False) site_id = get_site_id(site) if not request: return {'content': ''} if lang is None: lang = get_language_from_request(request) content = None if cache_result: base_key = _get_cache_key('_show_placeholder_for_page', page_lookup, lang, site_id) cache_key = _clean_key('%s_placeholder:%s' % (base_key, placeholder_name)) content = cache.get(cache_key) if not content: page = _get_page_by_untyped_arg(page_lookup, request, site_id) if not page: return {'content': ''} try: placeholder = page.placeholders.get(slot=placeholder_name) except PlaceholderModel.DoesNotExist: if settings.DEBUG: raise return {'content': ''} baseqs = get_cmsplugin_queryset(request) plugins = baseqs.filter( placeholder=placeholder, language=lang, placeholder__slot__iexact=placeholder_name, parent__isnull=True).order_by('position').select_related() c = render_plugins(plugins, context, placeholder) content = "".join(c) if cache_result: cache.set(cache_key, content, settings.CMS_CACHE_DURATIONS['content']) if content: return {'content': mark_safe(content)} return {'content': ''}
def assign_plugins(request, placeholders, template, lang=None, no_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ placeholders = list(placeholders) if not placeholders: return lang = lang or get_language_from_request(request) request_lang = lang qs = get_cmsplugin_queryset(request).filter( placeholder__in=placeholders, language=request_lang).order_by('placeholder', 'tree_id', 'level', 'position') plugins = list(qs) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if not no_fallback: for placeholder in placeholders: found = False for plugin in plugins: if plugin.placeholder_id == placeholder.pk: found = True break if found: continue elif placeholder and get_placeholder_conf( "language_fallback", placeholder.slot, template, False): if hasattr(request, 'toolbar') and request.toolbar.edit_mode: continue fallbacks = get_fallback_languages(lang) for fallback_language in fallbacks: assign_plugins(request, [placeholder], template, fallback_language, no_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: plugins += fallback_plugins break # If no plugin is present, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, placeholders, template, lang) plugin_list = downcast_plugins(plugins, placeholders) # split the plugins up by placeholder groups = dict((key, list(plugins)) for key, plugins in groupby( plugin_list, operator.attrgetter('placeholder_id'))) for group in groups: groups[group] = build_plugin_tree(groups[group]) for placeholder in placeholders: setattr(placeholder, '_plugins_cache', list(groups.get(placeholder.pk, [])))
def render(self, context, instance, placeholder): # https://github.com/divio/django-cms/blob/develop/cms/plugins/inherit/cms_plugins.py template_vars = { 'placeholder': placeholder, } template_vars['object'] = instance request = context.get('request', None) if context.has_key('request'): lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE page = instance.placeholder.page divs = [] from_pages = Page.objects.published().exclude( pk=page.pk).order_by('-publication_date')[0:5] for from_page in from_pages: if page.publisher_is_draft: from_page = from_page.get_draft_object() else: from_page = from_page.get_public_object() try: plugins = get_cmsplugin_queryset(request).filter( placeholder__page=from_page, language=lang, placeholder__slot__iexact='page', #placeholder, parent__isnull=True).order_by('position').select_related() plugin_output = ['<h2>%s</h2>' % from_page.get_title()] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() if inst is None: continue outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) except Http404: plugin_output.append('404 Not Found') if from_page == from_pages[0]: el_attrs = 'class="button carouslide-focus"' else: el_attrs = 'class="button carouslide-blur"' divs.append('<div onclick="document.location=\'%s\'" %s>%s</div>' % \ (from_page.get_absolute_url(), el_attrs, ''.join(plugin_output))) # template_vars['carouslide_content'] = mark_safe('<div>%s</div>' % \ # '</div><div>'.join(divs)) template_vars['carouslide_content'] = mark_safe(''.join(divs)) context.update(template_vars) context.update({'instance': instance}) return context
def render(self, context, instance, placeholder): # https://github.com/divio/django-cms/blob/develop/cms/plugins/inherit/cms_plugins.py template_vars = { 'placeholder': placeholder, } template_vars['object'] = instance request = context.get('request', None) if context.has_key('request'): lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE page = instance.placeholder.page divs = [] from_pages = Page.objects.published().exclude(pk=page.pk).order_by('-publication_date')[0:5] for from_page in from_pages: if page.publisher_is_draft: from_page = from_page.get_draft_object() else: from_page = from_page.get_public_object() try: plugins = get_cmsplugin_queryset(request).filter( placeholder__page=from_page, language=lang, placeholder__slot__iexact='page', #placeholder, parent__isnull=True ).order_by('position').select_related() plugin_output = ['<h2>%s</h2>' % from_page.get_title()] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() if inst is None: continue outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) except Http404: plugin_output.append('404 Not Found') if from_page == from_pages[0]: el_attrs = 'class="button carouslide-focus"' else: el_attrs = 'class="button carouslide-blur"' divs.append('<div onclick="document.location=\'%s\'" %s>%s</div>' % \ (from_page.get_absolute_url(), el_attrs, ''.join(plugin_output))) # template_vars['carouslide_content'] = mark_safe('<div>%s</div>' % \ # '</div><div>'.join(divs)) template_vars['carouslide_content'] = mark_safe(''.join(divs)) context.update(template_vars) context.update({'instance': instance}) return context
def render(self, context, instance, placeholder): template_vars = { 'object': instance, 'placeholder': placeholder, } lang = instance.from_language request = context.get('request', None) if not lang: if 'request' in context: lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE page = instance.placeholder.page from_page = instance.from_page if page.publisher_is_draft: from_page = from_page.get_draft_object() else: from_page = from_page.get_public_object() plugins = get_cmsplugin_queryset(request).filter( placeholder__page=from_page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True).order_by('position').select_related() plugin_output = [] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() if inst is None: continue # Get child plugins for this plugin instance, if any child plugins # exist try: # django CMS 3- plugins = [inst] + list( inst.get_descendants(include_self=True).order_by( 'placeholder', 'tree_id', 'level', 'position')) except (FieldError, TypeError): # django CMS 3.1+ plugins = [inst] + list( inst.get_descendants().order_by('path')) plugin_tree = downcast_plugins(plugins) plugin_tree[0].parent_id = None plugin_tree = build_plugin_tree(plugin_tree) # Replace plugin instance with plugin instance with correct # child_plugin_instances set inst = plugin_tree[0] outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) template_vars['parent_output'] = plugin_output context.update(template_vars) return context
def render(self, context, instance, placeholder): template_vars = { 'object': instance, 'placeholder': placeholder, } lang = instance.from_language request = context.get('request', None) if not lang: if 'request' in context: lang = get_language_from_request(request) else: lang = settings.LANGUAGE_CODE page = instance.placeholder.page from_page = instance.from_page if page.publisher_is_draft: from_page = from_page.get_draft_object() else: from_page = from_page.get_public_object() plugins = get_cmsplugin_queryset(request).filter( placeholder__page=from_page, language=lang, placeholder__slot__iexact=placeholder, parent__isnull=True ).order_by('position').select_related() plugin_output = [] template_vars['parent_plugins'] = plugins for plg in plugins: tmpctx = copy.copy(context) tmpctx.update(template_vars) inst, name = plg.get_plugin_instance() if inst is None: continue # Get child plugins for this plugin instance, if any child plugins # exist try: # django CMS 3- plugins = [inst] + list(inst.get_descendants(include_self=True) .order_by('placeholder', 'tree_id', 'level', 'position')) except (FieldError, TypeError): # django CMS 3.1+ plugins = [inst] + list(inst.get_descendants().order_by('path')) plugin_tree = downcast_plugins(plugins) plugin_tree[0].parent_id = None plugin_tree = build_plugin_tree(plugin_tree) # Replace plugin instance with plugin instance with correct # child_plugin_instances set inst = plugin_tree[0] outstr = inst.render_plugin(tmpctx, placeholder) plugin_output.append(outstr) template_vars['parent_output'] = plugin_output context.update(template_vars) return context
def get_plugins(request, placeholder, lang=None): if not placeholder: return [] lang = lang or get_language_from_request(request) if not hasattr(placeholder, '_%s_plugins_cache' % lang): setattr(placeholder, '_%s_plugins_cache' % lang, get_cmsplugin_queryset(request).filter( placeholder=placeholder, language=lang, parent__isnull=True ).order_by('placeholder', 'position').select_related()) return getattr(placeholder, '_%s_plugins_cache' % lang)
def get_plugins_for_page(request, page, lang=None): if not page: return [] lang = lang or get_language_from_request(request) if not hasattr(page, '_%s_plugins_cache' % lang): slots = get_placeholders(page.get_template()) setattr(page, '_%s_plugins_cache' % lang, get_cmsplugin_queryset(request).filter( placeholder__page=page, placeholder__slot__in=slots, language=lang, parent__isnull=True ).order_by('placeholder', 'position').select_related()) return getattr(page, '_%s_plugins_cache' % lang)
def get_plugins_for_page(request, page, lang=None): if not page: return [] lang = lang or get_language_from_request(request) if not hasattr(page, '_%s_plugins_cache' % lang): slots = [pl.slot for pl in get_placeholders(page.get_template())] setattr(page, '_%s_plugins_cache' % lang, get_cmsplugin_queryset(request).filter( placeholder__page=page, placeholder__slot__in=slots, language=lang, parent__isnull=True ).order_by('placeholder', 'position').select_related()) return getattr(page, '_%s_plugins_cache' % lang)
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None, site=None, cache_result=True): """ Shows the content of a page with a placeholder name and given lookup arguments in the given language. This is useful if you want to have some more or less static content that is shared among many pages, such as a footer. See _get_page_by_untyped_arg() for detailed information on the allowed types and their interpretation for the page_lookup argument. """ validate_placeholder_name(placeholder_name) request = context.get('request', False) site_id = get_site_id(site) if not request: return {'content': ''} if lang is None: lang = get_language_from_request(request) content = None if cache_result: base_key = _get_cache_key('_show_placeholder_for_page', page_lookup, lang, site_id) cache_key = _clean_key('%s_placeholder:%s' % (base_key, placeholder_name)) content = cache.get(cache_key) if not content: page = _get_page_by_untyped_arg(page_lookup, request, site_id) if not page: return {'content': ''} try: placeholder = page.placeholders.get(slot=placeholder_name) except PlaceholderModel.DoesNotExist: if settings.DEBUG: raise return {'content': ''} baseqs = get_cmsplugin_queryset(request) plugins = baseqs.filter( placeholder=placeholder, language=lang, placeholder__slot__iexact=placeholder_name, parent__isnull=True ).order_by('position').select_related() c = render_plugins(plugins, context, placeholder) content = "".join(c) if cache_result: cache.set(cache_key, content, settings.CMS_CACHE_DURATIONS['content']) if content: return {'content': mark_safe(content)} return {'content': ''}
def get_plugins(request, placeholder, lang=None): if not placeholder: return [] lang = lang or get_language_from_request(request) if not hasattr(placeholder, '_%s_plugins_cache' % lang): setattr( placeholder, '_%s_plugins_cache' % lang, get_cmsplugin_queryset(request).filter( placeholder=placeholder, language=lang, parent__isnull=True).order_by('placeholder', 'position').select_related()) return getattr(placeholder, '_%s_plugins_cache' % lang)
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None, site=None, cache_result=True): """ Shows the content of a page with a placeholder name and given lookup arguments in the given language. This is useful if you want to have some more or less static content that is shared among many pages, such as a footer. See _get_page_by_untyped_arg() for detailed information on the allowed types and their interpretation for the page_lookup argument. """ request = context.get("request", False) site_id = get_site_id(site) if not request: return {"content": ""} if lang is None: lang = get_language_from_request(request) content = None if cache_result: cache_key = ( _get_cache_key("_show_placeholder_for_page", page_lookup, lang, site_id) + "_placeholder:" + placeholder_name ) content = cache.get(cache_key) if not content: page = _get_page_by_untyped_arg(page_lookup, request, site_id) if not page: return {"content": ""} placeholder = page.placeholders.get(slot=placeholder_name) baseqs = get_cmsplugin_queryset(request) plugins = ( baseqs.filter( placeholder=placeholder, language=lang, placeholder__slot__iexact=placeholder_name, parent__isnull=True ) .order_by("position") .select_related() ) c = render_plugins(plugins, context, placeholder) content = "".join(c) if cache_result: cache.set(cache_key, content, settings.CMS_CONTENT_CACHE_DURATION) if content: return {"content": mark_safe(content)} return {"content": ""}
def get_chart_as_json(request, chart_id): chart_obj = get_object_or_404(ChartModel, id=chart_id) # Get the CMSPlugins of any Child Datasets qs = get_cmsplugin_queryset() qs = qs.filter(Q(parent_id=chart_id)) # For some reason we need position not path here... plugins = list(qs.order_by('placeholder', 'position')) datasets = [p.get_plugin_instance()[0] for p in plugins] # Set Child objects chart_obj.child_plugin_instances = datasets return JsonResponse(chart_obj.get_chart_as_dict())
def assign_plugins(request, placeholders, template, lang=None, is_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ if not placeholders: return placeholders = tuple(placeholders) lang = lang or get_language_from_request(request) qs = get_cmsplugin_queryset(request) qs = qs.filter(placeholder__in=placeholders, language=lang) plugins = list(qs.order_by('placeholder', 'path')) fallbacks = defaultdict(list) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if (not is_fallback and not (hasattr(request, 'toolbar') and request.toolbar.edit_mode)): disjoint_placeholders = (ph for ph in placeholders if all(ph.pk != p.placeholder_id for p in plugins)) for placeholder in disjoint_placeholders: if get_placeholder_conf("language_fallback", placeholder.slot, template, True): for fallback_language in get_fallback_languages(lang): assign_plugins(request, (placeholder,), template, fallback_language, is_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: fallbacks[placeholder.pk] += fallback_plugins break # These placeholders have no fallback non_fallback_phs = [ph for ph in placeholders if ph.pk not in fallbacks] # If no plugin is present in non fallback placeholders, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, non_fallback_phs, template, lang) plugins = downcast_plugins(plugins, non_fallback_phs, request=request) # split the plugins up by placeholder # Plugins should still be sorted by placeholder plugin_groups = dict((key, list(plugins)) for key, plugins in groupby(plugins, attrgetter('placeholder_id'))) all_plugins_groups = plugin_groups.copy() for group in plugin_groups: plugin_groups[group] = build_plugin_tree(plugin_groups[group]) groups = fallbacks.copy() groups.update(plugin_groups) for placeholder in placeholders: # This is all the plugins. setattr(placeholder, '_all_plugins_cache', all_plugins_groups.get(placeholder.pk, [])) # This one is only the root plugins. setattr(placeholder, '_plugins_cache', groups.get(placeholder.pk, []))
def _show_placeholder_by_id(context, placeholder_name, reverse_id, lang=None, site=None, cache_result=True): """ Show the content of a page with a placeholder name and a reverse id in the right language This is mostly used if you want to have static content in a template of a page (like a footer) """ request = context.get('request', False) site_id = get_site_id(site) if not request: return {'content':''} if lang is None: lang = get_language_from_request(request) content = None if cache_result: key = 'show_placeholder_by_id_pid:'+reverse_id+'_placeholder:'+placeholder_name+'_site:'+str(site_id)+'_l:'+str(lang) content = cache.get(key) if not content: try: page = get_page_queryset(request).get(reverse_id=reverse_id, site=site_id) except: if settings.DEBUG: raise else: site = Site.objects.get_current() send_mail(_('Reverse ID not found on %(domain)s') % {'domain':site.domain}, _("A show_placeholder_by_id template tag didn't found a page with the reverse_id %(reverse_id)s\n" "The url of the page was: http://%(host)s%(path)s") % {'reverse_id':reverse_id, 'host':request.host, 'path':request.path}, settings.DEFAULT_FROM_EMAIL, settings.MANAGERS, fail_silently=True) return {'content':''} plugins = get_cmsplugin_queryset(request).filter(page=page, language=lang, placeholder__iexact=placeholder_name, parent__isnull=True).order_by('position').select_related() content = "" for plugin in plugins: content += plugin.render_plugin(context, placeholder_name) if cache_result: cache.set(key, content, settings.CMS_CONTENT_CACHE_DURATION) if content: return {'content':mark_safe(content)} return {'content':''}
def assign_plugins(request, placeholders, template, lang=None, no_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ placeholders = list(placeholders) if not placeholders: return lang = lang or get_language_from_request(request) request_lang = lang qs = get_cmsplugin_queryset(request).filter(placeholder__in=placeholders, language=request_lang).order_by( 'placeholder', 'tree_id', 'level', 'position') plugins = list(qs) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if not no_fallback: for placeholder in placeholders: found = False for plugin in plugins: if plugin.placeholder_id == placeholder.pk: found = True break if found: continue elif placeholder and get_placeholder_conf("language_fallback", placeholder.slot, template, False): if hasattr(request, 'toolbar') and request.toolbar.edit_mode: continue fallbacks = get_fallback_languages(lang) for fallback_language in fallbacks: assign_plugins(request, [placeholder], template, fallback_language, no_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: plugins += fallback_plugins break # If no plugin is present, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, placeholders, template, lang) plugin_list = downcast_plugins(plugins, placeholders) # split the plugins up by placeholder groups = dict((key, list(plugins)) for key, plugins in groupby(plugin_list, operator.attrgetter('placeholder_id'))) for group in groups: groups[group] = build_plugin_tree(groups[group]) for placeholder in placeholders: setattr(placeholder, '_plugins_cache', list(groups.get(placeholder.pk, [])))
def assign_plugins(request, placeholders, template, lang=None, is_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ if not placeholders: return placeholders = tuple(placeholders) lang = lang or get_language_from_request(request) qs = get_cmsplugin_queryset(request) qs = qs.filter(placeholder__in=placeholders, language=lang) plugins = list(qs.order_by('placeholder', 'path')) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if (not is_fallback and not (hasattr(request, 'toolbar') and request.toolbar.edit_mode)): disjoint_placeholders = (ph for ph in placeholders if all( ph.pk != p.placeholder_id for p in plugins)) for placeholder in disjoint_placeholders: if get_placeholder_conf("language_fallback", placeholder.slot, template, True): for fallback_language in get_fallback_languages(lang): assign_plugins(request, (placeholder, ), template, fallback_language, is_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: plugins += fallback_plugins break # If no plugin is present, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, placeholders, template, lang) plugins = downcast_plugins(plugins, placeholders) # split the plugins up by placeholder # Plugins should still be sorted by placeholder groups = dict((ph_id, build_plugin_tree(ph_plugins)) for ph_id, ph_plugins in groupby( plugins, attrgetter('placeholder_id'))) for placeholder in placeholders: setattr(placeholder, '_plugins_cache', groups.get(placeholder.pk, []))
def render(self, context, instance, placeholder): assert(instance is not None and instance.placeholder_name) target_page = instance.target_page or context["current_page"] child_pages = target_page.get_children() if instance.max_items: child_pages = child_pages[:instance.max_items] plugins = [] for page in child_pages: plugins += get_cmsplugin_queryset(context["request"]).filter( Q(placeholder__page = page), placeholder__slot__iexact= instance.placeholder_name).order_by('position').select_related() child_outputs = [ (page, plugin.get_plugin_instance()[0].render_plugin(copy(context), instance.placeholder_name)) for page, plugin in zip (child_pages, plugins) ] context.update({"child_outputs": child_outputs}) return context
def render(self, context): if not 'request' in context: return '' l = get_language_from_request(context['request']) request = context['request'] page = request.current_page if page == "dummy": return "" plugins = get_cmsplugin_queryset(request).filter(page=page, language=l, placeholder__iexact=self.name, parent__isnull=True).order_by('position').select_related() if settings.CMS_PLACEHOLDER_CONF and self.name in settings.CMS_PLACEHOLDER_CONF: if "extra_context" in settings.CMS_PLACEHOLDER_CONF[self.name]: context.update(settings.CMS_PLACEHOLDER_CONF[self.name]["extra_context"]) if self.theme: # this may overwrite previously defined key [theme] from settings.CMS_PLACEHOLDER_CONF context.update({'theme': self.theme,}) c = "" for plugin in plugins: c += plugin.render_plugin(context, self.name) return c
def get_footnotes_for_page(request, page): """ Gets the Footnote instances for `page`, with the correct order. """ plugins = get_cmsplugin_queryset(request) footnote_and_text_plugins = plugins.filter( placeholder__page=page, plugin_type__in=('FootnotePlugin', 'TextPlugin'), language=translation.get_language(), ).order_by('position').values('parent', 'plugin_type', 'pk') pks = [p['pk'] for p in footnote_and_text_plugins] footnote_dict = Footnote.objects.in_bulk(pks) text_dict = Text.objects.in_bulk(pks) def get_footnote_or_text(plugin_pk, plugin_type): d = footnote_dict if plugin_type == 'FootnotePlugin' else text_dict try: return d[plugin_pk] except KeyError: if CMSPLUGIN_FOOTNOTE_DEBUG: raise root_footnote_and_text_plugins = [ p for p in footnote_and_text_plugins if p['parent'] is None ] footnotes = [] for plugin in root_footnote_and_text_plugins: footnote_or_text = get_footnote_or_text(plugin['pk'], plugin['plugin_type']) if footnote_or_text is None: continue if plugin['plugin_type'] == 'FootnotePlugin': footnotes.append(footnote_or_text) else: for pk in plugin_tags_to_id_list(footnote_or_text.body): footnote = get_footnote_or_text(pk, 'FootnotePlugin') if footnote is not None: footnotes.append(footnote) return footnotes
def get_plugins(request, obj, placeholder, lang=None): """ Get all plugins for a page, placeholder and language. Internally this will cache the plugin results for all placeholders, so that ideally only one query will be made for all calls to this. In case a placeholder hasn't any plugins defined on the page get_plugins_recursively is used. Returns an iterable with the plugins. """ if not obj: return [] lang = lang or get_language_from_request(request) # STAGE ONE - GET AND CACHE ALL PLACEHOLDERS cache_key = "_%s_plugins_cache" % lang if not hasattr(obj, cache_key): plugins = get_cmsplugin_queryset(request).filter(page=obj, language=lang, parent__isnull=True) plugins = plugins.order_by('placeholder', 'position').select_related() cache_dict = {} for plugin in plugins: if plugin.placeholder not in cache_dict: cache_dict[plugin.placeholder] = [] cache_dict[plugin.placeholder].append(plugin) setattr(obj, cache_key, cache_dict) cache_dict = getattr(obj, cache_key) # STAGE TWO GET AND CACHE FOR THIS PLACEHOLDER # WITH POSSIBLE RECURSION. placeholder_cache_key = "_%s_placeholder_%s_plugins_cache" % (lang, placeholder) if placeholder in cache_dict: setattr(obj, placeholder_cache_key, cache_dict[placeholder]) if not hasattr(obj, placeholder_cache_key): # NOTE: We pass obj.parent since we already _know_ that the current page doesn't # provide the plugins for this placeholder. setattr(obj, placeholder_cache_key, get_plugins_recursively(request, obj.parent, placeholder, lang)) return getattr(obj, placeholder_cache_key)
def get_footnotes_for_page(request, page): """ Gets the Footnote instances for `page`, with the correct order. """ plugins = get_cmsplugin_queryset(request) footnote_and_text_plugins = plugins.filter( placeholder__page=page, plugin_type__in=('FootnotePlugin', 'TextPlugin'), language=translation.get_language(), ).order_by('position').values('parent', 'plugin_type', 'pk') pks = [p['pk'] for p in footnote_and_text_plugins] footnote_dict = Footnote.objects.in_bulk(pks) text_dict = Text.objects.in_bulk(pks) def get_footnote_or_text(plugin_pk, plugin_type): d = footnote_dict if plugin_type == 'FootnotePlugin' else text_dict try: return d[plugin_pk] except KeyError: if CMSPLUGIN_FOOTNOTE_DEBUG: raise root_footnote_and_text_plugins = [p for p in footnote_and_text_plugins if p['parent'] is None] footnotes = [] for plugin in root_footnote_and_text_plugins: footnote_or_text = get_footnote_or_text(plugin['pk'], plugin['plugin_type']) if footnote_or_text is None: continue if plugin['plugin_type'] == 'FootnotePlugin': footnotes.append(footnote_or_text) else: for pk in plugin_tags_to_id_list(footnote_or_text.body): footnote = get_footnote_or_text(pk, 'FootnotePlugin') if footnote is not None: footnotes.append(footnote) return footnotes
def show_placeholder_by_id(context, placeholder_name, reverse_id, lang=None, site=None): """ Show the content of a page with a placeholder name and a reverse id in the right language This is mostly used if you want to have static content in a template of a page (like a footer) """ request = context.get('request', False) site_id = get_site_id(site) if not request: return {'content': ''} if lang is None: lang = get_language_from_request(request) key = 'show_placeholder_by_id_pid:' + reverse_id + '_placeholder:' + placeholder_name + '_site:' + str( site_id) + '_l:' + str(lang) content = cache.get(key) if not content: try: page = get_page_queryset(request).get(reverse_id=reverse_id, site=site_id) except: send_missing_mail(reverse_id, request) return {'content': ''} plugins = get_cmsplugin_queryset(request).filter( page=page, language=lang, placeholder__iexact=placeholder_name, parent__isnull=True).order_by('position').select_related() content = "" for plugin in plugins: content += plugin.render_plugin(context, placeholder_name) cache.set(key, content, settings.CMS_CONTENT_CACHE_DURATION) if content: return {'content': mark_safe(content)} return {'content': ''}
def assign_plugins(request, placeholders, template, lang=None, is_fallback=False): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ if not placeholders: return placeholders = tuple(placeholders) lang = lang or get_language_from_request(request) qs = get_cmsplugin_queryset(request) qs = qs.filter(placeholder__in=placeholders, language=lang) plugins = list(qs.order_by('placeholder', 'path')) # If no plugin is present in the current placeholder we loop in the fallback languages # and get the first available set of plugins if (not is_fallback and not (hasattr(request, 'toolbar') and request.toolbar.edit_mode)): disjoint_placeholders = (ph for ph in placeholders if all(ph.pk != p.placeholder_id for p in plugins)) for placeholder in disjoint_placeholders: if get_placeholder_conf("language_fallback", placeholder.slot, template, True): for fallback_language in get_fallback_languages(lang): assign_plugins(request, (placeholder,), template, fallback_language, is_fallback=True) fallback_plugins = placeholder._plugins_cache if fallback_plugins: plugins += fallback_plugins break # If no plugin is present, create default plugins if enabled) if not plugins: plugins = create_default_plugins(request, placeholders, template, lang) plugins = downcast_plugins(plugins, placeholders) # split the plugins up by placeholder # Plugins should still be sorted by placeholder groups = dict((ph_id, build_plugin_tree(ph_plugins)) for ph_id, ph_plugins in groupby(plugins, attrgetter('placeholder_id'))) for placeholder in placeholders: setattr(placeholder, '_plugins_cache', groups.get(placeholder.pk, []))
def get_plugins_recursively(request, obj, placeholder, lang): """ Get plugins for obj, language and placeholder. If no plugins are found and the placeholder config in CMS_PLACEHOLDER_CONF contains "parent_plugins" with a True value, return the plugins for the parent pages recursively. That is plugins are searched until a page with plugins is found or the root page is found. """ if not obj: return [] recurse = False if settings.CMS_PLACEHOLDER_CONF and placeholder in settings.CMS_PLACEHOLDER_CONF: recurse = settings.CMS_PLACEHOLDER_CONF[placeholder].get("parent_plugins") while True: plugins = get_cmsplugin_queryset(request).filter(page=obj, language=lang, parent__isnull=True, placeholder__iexact=placeholder) if not recurse: break if not obj.parent or plugins.count(): break obj = obj.parent return plugins.order_by('placeholder', 'position').select_related()
def assign_plugins(request, placeholders, lang=None): """ Fetch all plugins for the given ``placeholders`` and cast them down to the concrete instances in one query per type. """ placeholders = list(placeholders) if not placeholders: return lang = lang or get_language_from_request(request) # get all plugins for the given placeholders qs = get_cmsplugin_queryset(request).filter(placeholder__in=placeholders, language=lang, parent__isnull=True).order_by( 'placeholder', 'position') plugin_list = downcast_plugins(qs) # split the plugins up by placeholder groups = dict((key, list(plugins)) for key, plugins in groupby( plugin_list, operator.attrgetter('placeholder_id'))) for placeholder in placeholders: setattr(placeholder, '_%s_plugins_cache' % lang, list(groups.get(placeholder.pk, [])))
def get_next_level(current_level): all_plugins = get_cmsplugin_queryset() return all_plugins.filter(parent__in=[x.pk for x in current_level])