def document(request, document_slug, template=None, document=None): """View a wiki document.""" fallback_reason = None # If a slug isn't available in the requested locale, fall back to en-US: try: doc = Document.objects.get(locale=request.LANGUAGE_CODE, slug=document_slug) if (not doc.current_revision and doc.parent and doc.parent.current_revision): # This is a translation but its current_revision is None # and OK to fall back to parent (parent is approved). fallback_reason = 'translation_not_approved' elif not doc.current_revision: # No current_revision, no parent with current revision, so # nothing to show. fallback_reason = 'no_content' except Document.DoesNotExist: # Look in default language: doc = get_object_or_404(Document, locale=settings.WIKI_DEFAULT_LANGUAGE, slug=document_slug) # If there's a translation to the requested locale, take it: translation = doc.translated_to(request.LANGUAGE_CODE) if translation: url = translation.get_absolute_url() url = urlparams(url, query_dict=request.GET) return HttpResponseRedirect(url) elif doc.current_revision: # There is no translation # and OK to fall back to parent (parent is approved). fallback_reason = 'no_translation' # Obey explicit redirect pages: # Don't redirect on redirect=no (like Wikipedia), so we can link from a # redirected-to-page back to a "Redirected from..." link, so you can edit # the redirect. redirect_url = (None if request.GET.get('redirect') == 'no' else doc.redirect_url(request.LANGUAGE_CODE)) if redirect_url: url = urlparams(redirect_url, query_dict=request.GET, redirectslug=doc.slug, redirectlocale=doc.locale) return HttpResponseRedirect(url) # Get "redirected from" doc if we were redirected: redirect_slug = request.GET.get('redirectslug') redirect_locale = request.GET.get('redirectlocale') redirected_from = None if redirect_slug and redirect_locale: try: redirected_from = Document.objects.get(locale=redirect_locale, slug=redirect_slug) except Document.DoesNotExist: pass contributors = doc.contributors.all() products = doc.get_products() if len(products) < 1: product = Product.objects.filter(visible=True)[0] else: product = products[0] topics = Topic.objects.filter(product=product, visible=True, parent=None) ga_push = [] if fallback_reason is not None: ga_push.append(['_trackEvent', 'Incomplete L10n', 'Not Localized', '%s/%s' % (doc.slug, request.LANGUAGE_CODE)]) elif doc.is_outdated(): ga_push.append(['_trackEvent', 'Incomplete L10n', 'Not Updated', '%s/%s' % (doc.parent.slug, request.LANGUAGE_CODE)]) if document_slug in COLLAPSIBLE_DOCUMENTS.get(request.LANGUAGE_CODE, []): document_css_class = 'collapsible' else: document_css_class = '' data = { 'document': doc, 'redirected_from': redirected_from, 'contributors': contributors, 'fallback_reason': fallback_reason, 'is_aoa_referral': request.GET.get('ref') == 'aoa', 'topics': topics, 'product': product, 'products': products, 'ga_push': ga_push, 'document_css_class': document_css_class, } return render(request, template, data)
def document(request, document_slug, template=None, document=None): """View a wiki document.""" fallback_reason = None # If a slug isn't available in the requested locale, fall back to en-US: try: doc = Document.objects.get(locale=request.LANGUAGE_CODE, slug=document_slug) if not doc.current_revision and doc.parent and doc.parent.current_revision: # This is a translation but its current_revision is None # and OK to fall back to parent (parent is approved). fallback_reason = "translation_not_approved" elif not doc.current_revision: # No current_revision, no parent with current revision, so # nothing to show. fallback_reason = "no_content" except Document.DoesNotExist: # Look in default language: doc = get_object_or_404(Document, locale=settings.WIKI_DEFAULT_LANGUAGE, slug=document_slug) # If there's a translation to the requested locale, take it: translation = doc.translated_to(request.LANGUAGE_CODE) if translation: url = translation.get_absolute_url() url = urlparams(url, query_dict=request.GET) return HttpResponseRedirect(url) elif doc.current_revision: # There is no translation # and OK to fall back to parent (parent is approved). fallback_reason = "no_translation" # Obey explicit redirect pages: # Don't redirect on redirect=no (like Wikipedia), so we can link from a # redirected-to-page back to a "Redirected from..." link, so you can edit # the redirect. redirect_url = None if request.GET.get("redirect") == "no" else doc.redirect_url(request.LANGUAGE_CODE) if redirect_url: url = urlparams(redirect_url, query_dict=request.GET, redirectslug=doc.slug, redirectlocale=doc.locale) return HttpResponseRedirect(url) # Get "redirected from" doc if we were redirected: redirect_slug = request.GET.get("redirectslug") redirect_locale = request.GET.get("redirectlocale") redirected_from = None if redirect_slug and redirect_locale: try: redirected_from = Document.objects.get(locale=redirect_locale, slug=redirect_slug) except Document.DoesNotExist: pass contributors = doc.contributors.all() products = doc.get_products() if len(products) < 1: product = Product.objects.filter(visible=True)[0] else: product = products[0] product_topics = Topic.objects.filter(product=product, visible=True, parent=None) ga_push = [] if fallback_reason is not None: ga_push.append( ["_trackEvent", "Incomplete L10n", "Not Localized", "{0!s}/{1!s}".format(doc.slug, request.LANGUAGE_CODE)] ) elif doc.is_outdated(): ga_push.append( [ "_trackEvent", "Incomplete L10n", "Not Updated", "{0!s}/{1!s}".format(doc.parent.slug, request.LANGUAGE_CODE), ] ) if document_slug in COLLAPSIBLE_DOCUMENTS.get(request.LANGUAGE_CODE, []): document_css_class = "collapsible" else: document_css_class = "" if request.MOBILE and "minimal" in request.GET: template = "{0!s}document-minimal.html".format(template) minimal = True else: template = "{0!s}document.html".format(template) minimal = False # Build a set of breadcrumbs, ending with the document's title, and # starting with the product, with the topic(s) in between. # The breadcrumbs are built backwards, and then reversed. # Get document title. If it is like "Title - Subtitle", strip off the subtitle. trimmed_title = doc.title.split(" - ")[0].strip() breadcrumbs = [(None, trimmed_title)] # Get the dominant topic, and all parent topics. Save the topic chosen for # picking a product later. document_topics = doc.topics.order_by("display_order") if len(document_topics) > 0: topic = document_topics[0] first_topic = topic while topic is not None: breadcrumbs.append((topic.get_absolute_url(), topic.title)) topic = topic.parent # Get the product breadcrumbs.append((first_topic.product.get_absolute_url(), first_topic.product.title)) else: breadcrumbs.append((product.get_absolute_url(), product.title)) # The list above was built backwards, so flip this. breadcrumbs.reverse() data = { "document": doc, "redirected_from": redirected_from, "contributors": contributors, "fallback_reason": fallback_reason, "is_aoa_referral": request.GET.get("ref") == "aoa", "product_topics": product_topics, "product": product, "products": products, "related_products": doc.related_products.exclude(pk=product.pk), "ga_push": ga_push, "breadcrumb_items": breadcrumbs, "document_css_class": document_css_class, } response = render(request, template, data) if minimal: response["X-Frame-Options"] = "ALLOW" return response