def provide_report(session, form): """Extracts report type, report parameters, and output type from the CGI form; offers up the results in the chosen format.""" # Which report? report_id = ws.get_cgi_parameter_str(form, PARAM.REPORT_ID) report = get_report_instance(report_id) if not report: return cc_html.fail_with_error_stay_logged_in("Invalid report_id") # What output type? outputtype = ws.get_cgi_parameter_str(form, PARAM.OUTPUTTYPE) if outputtype is not None: outputtype = outputtype.lower() if (outputtype != VALUE.OUTPUTTYPE_HTML and outputtype != VALUE.OUTPUTTYPE_TSV): return cc_html.fail_with_error_stay_logged_in("Unknown outputtype") # Get parameters params = get_params_from_form(report.get_param_spec_list(), form) # Get query details rows, descriptions = report.get_rows_descriptions(**params) if rows is None or descriptions is None: return cc_html.fail_with_error_stay_logged_in( "Report failed to return a list of descriptions/results") if outputtype == VALUE.OUTPUTTYPE_TSV: filename = ( "CamCOPS_" + report.get_report_id() + "_" + cc_dt.format_datetime(pls.NOW_LOCAL_TZ, DATEFORMAT.FILENAME) + ".tsv" ) return ws.tsv_result(tsv_from_query(rows, descriptions), [], filename) else: # HTML html = pls.WEBSTART + u""" {} <h1>{}</h1> """.format( session.get_current_user_html(), report.get_report_title(), ) + ws.html_table_from_query(rows, descriptions) + cc_html.WEBEND return html
def user_management_failure_message(msg, as_manager=True): """Generic failure HTML for user management.""" extra_html = "" if as_manager: extra_html = """ <div> <a href={}>Return to user menu</a> </div> """.format( cc_html.get_generic_action_url(ACTION.MANAGE_USERS) ) return cc_html.fail_with_error_stay_logged_in(msg, extra_html)
def offer_individual_report(session, form): """For a specific report (specified within the CGI form), offers an HTML page with the parameters to be configured for that report.""" # Which report? report_id = ws.get_cgi_parameter_str(form, PARAM.REPORT_ID) report = get_report_instance(report_id) if not report: return cc_html.fail_with_error_stay_logged_in("Invalid report_id") html = pls.WEBSTART + u""" {userdetails} <h1>Report: {reporttitle}</h1> <div class="filter"> <form method="GET" action="{script}"> <input type="hidden" name="{PARAM.ACTION}" value="{ACTION.PROVIDE_REPORT}"> <input type="hidden" name="{PARAM.REPORT_ID}" value="{report_id}"> """.format( userdetails=session.get_current_user_html(), reporttitle=report.get_report_title(), script=pls.SCRIPT_NAME, ACTION=ACTION, PARAM=PARAM, report_id=report_id ) for p in report.get_param_spec_list(): html += get_param_html(p) html += u""" <br> Report output format:<br> <label> <input type="radio" name="{PARAM.OUTPUTTYPE}" value="{VALUE.OUTPUTTYPE_HTML}" checked> HTML </label><br> <label> <input type="radio" name="{PARAM.OUTPUTTYPE}" value="{VALUE.OUTPUTTYPE_TSV}"> Tab-separated values (TSV) </label><br> <br> <input type="submit" value="Report"> </form> </div> """.format( PARAM=PARAM, VALUE=VALUE, ) return html + cc_html.WEBEND