Page views

Page views are constructed to support both a live preview and an export view that will be rendered to S3 when publishing an election.

Export Inheritance

To swap out key script references on the server once we bake out pages, each view class generally has a child that inherits from it. For example, if there is a RacePage view, there will also be a RacePageExport view.

The child view uses a template that also inherits from the parent’s template, but overrides any resources that will be baked out.

For example, a parent template may reference a script on the server like this:

class RacePage(ClassView):
  template = 'page.html'
<!-- page.html -->

{%block foot%}
<script src="{% static 'theshow/js/app.js'%}"></script>
{%endblock%}

… while the export view’s template would override that reference with the script’s URL on AWS:

class RacePageExport(RacePage):
  template = 'page.export.html'
<!-- page.export.html -->

{% extends "page.html" %}

{%block foot%}
<script src="http://politico.com/election-results/cdn/{{election_day}}/js/app.js"></script>
{%endblock%}

Static context builders

To support exporting views in a management command, the parent view class has a static build_context method that can return a context object. That context object should be enough to render the child template with the django.template.loader.render_to_string method when baking to AWS.