Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- from flask import Flask, render_template, request
- from psycopg2.extras import RealDictCursor
- def connect():
- return psycopg2.connect("host=localhos")
- class Item:
- def __init__(self, id, ver, manu, make, model, date):
- self.id = id
- self.ver = ver
- self.manu = manu
- self.model = model
- self.date = date
- class ItemDatabase:
- def __init__(self, conn):
- self.conn = conn
- def push(self):
- query = """
- INSERT INTO pg_equipment(ver, manu, model) VALUES(
- %(ver)s,
- %(make)s,
- %(model)s
- )
- """
- curs = self.conn.cursor(cursor_factory=RealDictCursor)
- curs.execute(query)
- def pull(self, ver=None, manu=None, make=None, model=None):
- query = """SELECT * from pg_equipment"""
- curs = self.conn.cursor(cursor_factory=RealDictCursor)
- curs.execute(
- query,
- dict(
- ver=ver,
- manu=manu,
- model=model,
- ))
- row = curs.fetchall()
- if row:
- return Item(row)
- return None
- app = Flask(__name__)
- @app.route('/', methods=["GET", "POST"])
- def home():
- if request.form:
- conn = connect()
- item = ItemDatabase(conn).push(request.form)
- conn.commit()
- return render_template("input.html")
- @app.route('/view', methods=["GET", "POST"])
- def view():
- conn = connect()
- items = ItemDatabase(conn).pull()
- conn.commit()
- return render_template("view.html", items=items)
- if __name__ == "__main__":
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement