Example #1
0
def report(request):
    log_start_time = datetime.utcnow()
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/ui/login/?next=/ui/')

    sample_point_ids = []
    # create a log entry    
    log_entry = LogEntry()
    log_entry.user = request.user
    log_entry.timestamp = log_start_time
    log_entry.post_data = request.POST
    
    #check if the post was from an info window (single sample point)
    if request.POST.has_key('btnInfoWindow') \
        and request.POST.has_key('sample_point_id') \
        and request.POST.has_key('txtInfoStartDate') \
        and request.POST.has_key('txtInfoEndDate'):
         
        sample_point_ids.append(request.POST['sample_point_id'])
        start_date = request.POST.get('txtInfoStartDate')
        end_date = request.POST.get('txtInfoEndDate')
        report_type = 'FULL'
         
    elif request.POST.has_key('btnSidePanel')\
        and request.POST.has_key('sample_point') \
        and request.POST.has_key('txtSideStartDate') \
        and request.POST.has_key('txtSideEndDate') \
        and request.POST.has_key('radReportType'):
        
        report_type = request.POST.get('radReportType') 
        start_date = request.POST.get('txtSideStartDate') 
        end_date = request.POST.get('txtSideEndDate') 
        sample_point_ids = request.POST.getlist('sample_point')
    else:
        return HttpResponse('insufficient parameters')
    
    
    
    if report_type == 'SITE_COUNT':
        filename = Reports.get_filename(sample_point_ids, start_date, end_date, 'site-test-count')
        log_entry.event_type = 'rep_site_test_count'
    elif report_type == 'REPORTER_COUNT':
        filename = Reports.get_filename(sample_point_ids, start_date, end_date, 'reporter-test-count')
        log_entry.event_type = 'rep_test_count'
    else:
        filename = Reports.get_filename(sample_point_ids, start_date, end_date)
        log_entry.event_type = 'rep_full'
    
    # Create the HttpResponse object with the appropriate XLS header info.
    response = HttpResponse(mimetype="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    
    if report_type == 'SITE_COUNT':
        wb = Reports.render_excel(Reports.get_test_counts(sample_point_ids, start_date, end_date))
    elif report_type == 'REPORTER_COUNT':
        wb = Reports.render_excel(Reports.get_reporter_test_counts(sample_point_ids, start_date, end_date))
    else:
        wb = Reports.get_basic(sample_point_ids, start_date, end_date)
        
    wb.save(response)
    
    log_entry.set_processing_time(log_start_time)
    log_entry.save()
    
    return response