def report_excel(request): log_start_time = datetime.utcnow() if not request.user.is_authenticated(): return HttpResponseRedirect('/ui/login/?next=/ui/') log_entry = LogEntry() log_entry.user = request.user log_entry.timestamp = log_start_time log_entry.post_data = request.POST if request.POST.has_key('chkReporter') \ and request.POST.has_key('hdnResultType') \ and request.POST.has_key('txtStartDate') \ and request.POST.has_key('txtEndDate'): start_date = request.POST.get('txtStartDate') end_date = request.POST.get('txtEndDate') reporters = request.POST.get('chkReporter').split(',') reporter_type = request.POST.get('hdnResultType') else: return HttpResponse('insufficient parameters') filename = 'wqm_reporters_%s_%s.xls' % (start_date, end_date) if reporter_type == 'NAME': samples = Sample.objects.filter(reporter_name__in=reporters, date_taken__gte=start_date, date_taken__lte=end_date).order_by('date_taken') log_entry.event_type = 'rep_reporter_name' elif reporter_type == 'TEL': samples = Sample.objects.filter(taken_by__connections__identity__in=reporters, date_taken__gte=start_date, date_taken__lte=end_date).order_by('date_taken') log_entry.event_type = 'rep_reporter_tel' else: return HttpResponse('Unknown reporter type supplied') # Create the HttpResponse object with the appropriate XLS header info. response = HttpResponse(mimetype="application/ms-excel") response['Content-Disposition'] = 'attachment; filename=%s' % filename wb = Reports.get_basic_for_samples(samples) wb.save(response) log_entry.set_processing_time(log_start_time) log_entry.save() return response
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
def report_preview(request, domain_id): log_start_time = datetime.utcnow() if not request.user.is_authenticated(): return HttpResponseRedirect('/ui/login/?next=/ui/') try: # attempt load up the requested domain if the user has access to it domain = Domain.objects.filter( membership__member_type = ContentType.objects.get_for_model(User), membership__member_id = request.user.id, membership__is_active=True, # Looks in membership table is_active=True, id = domain_id)[0] # Looks in domain table except IndexError: # if it wasn't loaded it either doesn't exist (in which case the user is poking around with the url) # or the user doesn't have access to it. Either way, best to just display access denied so people # messing with URLs get as little info as possible. #TODO: redirect to an access denied page return HttpResponse('access denied - you don\'t have access to this domain id') log_entry = LogEntry() log_entry.user = request.user log_entry.timestamp = log_start_time log_entry.post_data = request.POST if request.POST.has_key('chkReporter') \ and request.POST.has_key('hdnResultType') \ and request.POST.has_key('txtStartDate') \ and request.POST.has_key('txtEndDate'): start_date = request.POST.get('txtStartDate') end_date = request.POST.get('txtEndDate') reporters = request.POST.getlist('chkReporter') reporter_type = request.POST.get('hdnResultType') else: return HttpResponse('insufficient parameters') filename = 'wqm_reporters_%s_%s_%s.xls' % (domain.name.replace(' ', '_') .replace('\\','_').replace('//','_').replace('?','_').replace("'","_"), start_date, end_date) if reporter_type == 'NAME': samples = Sample.objects.filter(reporter_name__in=reporters, date_taken__gte=start_date, date_taken__lte=end_date).order_by('date_taken') log_entry.event_type = 'rep_prev_reporter_name' elif reporter_type == 'TEL': samples = Sample.objects.filter(taken_by__connections__identity__in=reporters, date_taken__gte=start_date, date_taken__lte=end_date).order_by('date_taken') log_entry.event_type = 'rep_prev_reporter_tel' else: return HttpResponse('Unknown reporter type supplied') report_html = Reports.get_report_html(samples) template = loader.get_template('ui-report-preview.html') context = Context({ 'domain' : domain, 'user' : request.user, 'report_table' : report_html, 'start_date' : start_date, 'end_date' : end_date, 'result_type' : reporter_type, 'reporters' : ','.join(reporters) }) response = HttpResponse(template.render(context)) log_entry.set_processing_time(log_start_time) log_entry.save() return response
def reporters(request, domain_id): log_start_time = datetime.utcnow() # user authentication - this is done manually to force the use of a login URL specifc to the UI app # dev version of django can do this using @login_required, but 1.2 can't, hence this work-around if not request.user.is_authenticated(): return HttpResponseRedirect('/ui/login/?next=%s' % request.path) try: # attempt load up the requested domain if the user has access to it domain = Domain.objects.filter( membership__member_type = ContentType.objects.get_for_model(User), membership__member_id = request.user.id, membership__is_active=True, # Looks in membership table is_active=True, id = domain_id)[0] # Looks in domain table except IndexError: # if it wasn't loaded it either doesn't exist (in which case the user is poking around with the url) # or the user doesn't have access to it. Either way, best to just display access denied so people # messing with URLs get as little info as possible. #TODO: redirect to an access denied page return HttpResponse('access denied - you don\'t have access to this domain id') # authentication and authorisation taken care of log_entry = LogEntry() log_entry.user = request.user log_entry.timestamp = log_start_time log_entry.post_data = request.POST log_entry.event_type = 'page_search' # check if this is a search and there's something to search on if request.POST.has_key('btnSearch') and request.POST.has_key('txtName') and request.POST.get('txtName') != '': search_name = request.POST.get('txtName') search_tel = '' result_type = 'NAME' result = Sample.objects.filter(reporter_name__icontains=search_name).filter(sampling_point__wqmarea__wqmauthority__domain__id=domain_id).values('reporter_name').order_by('reporter_name').distinct() reporters = list(rep['reporter_name'] for rep in result) elif request.POST.has_key('btnSearch') and request.POST.has_key('txtTel') and request.POST.get('txtTel') != '': result_type = 'TEL' search_name = '' search_tel = request.POST.get('txtTel') result = PersistantConnection.objects \ .filter(identity__icontains=search_tel) \ .filter(reporter__sample__sampling_point__wqmarea__wqmauthority__domain__id=domain_id) \ .values('identity').order_by('identity').distinct() reporters = list(rep['identity'] for rep in result) else: reporters = None search_name = '' search_tel = '' result_type = '' template = loader.get_template('ui-reporters.html') context = Context({ 'domain' : domain, 'user' : request.user, 'reporters' : reporters, 'search_name' : search_name, 'search_tel' : search_tel, 'result_type' : result_type, }) response = HttpResponse(template.render(context)) log_entry.set_processing_time(log_start_time) log_entry.save() return response