def application_show_environment_test_database(environ, start_response): linebreak = "=" * 79 + "\n" status = "200 OK" if not environ["mod_wsgi.process_group"]: output = "mod_wsgi EMBEDDED MODE" else: output = "mod_wsgi DAEMON MODE" output += "\n\nenviron parameter:\n" + linebreak for (k, v) in sorted(environ.iteritems()): output += str(k) + ": " + str(v) + "\n" output += "\nos.environ:\n" + linebreak for (k, v) in sorted(os.environ.iteritems()): output += str(k) + ": " + str(v) + "\n" output += "\nCGI form:\n" + linebreak form = ws.get_cgi_fieldstorage_from_wsgi_env(environ) for k in form.keys(): output += "{0} = {1}\n".format(k, form.getvalue(k)) output += "\nCGI value for 'test' field:\n" + linebreak output += "{}\n".format(ws.get_cgi_parameter_str(form, "test")) output += "\nCGI value for 'Test' field:\n" + linebreak output += "{}\n".format(ws.get_cgi_parameter_str(form, "Test")) # This successfully writes to the Apache log: sys.stderr.write("testwsgi.py: STARTING\n") # Let's not bother with a persistent database connection for now. # http://stackoverflow.com/questions/405352/mysql-connection-pooling-question-is-it-worth-it # noqa test_database = False if test_database: db = connect_to_database(environ) output += "\nCONNECTED TO DATABASE\n" + linebreak output += ("Count: " + str(db.fetchvalue("SELECT COUNT(*) FROM expdetthreshold")) + "\n") # Final output response_headers = [ ("Content-type", "text/plain"), ("Content-Length", str(len(output))), ] start_response(status, response_headers) return [output]
def application_show_environment_test_database(environ, start_response): linebreak = "=" * 79 + "\n" status = '200 OK' if not environ['mod_wsgi.process_group']: output = 'mod_wsgi EMBEDDED MODE' else: output = 'mod_wsgi DAEMON MODE' output += "\n\nenviron parameter:\n" + linebreak for (k, v) in sorted(environ.iteritems()): output += str(k) + ": " + str(v) + "\n" output += "\nos.environ:\n" + linebreak for (k, v) in sorted(os.environ.iteritems()): output += str(k) + ": " + str(v) + "\n" output += "\nCGI form:\n" + linebreak form = ws.get_cgi_fieldstorage_from_wsgi_env(environ) for k in form.keys(): output += "{0} = {1}\n".format(k, form.getvalue(k)) output += "\nCGI value for 'test' field:\n" + linebreak output += "{}\n".format(ws.get_cgi_parameter_str(form, "test")) output += "\nCGI value for 'Test' field:\n" + linebreak output += "{}\n".format(ws.get_cgi_parameter_str(form, "Test")) # This successfully writes to the Apache log: sys.stderr.write("testwsgi.py: STARTING\n") # Let's not bother with a persistent database connection for now. # http://stackoverflow.com/questions/405352/mysql-connection-pooling-question-is-it-worth-it # noqa test_database = False if test_database: db = connect_to_database(environ) output += "\nCONNECTED TO DATABASE\n" + linebreak output += ( "Count: " + str(db.fetchvalue("SELECT COUNT(*) FROM expdetthreshold")) + "\n" ) # Final output response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
def provide_report(session: SESSION_FWD_REF, form: cgi.FieldStorage) \ -> Union[str, WSGI_TUPLE_TYPE]: """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.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.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 + """ {} <h1>{}</h1> """.format( session.get_current_user_html(), report.report_title, ) + ws.html_table_from_query(rows, descriptions) + WEBEND return html
def offer_individual_report(session: SESSION_FWD_REF, form: cgi.FieldStorage) -> str: """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 + """ {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.report_title, script=pls.SCRIPT_NAME, ACTION=ACTION, PARAM=PARAM, report_id=report_id ) for p in report.param_spec_list: html += get_param_html(p) html += """ <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 + WEBEND