def test_english(self): # Create the English document d = document(title='A doc') d.save() # Now it exists obj = get_object_fallback(Document, 'A doc', 'en-US', '!') eq_(d, obj)
def _hook_template(self, parser, space, title): """Handles Template:Template name, formatting the content using given args""" params = title.split('|') short_title = params.pop(0) template_title = 'Template:' + short_title message = _('The template "%s" does not exist or has no approved ' 'revision.') % short_title template = get_object_fallback(Document, template_title, locale=self.locale, is_template=True) if not template or not template.current_revision: return message if template.id in parser.inclusions: return RECURSION_MESSAGE % template_title else: parser.inclusions.append(template.id) c = template.current_revision.content.rstrip() # Note: this completely ignores the allowed attributes passed to the # WikiParser.parse() method and defaults to ALLOWED_ATTRIBUTES. parsed = parser.parse(c, show_toc=False, attributes=ALLOWED_ATTRIBUTES, locale=self.locale) parser.inclusions.pop() # Special case for inline templates if '\n' not in c: parsed = parsed.replace('<p>', '') parsed = parsed.replace('</p>', '') # Do some string formatting to replace parameters return _format_template_content(parsed, _build_template_params(params))
def _data(docs, locale): """Add the documents and showfor data to the context data.""" data = {} for side, title in docs.iteritems(): data[side] = get_object_fallback(Document, title, locale) data.update(SHOWFOR_DATA) return data
def test_from_french(self): # Create the English document d = document(title='A doc') d.save() # Returns English document for French obj = get_object_fallback(Document, 'A doc', 'fr', '!') eq_(d, obj)
def _data(docs, locale, product): """Add the documents and showfor data to the context data.""" data = {} for side, title in docs.iteritems(): data[side] = get_object_fallback(Document, title, locale) data.update(SHOWFOR_DATA) data.update(search_params={'q_tags': product, 'product': product}) return data
def mobile(request): data = {} for side, title in MOBILE_DOCS.iteritems(): message = _lazy(u'The template "%s" does not exist.') % title data[side] = get_object_fallback(Document, title, request.locale, message) data.update(SHOWFOR_DATA) return render(request, "dashboards/mobile.html", data)
def _hook_include(self, parser, space, name): """Record an include link between documents, and then call super().""" include = get_object_fallback(Document, name, locale=self.locale) if include: self.current_doc.add_link_to(include, 'include') return (super(WhatLinksHereParser, self) ._hook_include(parser, space, name))
def home(request): data = {} for side, title in HOME_DOCS.iteritems(): message = _lazy(u'The template "%s" does not exist.') % title data[side] = get_object_fallback( Document, title, request.locale, message) data.update(SHOWFOR_DATA) return jingo.render(request, 'dashboards/home.html', data)
def test_french(self): # Create English parent document en_d = document() en_d.save() en_r = revision(document=en_d, is_approved=True) en_r.save() # Create the French document fr_d = document(parent=en_d, title='A doc', locale='fr') fr_d.save() obj = get_object_fallback(Document, 'A doc', 'fr', '!') eq_(fr_d, obj) # Also works when English exists d = document(title='A doc') d.save() obj = get_object_fallback(Document, 'A doc', 'fr', '!') eq_(fr_d, obj)
def mobile(request, template=None): data = {} docs = MOBILE_DOCS_FOR_MOBILE if request.MOBILE else MOBILE_DOCS for side, title in docs.iteritems(): message = _lazy(u'The template "%s" does not exist.') % title data[side] = get_object_fallback( Document, title, request.locale, message) data.update(SHOWFOR_DATA) return jingo.render(request, template, data)
def test_translated(self): """If a localization of the English fallback exists, use it.""" en_d = document(title='A doc') en_d.save() en_r = revision(document=en_d, is_approved=True) en_r.save() fr_d = document(parent=en_d, title='Une doc', locale='fr') fr_d.save() # Without an approved revision, the en-US doc should be returned. obj = get_object_fallback(Document, 'A doc', 'fr') eq_(en_d, obj) # Approve a revision, then fr doc should be returned. fr_r = revision(document=fr_d, is_approved=True) fr_r.save() obj = get_object_fallback(Document, 'A doc', 'fr') eq_(fr_d, obj)
def _hook_video(self, parser, space, title): """Handles [[Video:video title]] with locale from parser.""" message = _lazy(u'The video "%s" does not exist.') % title # params, only modal supported for now title, params = build_hook_params(title, self.locale, VIDEO_PARAMS) v = get_object_fallback(Video, title, self.locale, message) if isinstance(v, basestring): return v return generate_video(v, params)
def _hook_internal_link(self, parser, space, name): """Records links between documents, and then calls super().""" title = name.split('|')[0] locale = self.current_doc.locale linked_doc = get_object_fallback(Document, title, locale) if linked_doc is not None: self.current_doc.add_link_to(linked_doc, 'link') return (super(WhatLinksHereParser, self)._hook_internal_link(parser, space, name))
def _hook_internal_link(self, parser, space, name): """Records links between documents, and then calls super().""" title = name.split('|')[0] locale = self.current_doc.locale linked_doc = get_object_fallback(Document, title, locale) if linked_doc is not None: self.current_doc.add_link_to(linked_doc, 'link') return (super(WhatLinksHereParser, self) ._hook_internal_link(parser, space, name))
def _hook_template(self, parser, space, name): """Record a template link between documents, and then call super().""" params = name.split('|') template = get_object_fallback(Document, 'Template:' + params.pop(0), locale=self.locale, is_template=True) if template: self.current_doc.add_link_to(template, 'template') return (super(WhatLinksHereParser, self) ._hook_template(parser, space, name))
def _hook_template(self, parser, space, name): """Record a template link between documents, and then call super().""" params = name.split('|') template = get_object_fallback(Document, 'Template:' + params.pop(0), locale=self.locale, is_template=True) if template: self.current_doc.add_link_to(template, 'template') return (super(WhatLinksHereParser, self)._hook_template(parser, space, name))
def _data(docs, locale, product, only_kb=False): """Add the documents and showfor data to the context data.""" data = {} for side, title in docs.iteritems(): data[side] = get_object_fallback(Document, title, locale) data.update(SHOWFOR_DATA) data.update(search_params={'product': product}) if only_kb: data['search_params'].update(w=1) else: data['search_params'].update(q_tags=product) return data
def test_redirect(self): """Assert get_object_fallback follows wiki redirects.""" target_rev = revision(document=document(title='target', save=True), is_approved=True, save=True) translated_target_rev = revision(document=document( parent=target_rev.document, locale='de', save=True), is_approved=True, save=True) revision(document=document(title='redirect', save=True), content='REDIRECT [[target]]', is_approved=True).save() eq_(translated_target_rev.document, get_object_fallback(Document, 'redirect', 'de'))
def _hook_include(self, parser, space, title): """Returns the document's parsed content.""" message = _('The document "%s" does not exist.') % title include = get_object_fallback(Document, title, locale=self.locale) if not include or not include.current_revision: return message if include.id in parser.inclusions: return RECURSION_MESSAGE % title else: parser.inclusions.append(include.id) ret = parser.parse(include.current_revision.content, show_toc=False, locale=self.locale) parser.inclusions.pop() return ret
def _data(docs, locale, product=None, q_tags=None, only_kb=False): """Add the documents and showfor data to the context data.""" data = {} for side, title in docs.iteritems(): data[side] = get_object_fallback(Document, title, locale) data.update(SHOWFOR_DATA) if product: data.update(search_params={'product': product}) if only_kb: data.setdefault('search_params', {}).update({'w': 1}) elif q_tags: data['search_params'].update(q_tags=q_tags) return data
def _data(docs, locale, product=None, q_tags=None, only_kb=False): """Add the documents and showfor data to the context data.""" data = {} for side, title in docs.iteritems(): data[side] = get_object_fallback(Document, title, locale) data.update(showfor_data()) if product: data.update(search_params={'product': product}) if only_kb: data.setdefault('search_params', {}).update({'w': 1}) elif q_tags: data['search_params'].update(q_tags=q_tags) return data
def _hook_include(self, parser, space, title): """Returns the document's parsed content.""" from wiki.models import Document message = _('The document "%s" does not exist.') % title t = get_object_fallback(Document, title, locale=self.locale) if not t or not t.current_revision: return message if t.id in parser.inclusions: return RECURSION_MESSAGE % title else: parser.inclusions.append(t.id) ret = parser.parse(t.current_revision.content, show_toc=False, locale=self.locale) parser.inclusions.pop() return ret
def test_redirect(self): """Assert get_object_fallback follows wiki redirects.""" target_rev = revision( document=document(title='target', save=True), is_approved=True, save=True) translated_target_rev = revision( document=document(parent=target_rev.document, locale='de', save=True), is_approved=True, save=True) revision( document=document(title='redirect', save=True), content='REDIRECT [[target]]', is_approved=True).save() eq_(translated_target_rev.document, get_object_fallback(Document, 'redirect', 'de'))
def test_redirect_translations_only(self): """Make sure get_object_fallback doesn't follow redirects when working purely in the default language. That would make it hard to navigate to redirects (to edit them, for example). """ revision(document=document(title='target', save=True), content='O hai.', is_approved=True).save() redirect_rev = revision(document=document(title='redirect', save=True), content='REDIRECT [[target]]', is_approved=True, save=True) eq_(redirect_rev.document, get_object_fallback(Document, 'redirect', redirect_rev.document.locale))
def test_redirect_translations_only(self): """Make sure get_object_fallback doesn't follow redirects when working purely in the default language. That would make it hard to navigate to redirects (to edit them, for example). """ revision(document=document(title='target', save=True), content='O hai.', is_approved=True).save() redirect_rev = revision(document=document(title='redirect', save=True), content='REDIRECT [[target]]', is_approved=True, save=True) eq_( redirect_rev.document, get_object_fallback(Document, 'redirect', redirect_rev.document.locale))
def _hook_template(self, parser, space, title): """Handles Template:Template name, formatting the content using given args""" from wiki.models import Document from wiki.models import ALLOWED_ATTRIBUTES params = title.split('|') short_title = params.pop(0) template_title = 'Template:' + short_title message = _('The template "%s" does not exist or has no approved ' 'revision.') % short_title t = get_object_fallback(Document, template_title, locale=self.locale, is_template=True) if not t or not t.current_revision: return message if t.id in parser.inclusions: return RECURSION_MESSAGE % template_title else: parser.inclusions.append(t.id) c = t.current_revision.content.rstrip() # Note: this completely ignores the allowed attributes passed to the # WikiParser.parse() method and defaults to ALLOWED_ATTRIBUTES. parsed = parser.parse(c, show_toc=False, attributes=ALLOWED_ATTRIBUTES, locale=self.locale) parser.inclusions.pop() # Special case for inline templates if '\n' not in c: parsed = parsed.replace('<p>', '') parsed = parsed.replace('</p>', '') # Do some string formatting to replace parameters return _format_template_content(parsed, _build_template_params(params))
def home(request): """The home page.""" if not show_new_sumo(request): return old_home(request) products = Product.objects.filter(visible=True) topics = Topic.objects.filter(visible=True) moz_news = get_object_fallback( Document, MOZILLA_NEWS_DOC, request.locale) try: hot_docs = documents_for( locale=request.locale, topics=[Topic.objects.get(slug=HOT_TOPIC_SLUG)]) except Topic.DoesNotExist: # "hot" topic doesn't exist, move on. hot_docs = None return jingo.render(request, 'landings/home.html', { 'products': products, 'topics': topics, 'hot_docs': hot_docs, 'moz_news': moz_news})
def home(request): """The home page.""" if not show_new_sumo(request): return old_home(request) products = Product.objects.filter(visible=True) topics = Topic.objects.filter(visible=True) moz_news = get_object_fallback(Document, MOZILLA_NEWS_DOC, request.locale) try: hot_docs = documents_for( locale=request.locale, topics=[Topic.objects.get(slug=HOT_TOPIC_SLUG)]) except Topic.DoesNotExist: # "hot" topic doesn't exist, move on. hot_docs = None return jingo.render( request, 'landings/home.html', { 'products': products, 'topics': topics, 'hot_docs': hot_docs, 'moz_news': moz_news })
def home(request): """The home page.""" if request.MOBILE: return redirect_to(request, 'products', permanent=False) products = Product.objects.filter(visible=True) topics = Topic.objects.filter(visible=True) moz_news = get_object_fallback( Document, MOZILLA_NEWS_DOC, request.LANGUAGE_CODE) try: hot_docs, fallback_hot_docs = documents_for( locale=request.LANGUAGE_CODE, topics=[Topic.objects.get(slug=HOT_TOPIC_SLUG)]) except Topic.DoesNotExist: # "hot" topic doesn't exist, move on. hot_docs = fallback_hot_docs = None return jingo.render(request, 'landings/home.html', { 'products': products, 'topics': topics, 'hot_docs': hot_docs, 'fallback_hot_docs': fallback_hot_docs, 'moz_news': moz_news})
def test_empty(self): """get_object_fallback returns message when no objects.""" # English does not exist obj = get_object_fallback(Document, 'A doc', 'en-US', '!') eq_('!', obj)