def details(request, account_id, space_id):
    account = get_object_or_404(Account, pk=account_id)
    space = get_object_or_404(Space, pk=space_id)
    if check_permission(request, account, space) is False:
        return HttpResponseRedirect("/%s/status/" % (account.id))

    # ---branching for POST vs. GET request
    if request.method == "POST":  # If a form has been submitted...
        if "export_details_tab" in request.POST:
            data_out = generate_space_info_csv(request, space)
            return CSVResponse(data=data_out, output_name="BuildingSpeak_Report_Space_Info")

    main_page_help_text = "Space Details - view Space details."
    tab_help_text = "click to export Space info to CSV file"

    context = {
        "sidebar": "buildingspeakapp/shared/account_sidebar.html",
        "tab": "details",
        "main_page_help_text": main_page_help_text,
        "tab_help_text": tab_help_text,
        "user": request.user,
        "account": account,
        "accounts": request.user.account_set.order_by("id"),
        "buildings": account.building_set.order_by("name"),
        "spaces": Space.objects.filter(Q(building__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "meters": account.meter_set.order_by("name"),
        "equipments": Equipment.objects.filter(Q(buildings__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "measures": EfficiencyMeasure.objects.filter(
            Q(equipments__buildings__account=account) | Q(meters__account=account)
        )
        .distinct()
        .order_by("name"),
        "alerts": space.get_all_alerts(reverse_boolean=True),
        "space": space,
        "space_ESPM_type": space.get_ESPM_type_display(),
    }

    template_name = "buildingspeakapp/spaces/details.html"
    return render(request, template_name, RequestContext(request, context))
def info(request, account_id, space_id):
    account = get_object_or_404(Account, pk=account_id)
    space = get_object_or_404(Space, pk=space_id)
    if check_permission(request, account, space) is False:
        return HttpResponseRedirect("/%s/status/" % (account.id))

    # ---branching for POST vs. GET request
    if request.method == "POST":  # If a form has been submitted...
        # ... and the form was the add event form
        if "add_event" in request.POST:
            add_event_form = AddEventForm(request.POST)
            if add_event_form.is_valid():  # All validation rules pass
                # Process the data in add_event_form.cleaned_data
                new_event = Message()
                new_event.__setattr__("message_type", "Event")
                new_event.__setattr__("when", add_event_form.cleaned_data["when"])
                new_event.__setattr__("subject", add_event_form.cleaned_data["subject"])
                new_event.save()
                space.messages.add(new_event)
                success_message = "'%s' on %s successfully added to space: %s." % (
                    new_event.subject,
                    new_event.when.strftime("%m/%d/%Y"),
                    space.name,
                )
                messages.success(request, success_message)
        # ... and the user has hit 'export' on the info tab
        elif "export_info_tab" in request.POST:
            data_out = generate_space_info_csv(request, space)
            return CSVResponse(data=data_out, output_name="BuildingSpeak_Report_Space_Info")

    main_page_help_text = "Space Info - view image, lists of efficiency measures, buildings, etc."
    tab_help_text = "click to export Space info to CSV file"

    context = {
        "sidebar": "buildingspeakapp/shared/account_sidebar.html",
        "tab": "info",
        "main_page_help_text": main_page_help_text,
        "tab_help_text": tab_help_text,
        "user": request.user,
        "account": account,
        "accounts": request.user.account_set.order_by("id"),
        "buildings": account.building_set.order_by("name"),
        "spaces": Space.objects.filter(Q(building__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "meters": account.meter_set.order_by("name"),
        "equipments": Equipment.objects.filter(Q(buildings__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "measures": EfficiencyMeasure.objects.filter(
            Q(equipments__buildings__account=account) | Q(meters__account=account)
        )
        .distinct()
        .order_by("name"),
        "alerts": [x for x in request.user.user_profile.alerts.all() if x.target() == space],
        "events": space.get_all_events(reverse_boolean=True),
        "space": space,
        "space_measures": EfficiencyMeasure.objects.filter(Q(equipments__spaces=space) | Q(meters__space=space))
        .distinct()
        .order_by("name"),
        "adding_events_is_enabled": True,
    }

    template_name = "buildingspeakapp/spaces/info.html"
    return render(request, template_name, RequestContext(request, context))