def details(request, account_id):
    account = get_object_or_404(Account, pk=account_id)
    if check_permission(request, account) 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_account_info_csv(request, account)
            return CSVResponse(data = data_out, output_name = 'BuildingSpeak_Report_Account_Info')

    main_page_help_text = 'Account Details - view Account details and contact info.'
    tab_help_text = 'click to export Account info to CSV file'

    context = {
        'sidebar':        'buildingspeakapp/shared/account_sidebar.html',
        'tab':            'details',
        'on_acct_page':     True,
        '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':         account.get_all_alerts(reverse_boolean=True),
    }

    template_name = 'buildingspeakapp/accounts/details.html'
    return render(request, template_name, RequestContext(request, context))
def info(request, account_id):
    account = get_object_or_404(Account, pk=account_id)
    if check_permission(request, account) 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()
            account.messages.add(new_event)
            success_message = "'%s' on %s successfully added to account: %s." % (new_event.subject, new_event.when.strftime('%m/%d/%Y'), account.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_account_info_csv(request, account)
            return CSVResponse(data = data_out, output_name = 'BuildingSpeak_Report_Account_Info')
    
    total_SF = float(account.building_set.all().aggregate(Sum('square_footage'))['square_footage__sum'])
    
    main_page_help_text = 'Account Info - view image, lists of efficiency measures, buildings, etc.'
    tab_help_text = 'click to export Account info to CSV file'

    context = {
        'sidebar':              'buildingspeakapp/shared/account_sidebar.html',
        'tab':                  'info',
        'on_acct_page':         True,
        '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() == account],
        'events':         account.get_all_events(reverse_boolean=True),
        'total_SF':       total_SF,
        'adding_events_is_enabled': True,
    }

    template_name = 'buildingspeakapp/accounts/info.html'
    return render(request, template_name, RequestContext(request, context))