def delete(self): conn = Postgres.connect() cur = Postgres.execute(conn, 'delete from steps where recipe_id = %s', (self.id, )) rows = Postgres.fetchall(cur) conn.commit() Postgres.closeall(conn, cur)
def delete_by_recipe_id(recipe_id): conn = Postgres.connect() cur = Postgres.execute( conn, 'delete from recipe_ingredients where recipe_id = %s', (recipe_id, )) conn.commit() Postgres.closeall(conn, cur)
def delete_by_recipe_id_not_in(recipe_id, not_in_ids): conn = Postgres.connect() cur = Postgres.execute( conn, 'delete from steps where recipe_id = %s and id not in (' + not_in_ids + ')', (recipe_id, )) conn.commit() Postgres.closeall(conn, cur)
def find_by_id(cls, _id): conn = Postgres.connect() cur = Postgres.execute( conn, 'select id, name from ingredients where id = %s;', (_id, )) row = Postgres.fetchone(cur) ingredient = cls(*row) Postgres.closeall(conn, cur) return ingredient
def find_all(cls): conn = Postgres.connect() cur = Postgres.execute( conn, 'select id, name from ingredients order by name;', None) rows = Postgres.fetchall(cur) ingredients = [] for row in rows: ingredients.append(cls(*row)) Postgres.closeall(conn, cur) return ingredients
def find_by_recipe_id(cls, _id): conn = Postgres.connect() cur = Postgres.execute( conn, 'select id, recipe_id, name, position, unit, amount from recipe_ingredients where recipe_id = %s order by position;', (_id, )) rows = Postgres.fetchall(cur) ingedients = [] if rows: for row in rows: ingedients.append(cls(*row)) Postgres.closeall(conn, cur) return ingedients
def find_by_recipe_id(cls, _id): conn = Postgres.connect() cur = Postgres.execute( conn, 'select id, recipe_id, position, description from steps where recipe_id = %s order by position;', (_id, )) rows = Postgres.fetchall(cur) steps = [] if rows: for row in rows: steps.append(cls(*row)) Postgres.closeall(conn, cur) return steps
def main(): psql = Postgres() conn = psql.connect() status, data, log = get_data() if status == 1: upload_data(data, conn) log = pd.DataFrame([log]) log.columns = ["code", "status", "total", "datetime"] log.drop(["status", "total"], axis=1, inplace=True) log.to_sql("log", con=conn, if_exists="append", index=False, schema="news-api") conn.close()
def update(self): conn = Postgres.connect() if self.id: cur = Postgres.execute( conn, 'update ingredients set name = %s where id = %s', (self.name, self.id)) else: cur = Postgres.execute( conn, 'insert into ingredients (name) values (%s) returning id', (self.name, )) self.id = Postgres.fetchone(cur)[0] conn.commit() Postgres.closeall(conn, cur)
def update(self): conn = Postgres.connect() if self.id: cur = Postgres.execute( conn, 'update steps set description = %s, position = %s where id = %s', (self.description, self.position, self.id)) else: cur = Postgres.execute( conn, 'insert into steps (recipe_id, description, position) values (%s, %s, %s) returning id', (self.recipe_id, self.description, self.position)) self.id = Postgres.fetchone(cur)[0] conn.commit() Postgres.closeall(conn, cur)
def update(self): conn = Postgres.connect() if self.id: cur = Postgres.execute( conn, 'update recipe_ingredients set name = %s, position = %s, unit = %s, amount = %s where id = %s', (self.name, self.position, self.unit, self.amount, self.id)) else: cur = Postgres.execute( conn, 'insert into recipe_ingredients (recipe_id, name, position, unit, amount) values (%s, %s, %s, %s, %s) returning id', (self.recipe_id, self.name, self.position, self.unit, self.amount)) self.id = Postgres.fetchone(cur)[0] conn.commit() Postgres.closeall(conn, cur)
def delete(self): conn = Postgres.connect() cur = Postgres.execute(conn, 'delete from ingredients where id = %s', (self.id, )) conn.commit() Postgres.closeall(conn, cur)
def setUpClass(cls): cls.db = Postgres(DEV_PG_CONNECION) cls.db.connect()