Esempio n. 1
0
def index(request):
    """
    index(request)
    
    Site Front Page.

    Presents introductory text and a map of NCA regions. A form
    allows selection of a region. On submit, user is redirected
    to the rgn_smry view at the selected region.
    """
    #     smrylogger.debug("INDEX VIEW")

    # Initialize a session.
    site_attr.init_session(request)

    attr_dct = site_attr.set_attrs(request, "modis_lnd_cvr")
    smrylogger.debug("index( )|attr_dct: %s", attr_dct)

    # Region selection form.
    if request.method == "POST":
        rgn_form = RegionForm(request.POST)
        if rgn_form.is_valid():
            # Update attributes.
            for key in rgn_form.cleaned_data.keys():
                attr_dct[key] = rgn_form.cleaned_data[key]
            #             smrylogger.debug("index( )|attr_dct: %s", attr_dct)

            # Set session attributes to dictionary items.
            site_attr.set_session_attr(request, attr_dct)

            # Create response to redirect to region summary.
            response = HttpResponseRedirect("rgn_smry/")
    else:
        # Create an instance of class RegionForm.
        rgn_form = RegionForm(
            initial={"region": abbrv_to_rgn[attr_dct["region"]], "dscrpt_txt": (attr_dct["dscrpt_txt"] == "True")}
        )

        # Get introductory text from the database and process to html.
        intro_text = text_items.get_txt_html("index", "intro_text")
        intro_text_1 = text_items.get_txt_html("index", "intro_text_1", replace={"<p>": "", "</p>": "<br /><br />"})

        # Load the region summary template.
        index_template = loader.get_template("smry/index.html")
        context = RequestContext(
            request,
            {"form": rgn_form, "form_help": rgn_form.form_help, "intro_text": intro_text, "intro_text_1": intro_text_1},
        )
        response = HttpResponse(index_template.render(context))

    return response
Esempio n. 2
0
def about(request):
    """
    about(request)
    
    About view.
    """
    #     smrylogger.debug("ABOUT VIEW")
    html = text_items.get_txt_html("about", "about")
    #     smrylogger.debug("html: %s", html)
    title = "U.S. Land Cover Indicators"

    about_template = loader.get_template("smry/about.html")
    context = RequestContext(request, {"title": title, "about_text": html})
    response = HttpResponse(about_template.render(context))

    return response
Esempio n. 3
0
def rgn_smry(request):
    """
    rgn_smry(request)
    
    Regional summary view.
    """
    smrylogger.debug("REGIONAL SUMMARY VIEW")

    # Set attributes.
    attr_dct = site_attr.set_attrs(request, "modis_lnd_cvr")

    # Create an instance of class Region_Region_Form.
    rgn_form = RegionYearForm_MODIS_LandCover(
        initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
    )

    if request.method == "POST":
        # Create an instance of class RegionYearForm and populate
        # with request data.
        rgn_form = RegionYearForm_MODIS_LandCover(request.POST)

        if rgn_form.is_valid():
            # Update attributes.
            for key in rgn_form.cleaned_data.keys():
                attr_dct[key] = rgn_form.cleaned_data[key]
            # 1
            smrylogger.debug("1 rgn_smry( )|attr_dct: %s", attr_dct)

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 2
            smrylogger.debug("2 rgn_smry( )|attr_dct: %s", attr_dct)
        else:
            # 3
            smrylogger.debug("3 rgn_smry( )|Warning: rgn_form is invalid")
    else:
        # Create an instance of class RegionForm.
        rgn_form = RegionYearForm_MODIS_LandCover(
            initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
        )

    if attr_dct["dscrpt_txt"]:
        # Get descriptive text from the database.
        intro_txt_html = text_items.get_txt_html("rgn_smry", "rgn_smry_intro")
        trends_txt_html = text_items.get_txt_html("rgn_smry", "trends", replace=del_p)
        vcf_txt_html = text_items.get_txt_html("rgn_smry", "vcf", replace=del_p)
    else:
        intro_txt_html = ""
        trends_txt_html = ""
        vcf_txt_html = ""

    # Get file name attributes from the database.
    # 4
    smrylogger.debug("4 rgn_smry( )|attr_dct: %s", attr_dct)
    # Expand USA48 to full region name.
    if attr_dct["region"] == "USA48":
        region_name = name_dcts.abbrv_to_rgn[attr_dct["region"]]
    else:
        region_name = attr_dct["region"]
    rgn_img = RegionImages()
    rgn_img.rgn_smry_images(region_name, attr_dct["year"])

    # Create distribution table data as a list.
    table_list = create_distr_table_list(attr_dct["region"], attr_dct["year"])

    # Create request context from attributes.
    dct = rgn_img.attr.dct
    for key in attr_dct.keys():
        dct[key] = attr_dct[key]
    dct["width"] = "1030"
    dct["rgn_name"] = attr_dct["region"]
    dct["intro_txt"] = str(intro_txt_html)
    dct["trends_txt"] = str(trends_txt_html)
    dct["vcf_txt"] = str(vcf_txt_html)
    dct["form"] = rgn_form
    dct["form_help"] = rgn_form.form_help
    dct["table_list"] = table_list

    rgn_img = None

    # Load the region summary template.
    rgn_template = loader.get_template("smry/rgn_smry.html")

    # Create context and render.
    context = RequestContext(request, dct)
    response = HttpResponse(rgn_template.render(context))

    return response