def do_GET(self): if self.path == "/meta": # TODO: Same code as pyweb.py. Only page needs a change conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) # returns dbmeta 'Meta' conn.close() graph_code = pgmeta.to_graph(meta) src_code = """ <html> <body> <script src="view/dist/main.js"></script> <canvas id='target-canvas'></canvas> <script> var canvas = document.getElementById('target-canvas'); var source = `{}`; nomnoml.draw(canvas, source); </script> </body> </html> """ self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(src_code.format(graph_code).encode()) # self.wfile.write(src_code.format(graph_code).encode()) # self.wfile.write(page.src_code.format(graph_code).encode()) else: SimpleHTTPRequestHandler.do_GET(self)
def do_GET(self): if self.path == "/meta": # TODO: Same code as pyweb.py. Only page needs a change conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() graph_code = pgmeta.to_graph(meta) src_code = """ <html> <body> <script src="view/dist/main.js"></script> <canvas id='target-canvas'></canvas> <script> var canvas = document.getElementById('target-canvas'); var source = `{}`; nomnoml.draw(canvas, source); </script> </body> </html> """ self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(src_code.format(graph_code).encode()) # self.wfile.write(src_code.format(graph_code).encode()) # self.wfile.write(page.src_code.format(graph_code).encode()) elif self.path == "/add": conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() table_options = "" for key in meta.tables: table_options += '<option value="{0}">{1}</option>\n'.format( key, key) page1 = page.replace("{{table_dropdown_options}}", table_options) page1 = page1.replace("{{insert_form}}", "") self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() print(page1) # output += "<input name='newRestaurantName' type='text' placeholder='New Restaurant Name'> " # output += "<input type='submit' value='Add Restaurant'>" # output += "</form></html></body>" self.wfile.write(page1.encode())
def do_POST(self): if self.path == "/add": conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(page.format(repr(s)).encode()) SimpleHTTPRequestHandler.do_GET(self)
def do_GET(self): if self.path == "/meta": conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() s = pgmeta.to_graph(meta) self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(page.format(repr(s)).encode()) else: SimpleHTTPRequestHandler.do_GET(self) # Fallback to built-in request handler
def do_GET(self): if self.path == "/meta": conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() s = pgmeta.to_graph(meta) # TODO: Use pgmeta.to_graph() (in pgmeta.py) to get a nomnoml graph in string form. # TODO: Embed this string in the 'page' above. # TODO: Send this page using the following methods of BaseHTTPRequestHandler # self.send_response, self.send_header, self.end_headers, self.wfile.write # See: https://docs.python.org/2/library/basehttpserver.html self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(page.format(s).encode()) else: SimpleHTTPRequestHandler.do_GET( self) # Fallback to built-in request handler
def check_record_in_db(): try: err = None points = 0 conn = dbexec.connect() (header, rows) = dbexec.exec_query(conn, "select count(*) from student where id = '77777'") if len(rows) == 1 and rows[0][0] == 1: c = conn.cursor() c.execute("delete from student where id = '77777'") c.close() points = 5 else: err = "Could not find student id '77777' in the database\n" + str(rows) conn.commit() conn.close() return (points, err) except Exception as e: # Unrecoverable error. traceback.print_exc() conn.close() sys.exit(1)
def do_POST(self): try: if self.path == "/add": # print(self.rfile) print("hi 1") # console.log("hi") # ctype, pdict = cgi.parse_header(self.headers['content-type']) # print("hi 2") # pdict['boundary'] = bytes(pdict['boundary'], "utf-8") # print("hi 3") # if ctype == 'multipart/form-data': # print("hi 4") form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'}) print("test") print(form.keys()) if ("table_select" in form.keys()): # print("Entered 1") selected_table = form.getvalue("table_select") conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() table_options = "" for key in meta.tables: if key == selected_table: table_options += '<option value="{0}" selected>{1}</option>\n'.format( key, key) else: table_options += '<option value="{0}">{1}</option>\n'.format( key, key) page1 = page.replace("{{table_dropdown_options}}", table_options) table = meta.tables[selected_table] insert_form_html = '<form action="/add" method="post">\n <div>{0}:</div>\n <input type="hidden" name="table_name" value="{1}">\n'.format( table.name, table.name) for column in table.columns: insert_form_html += '<div class="row">\n <div class="left">{0}</div>\n <div class="right">\n <input type="text" name="{1}">\n </div>\n </div>\n'.format( column.name, column.name) insert_form_html += '<div class="row">\n <div class="left"></div>\n <div class="right">\n <input type="submit" value="Add">\n </div>\n </div> </form>\n' page1 = page1.replace("{{insert_form}}", insert_form_html) # print(form.keys()) else: # print(form.keys()) # print("Len of form : ",len(form)) # selected_table = form.getvalue("table_select") selected_table = form.getvalue('table_name') conn = dbexec.connect() meta = pgmeta.get_meta_data(conn) conn.close() table_options = "" for key in meta.tables: if key == selected_table: table_options += '<option value="{0}" selected>{1}</option>\n'.format( key, key) else: table_options += '<option value="{0}">{1}</option>\n'.format( key, key) page1 = page.replace("{{table_dropdown_options}}", table_options) table = meta.tables[selected_table] l = form.keys() l.remove('table_name') conn = dbexec.connect() query = "insert into {0} ({1}) values ('{2}');".format( selected_table, ",".join(l), "','".join([form.getvalue(key) for key in l])) print(query) c = conn.cursor() try: c.execute(query) conn.commit() insert_form_html = '<form action="/add" method="post">\n <div>{0}:</div>\n <input type="hidden" name="table_name" value="{1}">\n'.format( table.name, table.name) for column in table.columns: insert_form_html += '<div class="row">\n <div class="left">{0}</div>\n <div class="right">\n <input type="text" name="{1}">\n </div>\n </div>\n'.format( column.name, column.name) insert_form_html += '<div class="row">\n <div class="left"></div>\n <div class="right">\n <input type="submit" value="Add">\n </div>\n </div> </form>\n' page1 = page1.replace("{{insert_form}}", insert_form_html) except Exception as e: insert_form_html = '<form action="/add" method="post">\n <div>{0}:</div>\n <input type="hidden" name="table_name" value="{1}">\n'.format( table.name, table.name) for column in table.columns: insert_form_html += '<div class="row">\n <div class="left">{0}</div>\n <div class="right">\n <input type="text" name="{1}" value="{2}">\n </div>\n </div>\n'.format( column.name, column.name, form.getvalue(column.name)) insert_form_html += '<div class="row">\n <div class="left"></div>\n <div class="right">\n <input type="submit" value="Add">\n </div>\n </div> </form>\n' page1 = page1.replace("{{insert_form}}", insert_form_html) page1 = page1.replace( '<div class="error"></div>', '<div class="error">{}</div>'.format("ERROR: " + str(e))) conn.close() self.send_response(200) self.send_header('Content-type', 'text/html') # self.send_header('Location', '/restaurant') self.end_headers() self.wfile.write(page1.encode()) except: print("Inside the exception block")
c = conn.cursor() # TODO : query referential_constraints table, which maps constraint in one table # : to unique constraint in another table # TODO : for each row create meta.tables[from_table].refersTo += meta.tables[to_table] query = """select constraint_name, unique_constraint_name from information_schema.referential_constraints ;""" (header, rows) = dbexec.exec_query(conn, query) for row in rows: meta.tables[constraints[row[0]]].refersTo.append( meta.tables[constraints[row[1]]]) c.close() def to_graph(meta): str = "" for tbl in meta.tables.values(): str += "[" + tbl.name + "|" str += "|".join([col.name for col in tbl.columns]) str += "]\n" for t in tbl.refersTo: str += "[%s] -> [%s]\n" % (tbl.name, t.name) return str if __name__ == "__main__": import config conn = dbexec.connect() meta = get_meta_data(conn) # returns dbmeta 'Meta' conn.close() print(to_graph(meta))