-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
204 lines (154 loc) · 6.74 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import json
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tables import tables, Food, Recipe, Review
class Database:
""" A class to handle all interactions with a Database. It takes an optional config_file location,
in case you want to save your config elsewhere."""
def __init__(self, config_file=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')):
self.config_file = config_file
self.grab_data()
self.get_session()
self.con = None
def grab_data(self):
""" Grabs the data from the config file and also makes a DATABSE_URI. I believe psycopg2 convention is
to have that variable name in all caps."""
with open(self.config_file, 'r') as infile:
self.data = json.load(infile)
# Scheme: "postgres+psycopg2://<USERNAME>:<PASSWORD>@<IP_ADDRESS>:<PORT>/<DATABASE_NAME>"
self.DATABASE_URI = f"postgres+psycopg2://postgres:{self.data['database_password']}" + \
f"@localhost:{self.data['port']}/{self.data['server_name']}"
def get_session(self):
""" This safely makes a sqlalchemy connection engine and a Session instance. The Session isn't fully formed,
so we can keep making new ones off the same instance."""
self.engine = create_engine(self.DATABASE_URI)
self.Session = sessionmaker(bind=self.engine)
def create_database(self, dbname):
""" This will make a new database, to help with setup."""
con = psycopg2.connect(user=self.data['database_user'],
host='',
password=self.data['database_password'])
con.autocommit = True
cur = con.cursor()
try:
cur.execute(f"""CREATE DATABASE {dbname}
WITH
OWNER = {self.data['database_user']}
ENCODING = 'UTF8'
CONNECTION LIMIT = -1;"""
)
print(f"Made {dbname} database")
except psycopg2.errors.DuplicateDatabase:
print('Duplicate Database, passing')
def create_tables(self, dbname, tables):
""" This safely makes the DB's only table, called weather_app. It takes an optional name parameter, in case we
want to further customize the emails later."""
con = psycopg2.connect(dbname=dbname,
user=self.data['database_user'],
host='',
password=self.data['database_password'])
con.autocommit = True
cur = con.cursor()
for table in tables:
try:
create = f"CREATE TABLE {table['name']} (" +\
f"{', '.join(table['columns'])});"
cur.execute(create)
print(f"Made {table['name']} table")
except psycopg2.errors.DuplicateTable:
print('Duplicate Table, passing')
def add_food(self, name):
""" Adds an email to the data base."""
session = self.Session()
food = Food(name=name)
print(self.data['database_password'])
session.add(food)
session.commit()
session.refresh(food)
session.close()
return int(food.id)
def delete_food(self, id):
session = self.Session()
query = session.query(Food).filter(Food.id == id)
query.delete()
session.commit()
def add_recipe(self, food_id, recipe_name, directions, change, notes, ingredients):
session = self.Session()
recipe = Recipe(food_id=food_id,
recipe_name=recipe_name,
directions=directions,
change=change,
notes=notes,
ingredients=ingredients)
session.add(recipe)
session.commit()
session.refresh(recipe)
session.close()
return int(recipe.recipe_id)
def delete_recipe(self, recipe_id):
session = self.Session()
query = session.query(Recipe).filter(Recipe.recipe_id == recipe_id)
query.delete()
session.commit()
def add_review(self, recipe_id, reviewer_name, taste, texture, appearance, overall, comments):
session = self.Session()
review = Review(recipe_id=recipe_id,
reviewer_name=reviewer_name,
taste=taste,
texture=texture,
appearance=appearance,
overall=overall,
comments=comments)
session.add(review)
session.commit()
session.close()
def delete_review(self, review_id):
session = self.Session()
query = session.query(Review).filter(Review.review_id == review_id)
query.delete()
session.commit()
def query_foods(self):
self.make_query_con()
cur = self.con.cursor()
cur.execute("""SELECT id, name FROM food;""")
foods = {food[1]: food[0] for food in cur.fetchall()}
return foods
def query_recipes(self, food_id):
self.make_query_con()
cur = self.con.cursor()
cur.execute(f"""SELECT recipe_name, change, recipe_id FROM recipe
WHERE food_id = {food_id}
ORDER BY recipe_id; """)
recipes = cur.fetchall()
recipes = {recipe[2]: (recipe[0], recipe[1]) for recipe in recipes}
return recipes
def query_reviews(self, recipe_id):
self.make_query_con()
cur = self.con.cursor()
cur.execute(f"""SELECT * from recipe
WHERE recipe_id = {recipe_id}; """)
recipe = cur.fetchall()
cur.execute(f"""SELECT * from reviews
WHERE recipe_id = {recipe_id}; """)
reviews = cur.fetchall()
return recipe, reviews
def make_query_con(self):
if not self.con:
self.con = psycopg2.connect(dbname='baking',
user=self.data['database_user'],
host='',
password=self.data['database_password'])
def food_name_from_id(self, food_id):
self.make_query_con()
cur = self.con.cursor()
cur.execute(f"""SELECT food from food
WHERE id = '{food_id}'; """)
foods = cur.fetchall()
food = foods[0][0].split(",")[1][1:-2]
return food
if __name__ == "__main__":
database = Database()
database.create_database(dbname='baking')
database.create_tables(dbname='baking', tables=tables)