# -*- coding: utf-8 -*-
#
#  views.py
#  wide-language-index-demo
#

"""
Public facing frontpage.
"""

import json
import random

from flask import (Blueprint, request, render_template, redirect)

from . import forms
from ..index import util

blueprint = Blueprint('public', __name__, static_folder="../static")


@blueprint.route("/", methods=["GET", "POST"])
def home():
    language = request.args.get('language')
    if language:
        return _render_language(language)

    form = forms.SearchForm()
    return render_template("public/home.html",
                           form=form,
                           has_query=False)


def _render_language(language):
    index = util.get_index()
    if language == 'random':
        language = random.choice(list(index.keys()))
        return redirect('/?language={0}'.format(language))

    records = index.get(language)
    inverted_name = util.get_languages().get(language)

    if records:
        record = random.choice(records)
        record_json = json.dumps(record, indent=2, sort_keys=True)

    form = forms.SearchForm(request.args)
    return render_template("public/home.html", form=form, record=record,
                           record_json=record_json,
                           inverted_name=inverted_name,
                           has_query=bool(request.args))

@@ -920,24 +920,65 @@
 (language)%0A%0A
+    record = None%0A    record_json = None%0A
     if recor

Fix an error when no samples were found.