Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
122 lines (104 loc) · 4.21 KB
/
Copy pathdatabase.py
File metadata and controls
122 lines (104 loc) · 4.21 KB
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
import sqlite3
class DB:
def __init__(self):
self.db = sqlite3.connect("food.db")
self.c = self.db.cursor()
def close(self):
self.db.commit()
self.c.close()
def check_log(self, username, password):
passw = self.c.execute(
"SELECT password from users where username = '{}'".format(username)).fetchone()
if passw:
passw = passw[0]
if passw == password:
return True
else:
return False
else:
return False
def add_user(self, username, password, fbid):
res = self.c.execute(
"SELECT username from users where username='{}'".format(username)).fetchone()
if not res:
self.c.execute("INSERT INTO users VALUES ({0}, '{1}','{2}')".format(
int(fbid), username, password))
return 1
else:
return 0
def add_ingredient(self, userid, ingname):
self.c.execute(
"INSERT INTO has VALUES ({0}, '{1}')".format(int(userid), ingname))
def del_ingredient(self, userid, ingname):
self.c.execute("DELETE FROM has WHERE userid={0} and ingredient='{1}'".format(
int(userid), ingname))
# def match(self, *users):
# recetas = []
# ingredients = []
# for i in users:
# userid = self.get_user_id(i)
# ingredients.append(self.c.execute(
# "SELECT has.ingredient FROM has WHERE userid={}".format(userid)).fetchall())
# group_ingredients = []
# for i in ingredients:
# for j in i:
# group_ingredients.append(j[0])
# for j in self.c.execute("SELECT * FROM recipes"):
# uses = j[3].split(",")
# filtro = list(filter(lambda x: x not in group_ingredients, uses))
# if len(filtro) == 0:
# recetas.append((j[1], "./img/recipes/{}.jpg".format(j[0])))
# return recetas
def match(self, user):
# (nombre,url,usuario_match)
friends = self.get_friends(user)
recetas = []
for i in friends:
ingredients = []
friendid = self.get_user_id(i)
userid = self.get_user_id(user)
ingredients.append(self.c.execute(
"SELECT has.ingredient FROM has WHERE userid={0} or userid={1}".format(friendid, userid)).fetchall())
group_ingredients = []
for k in ingredients:
for z in k:
group_ingredients.append(z[0])
for j in self.c.execute("SELECT * FROM recipes"):
uses = j[3].split(",")
filtro = list(
filter(lambda x: x not in group_ingredients, uses))
if len(filtro) == 0:
recetas.append(
(j[1], "img/recipes/{}.jpg".format(j[0]), i))
return recetas
def get_user_id(self, username):
user_id = self.c.execute(
"SELECT userid from users where username = '{}'".format(username)).fetchone()[0]
return int(user_id)
def get_user_ingredients(self, username):
ingredients = self.c.execute(
"SELECT ingredient, ingid from has,ingredients where userid = {} and ingredient = name".format(self.get_user_id(username))).fetchall()
ing = [(i[0], i[1]) for i in ingredients]
return ing
def get_ingredientes(self):
return self.c.execute("SELECT * FROM ingredients").fetchall()
def get_recipes(self):
return self.c.execute("SELECT recid,name from recipes")
def cook_recipe(self, username, rec_name):
mis_ing = [i[0] for i in self.get_user_ingredients(username)]
rec_ing = self.c.execute(
"SELECT ingredients from recipes where name='{}'".format(rec_name)).fetchone()[0]
rec_ing = rec_ing.split(",")
return mis_ing
def get_friends(self, username):
if username == "tamy":
return ["pipe", "benja"]
elif username == "pipe":
return ["tamy", "benja"]
else:
return ["tamy", "pipe"]
def get_pic(self, user):
return "img/users/{}.jpg".format(user)
# db = DB()
# print(db.match("tamy"))
# db.close()