def get_context_data(self, **kwargs):
     context = super(EventListView, self).get_context_data(**kwargs)
     context["fy"] = get_wa_fy()
     context["total"] = get_event_total(
         context.get("fy", {}).get("current_start"),
         context.get("fy", {}).get("current_end"),
         context.get("regions")
     )
     return context
def show_event_count_form(context, **kwargs):

    mode = kwargs.get("mode") or (
        "language" if "language" in context
        else "country" if "country" in context
        else "region" if "wa_region" in context
        else "dashboard"
    )

    if mode == "region":
        default = context["wa_region"].slug if context.get("wa_region") else None
        selected_option = kwargs.get("selected_option", default)
        options = [(r.slug, r.name) for r in WARegion.objects.filter(slug__iexact=selected_option)]
    elif mode == "country":
        default = context["country"].code if context.get("country") else None
        selected_option = kwargs.get("selected_option", default)
        options = [(c.code, c.name) for c in Country.objects.filter(code__iexact=selected_option)]
    elif mode == "language":
        default = context["language"].code if context.get("language") else None
        selected_option = kwargs.get("selected_option", default)
        options = [(l.code, l.name) for l in Language.objects.filter(code__iexact=selected_option)]
    else:
        selected_option = kwargs.get("selected_option", None)
        options = [(r.slug, r.name) for r in WARegion.objects.all()]

    year = int(get_wa_fy().get("full_year"))

    selected_fy = kwargs.get("selected_fy", "0")
    fiscal_years = [(str(x), str(year + x)) for x in range(-4, 2)]

    container = kwargs.get("container", "")
    # If "container" is not specified, then the assumption is that this form is displayed on a page that has no
    # table for the event count and implies that this form will have have to be submitted to another page that does. The
    # default page to display the event count table is the Event Count page.
    form_action = kwargs.get("form_action", reverse("tracking:event_count")) if not container else ""

    return {"mode": mode, "selected_option": selected_option, "selected_fy": selected_fy, "form_action": form_action,
            "options": options, "fiscal_years": fiscal_years, "container": container}
def get_event_count_data(mode="dashboard", option="overall", fy=0):
    """
    Construct and return rows of data to be displayed by an event-count table
    :param mode: Determines what objects are going to be the table entry. For example, "dashboard" mode means that the
        table is going to display a list of all WA regions, while, in "region" mode, it will list all the countries in a
        region. It defaults to "dashboard".
    :param option: The selected option from the <select name="option"> in _event_count_form.html
    :param fy: The number of year that should be added to the current fiscal year. 0 for the current financial year,
        1 for the next, -1 for the last, 3 for three financial years ahead, etc. This will be scope the search.
    :return: A two-dimensional list that represent the whole data table, with each second-dimension list represents a
        row in the table.
    """
    obj_list = WARegion.objects.all() if mode == "dashboard" and option == "overall"\
        else WARegion.objects.get(slug__iexact=option).country_set.all() if mode == "dashboard" and option != "overall"\
        else WARegion.objects.get(slug__iexact=option).country_set.all() if mode == "region"\
        else Country.objects.get(code__iexact=option).language_set.all() if mode == "country"\
        else Language.objects.filter(code__iexact=option) if mode == "language"\
        else []
    fiscal_year = get_wa_fy(datetime.now().date().year + int(fy))
    data = []

    for x in obj_list:
        # Create a row that contains just the monthly counts
        term = x.slug if isinstance(x, WARegion) else x.code
        row = [
            _get_monthly_count(fiscal_year.get("full_year"), month, x, term.lower())
            for month in range(10, 13) + range(1, 10)
        ]
        # Add the total of monthly counts to the end of the row
        row.append(sum(row))
        # Add the appropriate object name and link to the beginning of the row
        row.insert(0, mark_safe("<a href=\"" + x.get_absolute_url() + "\">" + x.name + "</a>"))
        # Register the finalized row entry
        data.append(row)

    return data