def test_standalone_renderer(self): """The renderer also works when used without a wrapping template""" renderer = TemplatePluginRenderer() renderer.register_template_renderer( HTML, ["renderer/html.html", "renderer/html.html"]) page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context()), "<b>Hello</b>\n") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context({"outer": "Test"})), "<b>Hello</b>Test\n") regions = Regions.from_item(page, renderer=renderer, timeout=3) self.assertEqual(regions.render("main", Context({"outer": "Test2"})), "<b>Hello</b>Test2\n") regions = Regions.from_item(page, renderer=renderer, timeout=3) # Output stays the same. self.assertEqual(regions.render("main", Context({"outer": "Test3"})), "<b>Hello</b>Test2\n") self.assertEqual(regions.cache_key("main"), "testapp.page-%s-main" % page.pk)
def article_detail(request, slug): page = page_for_app_request(request) page.activate_language(request) article = get_object_or_404(articles_for_page(page, allow_future=True), slug=slug) ancestors = list(page.ancestors().reverse()) edit = ( request.user.is_authenticated and request.user.section_set.filter(pk=article.section.pk).exists() ) return render( request, article.template.template_name, { "page": page, "article": article, "obj": article, "edit": edit, "header_image": article.get_header_image() or page.get_header_image(), "title": article.title, "meta_tags": meta_tags([article, page] + ancestors, request=request), "regions": Regions.from_item(article, renderer=renderer, timeout=60,), "page_regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, )
def article_detail(request, slug): page = page_for_app_request(request) page.activate_language(request) ancestors = list(page.ancestors().reverse()) article = get_object_or_404(Article, slug=slug, namespace=page.namespace, language_code=page.language_code) return render( request, 'blog/article_detail.html', { 'page': page, 'title': article.title, 'article': article, "meta_tags": meta_tags([article, page] + ancestors, request=request), "blog_regions": Regions.from_item(article, renderer=renderer, timeout=60), "regions": Regions.from_item(page, renderer=cms_renderer, timeout=60, inherit_from=ancestors), })
def event_detail(request, year, month, day, slug): page = page_for_app_request(request) page.activate_language(request) event = get_object_or_404( event_list_for_page(page, past_events=True), slug=slug, section=page.site.section, start_date__year=year, start_date__month=month, start_date__day=day, ) ancestors = list(page.ancestors().reverse()) return render( request, event.template.template_name, { "page": page, "event": event, "obj": event, "header_image": event.get_header_image() or page.get_header_image(), "title": event.title, "meta_tags": meta_tags( [event, page] + ancestors, request=request, ), "regions": Regions.from_item( event, renderer=renderer, timeout=60, ), "page_regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, )
def page_detail(request, path=None): page = get_object_or_404( Page.objects.active(), path=f"/{path}/" if path else '/', ) if page.redirect_to_url or page.redirect_to_page: return redirect(page.redirect_to_url or page.redirect_to_page) edit = request.user.is_authenticated and request.user.is_staff page.activate_language(request) ancestors = list(page.ancestors().reverse()) return render( request, page.template.template_name, { "page": page, "edit": edit, "header_image": page.image, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=renderer, timeout=60, inherit_from=ancestors), }, )
def teams_for_section(request): page = page_for_app_request(request) page.activate_language(request) section = page.site.section ancestors = list(page.ancestors().reverse()) return render_list( request, Team.objects.filter( section=section, language_code=page.language_code, ), { "page": page, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, paginate_by=10, )
def error404(request, exception): query = Article.objects.filter( slug=[x for x in request.path.split('/') if x][-1]) if bool(query): return redirect(query[0].get_absolute_url()) try: page = get_landing_page(request) except Http404: page = Page.objects.first() page.activate_language(request) return render( request, "404.html", { "exception": exception, "page": page, "regions": Regions.from_item( page, renderer=renderer, timeout=60, ), "header_image": page.get_header_image(), }, )
def event_list(request): page = page_for_app_request(request) page.activate_language(request) ancestors = list(page.ancestors().reverse()) event_list = event_list_for_page(page) if "section" in request.GET: event_list = event_list.filter(section__slug=request.GET["section"]) return render_list( request, event_list, { "location_list": Location.objects.filter(event__in=event_list, ).distinct(), "page": page, "header_image": page.get_header_image(), "vapid_public_key": settings.VAPID_PUBLIC_KEY, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, paginate_by=12, )
def event_list_for_section(request, pk): page = page_for_app_request(request) page.activate_language(request) section = get_object_or_404(Section, pk) event_list = event_list_for_page(page) event_list = event_list.filter(section=section) ancestors = list(page.ancestors().reverse()) return render_list( request, event_list_for_page(page), { "page": page, "header_image": page.get_header_image(), "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), "section": section, }, paginate_by=20, )
def team_detail(request, pk): page = page_for_app_request(request) page.activate_language(request) team = get_object_or_404(Team, pk=pk) ancestors = list(page.ancestors().reverse()) return render( request, "people/team_detail.html", { "page": page, "team": team, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, )
def create(request): page = page_for_app_request(request) page.activate_language(request) campaign = page.campaign form = TestimonialForm(campaign=campaign) if request.POST: form = TestimonialForm(request.POST, request.FILES, campaign=campaign) if form.is_valid(): form.save() return redirect(reverse_app(f"testimonials-{campaign.pk}", "index")) return render( request, page.template_key, { "form": form, "campaign": campaign, "page": page, "regions": Regions.from_item( page, renderer=renderer, timeout=60, inherit_from=page.ancestors().reverse(), ), }, )
def article_list(request): page = page_for_app_request(request) page.activate_language(request) article_list = articles_for_page(page) category_list = Category.objects.filter(article__in=article_list).distinct() if request.GET.get("category", None): article_list = article_list.filter( category__slug__in=request.GET.getlist("category") ) q = "" if request.GET.get("search", ""): vector = ( SearchVector("title", weight="A") + SearchVector("category__name", weight="A") + SearchVector("blog_richtext_set__text", weight="A") + SearchVector("blog_glossaryrichtext_set__text", weight="A") + SearchVector("author__first_name", weight="B") + SearchVector("author__last_name", weight="B") ) query = consume(request.GET["search"]) q = request.GET["search"] article_list = ( article_list.annotate( rank=SearchRank(vector, query, cover_density=True), headline=SearchHeadline( 'blog_richtext_set__text', query, max_words=25, min_words=20, max_fragments=2, ) ) .filter(rank__gt=0) .order_by("-rank") .distinct() ) ancestors = list(page.ancestors().reverse()) return render_list( request, article_list, { "page": page, "q": q, "header_image": page.get_header_image(), "vapid_public_key": settings.VAPID_PUBLIC_KEY, "category_list": category_list, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, ), }, paginate_by=12, )
def location_detail(request, slug): page = page_for_app_request(request) page.activate_language(request) location = get_object_or_404(Location.objects.all(), slug=slug) ancestors = list(page.ancestors().reverse()) return render( request, "events/location_detail.html", { "page": page, "location": location, "obj": location, "title": location.name, "header_image": location.header_image or page.get_header_image(), "event_list": location.event_set.filter(end_date__gte=timezone.now(), ), "meta_tags": meta_tags([location, page] + ancestors, request=request), "regions": Regions.from_item( location, renderer=location_renderer, timeout=60, ), "page_regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, )
def test_plugin_template_instance(self): renderer = TemplatePluginRenderer() renderer.register_template_renderer(HTML, Template("{{ plugin.html|safe }}")) page = Page.objects.create(template_key="standard") HTML.objects.create(parent=page, ordering=10, region="main", html="<b>Hello</b>") regions = Regions.from_item(page, renderer=renderer) self.assertEqual(regions.render("main", Context()), "<b>Hello</b>") self.assertEqual(regions.render("main", None), "<b>Hello</b>")
def page_detail(request, path=None): page = get_object_or_404( Page.objects.active(), path='/{}/'.format(path) if path else '/', ) page.activate_language(request) return render( request, page.template.template_name, { 'page': page, 'regions': Regions.from_item(page, renderer=renderer, inherit_from=page.ancestors().reverse()), })
def error500(request): page = get_landing_page(request) page.activate_language(request) return render( request, "500.html", { "page": page, "regions": Regions.from_item( page, renderer=renderer, timeout=60, ), "header_image": page.get_header_image(), }, )
def person_detail(request, pk): page = page_for_app_request(request) page.activate_language(request) person = get_object_or_404(Person, pk=pk) articles = Article.objects.filter(author=person) memberships = person.membership_set.filter( team__language_code=page.language_code) ancestors = list(page.ancestors().reverse()) return render( request, "people/person_detail.html", { "page": page, "person": person, "memberships": memberships, "articles": articles, "header_image": page.get_header_image(), "title": person.get_full_name(), "meta_tags": meta_tags( [page] + ancestors, request=request, title=person.get_full_name(), description=person.description(), ), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, )
def page_detail(request, path=None): page = Page.objects.active().filter(path=f"/{path}/" if path else "/") if path is None and not bool(page): return redirect(get_landing_page(request).path) if not bool(page): page = Page.objects.active().filter( path=f"/{request.LANGUAGE_CODE}/{path}/") if bool(page): return redirect(page[0].path) for language_code, _ in settings.LANGUAGES: page = Page.objects.active().filter( path=f"/{language_code}/{path}/") if bool(page): return redirect(page[0].path) raise Http404() page = page[0] if page.redirect_to_url or page.redirect_to_page: return redirect(page.redirect_to_url or page.redirect_to_page) edit = (request.user.is_authenticated and request.user.section_set.filter(pk=page.site.section.pk).exists()) page.activate_language(request) ancestors = list(page.ancestors().reverse()) return render( request, page.template.template_name, { "page": page, "edit": edit, "header_image": page.get_header_image(), "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=renderer, timeout=60, ), }, )
def page_detail(request, path=None): page = get_object_or_404(Page.objects.active(), path=("/%s/" % path) if path else "/") page.activate_language(request) if page.redirect_to_url or page.redirect_to_page: return redirect(page.redirect_to_url or page.redirect_to_page) return render( request, page.template.template_name, { "page": page, "regions": Regions.from_item(page, renderer=renderer, inherit_from=page.ancestors().reverse()), }, )
def glossary(request): page = page_for_app_request(request) page.activate_language(request) ancestors = list(page.ancestors().reverse()) entries = Entry.objects.filter(language_code=page.language_code) q = "" if "search" in request.GET and request.GET["search"]: vector = SearchVector("name", weight="A") + SearchVector("content", weight="B") query = consume(request.GET["search"]) print(query) q = request.GET["search"] print(q) entries = ( entries.annotate(rank=SearchRank(vector, query)) .filter(rank__gte=0.1) .order_by("-rank") ) return render_list( request, entries, { "page": page, "meta_tags": meta_tags([page] + ancestors, request=request), "header_image": page.get_header_image(), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), "search": q, }, paginate_by=50, )
def article_list(request): page = page_for_app_request(request) page.activate_language(request) ancestors = list(page.ancestors().reverse()) article_list = Article.objects.filter(namespace=page.namespace, publication_date__lte=timezone.now()) return render_list( request, article_list, { 'page': page, 'article_list': article_list, "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item(page, renderer=cms_renderer, timeout=60, inherit_from=ancestors), })
def error404(request, exception): query = Article.objects.filter(slug=request.path.replace('/', '')) if query.exists(): return redirect(query[0].get_absolute_url()) page = get_landing_page(request) page.activate_language(request) return render( request, "404.html", { "exception": exception, "page": page, "regions": Regions.from_item( page, renderer=renderer, timeout=60, ), "header_image": page.get_header_image(), }, )
def index(request): page = page_for_app_request(request) page.activate_language(request) campaign = page.campaign testimonials = campaign.testimonial_set.filter(public=True, validated=True) create_url = reverse_app(f"testimonials-{campaign.pk}", "create") paginator = Paginator(testimonials, 27) page_number = request.GET.get("page", 1) page_obj = paginator.get_page(page_number) ancestors = list(page.ancestors().reverse()) return render( request, page.template_key, { "campaign": campaign, "page_obj": page_obj, "page": page, "meta_tags": meta_tags([page] + ancestors, request=request), "create_url": create_url, "regions": Regions.from_item( page, renderer=renderer, timeout=60, inherit_from=page.ancestors().reverse(), ), }, )
def category_list(request, slug): page = page_for_app_request(request) page.activate_language(request) articles = articles_for_page(page).filter(category__slug=slug) ancestors = list(page.ancestors().reverse()) return render_list( request, articles, { "page": page, "header_image": page.get_header_image(), "meta_tags": meta_tags([page] + ancestors, request=request), "regions": Regions.from_item( page, renderer=pages.renderer.renderer, timeout=60, inherit_from=ancestors, ), }, paginate_by=10, )