Exemplo n.º 1
0
def reporting(request):
	"""
	Page to generate reports.
	"""
	from terapix.youpi.forms import ReportAdvancedImageForm

	# Sanity checks: looks for manfatory info
	mandatory = ('id', 'title', 'description', 'formats')
	for r in global_reports:
		for mand in mandatory:
			if not r.has_key(mand):
				raise ReportIncompleteDefinitionError, "Report '%s' has no '%s' set (required)" % (r['title'], mand)
	plugins = manager.plugins
	for p in plugins:
		try:
			rdata = p.reports()
			if not rdata: continue
			for r in rdata:
				for mand in mandatory:
					if not r.has_key(mand):
						raise ReportIncompleteDefinitionError, "Report '%s' has no '%s' set (required)" % (r['title'], mand)
		except AttributeError:
			pass

	global_reports.sort(cmp=lambda x,y: cmp(x['title'], y['title']))

	menu_id = 'reporting'
	return render_to_response('reporting.html', {	
						# Global reports
						'reports'			: global_reports,
						'plugins' 			: manager.plugins, 
						'selected_entry_id'	: menu_id, 
						'title' 			: get_title_from_menu_id(menu_id),
						'report_formats'	: ReportFormat.formats(),
					}, 
					context_instance = RequestContext(request))
Exemplo n.º 2
0
def get_global_report(request, reportId, format):
	"""
	Generates a global report.
	@param reportId report Id
	@param format report's output format
	"""
	post = request.POST
	# Supported report output formats
	formats = ReportFormat.formats()
	if format not in [f['name'] for f in formats]:
		raise ValueError, "unsupported report output format: " + format

	ext = ''
	for f in formats:
		if f['name'] == format:
			ext = f['ext'].lower()
			break
	fname = "%s-%s.%s" % (request.user.username, reportId, ext)
	title = get_report_data(global_reports, reportId)['title']

	if reportId == 'imssavedselections':
		import time
		sels = ImageSelections.objects.all().order_by('date')
		if not sels: 
			return render_to_response('report.html', {	
				'report_title' 		: title,
				'report_content' 	: "No saved selections found.",
			}, context_instance = RequestContext(request))

		if format != ReportFormat.HTML:
			fout = open(os.path.join(settings.MEDIA_ROOT, settings.MEDIA_TMP, fname), 'w')

			if format == ReportFormat.CSV:
				import csv
				writer = csv.writer(fout)
				k = 1
				for s in sels:
					writer.writerow([k, s.name])
					k += 1
			elif format == ReportFormat.PDF:
				for s in sels:
					fout.write(s.name + '\n')

			fout.close()
			return render_to_response('report.html', {	
				'report_title' 		: title,
				'report_content' 	: "<div class=\"report-download\"><a href=\"%s\">Download %s file report</a></div>" % 
					(os.path.join('/media', settings.MEDIA_TMP, fname + "?%s" % time.time()), format),
			}, context_instance = RequestContext(request))
		else:
			content = []
			for s in sels:
				content.append(s.name)
			return render_to_response('report.html', {	
				'report_title' 		: title,
				'report_content' 	: '<br/>'.join(content),
			}, context_instance = RequestContext(request))

	elif reportId == 'procresults':
		try: kind = post['kind_select']
		except Exception, e:
			return HttpResponseRedirect('/youpi/reporting/')
		from django.db import connection
		import time
		cur = connection.cursor()
		s = time.time()
		q = """
		SELECT t.success, t.title, t.start_date, u.username, t.hostname, t.clusterId, t.results_output_dir
		FROM youpi_processing_task AS t, auth_user AS u, youpi_processing_kind AS k
		WHERE t.user_id = u.id
		AND t.kind_id = k.id
		AND k.name = '%s'
		ORDER BY t.start_date
		""" % kind
		cur.execute(q)
		res = cur.fetchall()
		if not res: 
			return render_to_response('report.html', {	
				'report_title' 		: title,
				'report_content' 	: "No results found.",
			}, context_instance = RequestContext(request))

		content = []
		for r in res:
			status = 'F' # Failed
			if r[0]: status = 'S'
			row = [status]
			row.extend(r[1:])
			content.append(row)

		if format != ReportFormat.HTML:
			fout = open(os.path.join(settings.MEDIA_ROOT, settings.MEDIA_TMP, fname), 'w')

			if format == ReportFormat.CSV:
				import csv
				writer = csv.writer(fout)
				for row in content:
					writer.writerow(row)
			elif format == ReportFormat.LATEX:
				from terapix.reporting.latex import LaTeXReport
				r = LaTeXReport(fout, name = title, user = request.user, landscape = True)
				r.addRawContent("Yeahhhh")
				data = []
				for row in content:
					data.append(["%s\\\\\n%s on %s (job %s)\\\\\nBy %s on %s\\\\\n%s" % (row[1], row[0], row[4], row[5], row[3], row[2], row[6])])
				r.addTable(
					header = ['Job Details'], 
					data = data,
					caption = title
				)
				r.save()
			elif format == ReportFormat.PDF:
				from terapix.reporting.latex import LaTeXReport
				r = LaTeXReport(fout, name = title, user = request.user, landscape = True)
				r.save()

			fout.close()
			return render_to_response('report.html', {	
								'report_title' 		: title,
								'report_content' 	: "<div class=\"report-download\"><a href=\"%s\">Download %s file report</a></div>" % 
									(os.path.join('/media', settings.MEDIA_TMP, fname + "?%s" % time.time()), format),
			}, context_instance = RequestContext(request))
		else:
			# HTML
			return render_to_response('report.html', {	
								'report_title' 		: title,
								'report_content' 	: '<br/>'.join([' '.join(str(r)) for r in content]),
			}, context_instance = RequestContext(request))