forked from sparta-github-group6/show_me_the_recipe-mc6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
386 lines (286 loc) · 10.1 KB
/
app.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from flask import (
Flask,
render_template,
jsonify,
request,
session,
escape,
redirect,
url_for,
)
from pymongo import MongoClient
from datetime import datetime
import time
app = Flask(__name__)
# 서버 열 때 활성화
client = MongoClient("mongodb://test:test@localhost", 27017)
# 로컬에서 작업할 때 활성화
# client = MongoClient("localhost", 27017)
db = client.dbmaking
app.secret_key = "ABCDEFG"
# HTML 화면 표시
@app.route("/")
def init():
return render_template("index.html")
# 회원가입 페이지
@app.route("/register")
def sign_up():
return render_template("register.html")
# 레시피 추가 페이지
@app.route("/addrecipe")
def add_recipe():
return render_template("add_recipe.html")
# 로그인 페이지
@app.route("/login")
def sign_in():
return render_template("login.html")
# 멤버 소개 페이지
@app.route("/about")
def about_page():
return render_template("about.html")
# 전체 요리 레시피 순위 페이지
@app.route("/rank")
def rank_page():
return render_template("rank.html")
# 요리 레시피 리스트 요청
@app.route("/recommend")
def recommend_page():
return render_template("recommend.html")
# 요리 레시피 상세페이지
@app.route("/recipe")
def recipe_page():
return render_template("recipe.html")
# 요리 레시피 추가
@app.route("/request")
def recipe_add_page():
return render_template("request.html")
# 마이페이지
@app.route("/my")
def my_page():
return render_template("mypage.html")
# 랭크 페이지 좋아요순 정렬 API
@app.route("/rank/list", methods=["GET"])
def show_rank():
recipe_list = list(db.recipes.find({}, {"_id": False}).sort("like", -1))
return jsonify({"recipe_Lists": recipe_list})
# 랭크 페이지 가나다순 정렬 API
@app.route("/rank/sort", methods=["GET"])
def show_sort():
sort_list = list(db.recipes.find({}, {"_id": False}).sort("name", 1))
return jsonify({"sort_lists": sort_list})
# 검색 재료 선택 도우미 API
@app.route("/recommend/search", methods=["POST"])
def search_1to2():
ingredients = request.form
ingredients = ingredients.getlist("ing_list")
db.search.update_one({"name": "검색"}, {"$set": {"index": ingredients}})
return jsonify({"msg": "저장"})
# 재료로 레시피 검색 API
@app.route("/recommend/read", methods=["GET"])
def search():
try:
search = db.search.find_one({"name": "검색"})
# 좋아요 있는 경우
# if :
# 좋아요 없는 경우
# else:
recipes = list(
db.recipes.find({"search": {"$all": search["index"]}}, {"_id": False}).sort(
"like", -1
)
)
except Exception:
return jsonify({"msg": "없는 재료 입니다."})
if search is None:
return jsonify({"msg": "없는 재료 입니다."})
# recipes = list(db.recipes.find({'search':ingredients},{'_id':False}))
# recipes = list(db.recipes.find({'search':search['index']},{'_id':False}))
return jsonify({"recipes": recipes})
@app.route("/recommend/user", methods=["GET"])
def user_favorite():
try:
search = db.users.find_one({"user_id": session["user_id"]})
search = search["favorite"]
except Exception:
fav = []
return jsonify({"favorite": fav})
return jsonify({"favorite": search})
@app.route("/recommend/ingredient", methods=["GET"])
def search_ing():
search = db.search.find_one({"name": "검색"}, {"_id": False})
return jsonify({"ing": search})
@app.route("/recommend/search2", methods=["POST"])
def search_2to3():
name = request.form["name_give"]
db.search.update_one({"name": "검색2"}, {"$set": {"index": name}})
return jsonify({"msg": "저장"})
@app.route("/recipe/read", methods=["GET"])
def search2():
search = db.search.find_one({"name": "검색2"})
# recipes = list(db.recipes.find({'search': {'$all':ingredients}},{'_id':False}))
# recipes = list(db.recipes.find({'search':ingredients},{'_id':False}))
recipe = db.recipes.find_one({"name": search["index"]}, {"_id": False})
return jsonify({"recipes": recipe})
# 요리 레시피 요청
# @app.route('/recipe', methods=['GET'])
# def show_recipe():
# sample_receive = request.args.get('sample_give')
# print(sample_receive)
# return jsonify({'msg': 'list 연결되었습니다!'})
# ingredients = ['계란','물','소금']
# test = list(db.recipes.find({'search': {'$all':ingredients}},{'_id':False}))
# print(test,len(test))
# 좋아요
@app.route("/recipe/like", methods=["POST"])
def like_star():
name_receive = request.form["name_give"]
target_star = db.recipes.find_one({"name": name_receive})
current_like = target_star["like"]
new_like = current_like + 1
db.recipes.update_one({"name": name_receive}, {"$set": {"like": new_like}})
return jsonify({"msg": "좋아요!"})
# 싫어요
@app.route("/recipe/hate", methods=["POST"])
def hate_star():
name_receive = request.form["name_give"]
target_star = db.recipes.find_one({"name": name_receive})
current_like = target_star["like"]
hate_like = current_like - 1
db.recipes.update_one({"name": name_receive}, {"$set": {"like": hate_like}})
return jsonify({"msg": "싫어요!"})
# 로그인
@app.route("/login/check", methods=["POST"])
def login():
userid_receive = request.form["userid_give"]
userpw_receive = request.form["userpw_give"]
try:
target = db.users.find_one(
{"user_id": userid_receive, "user_pw": userpw_receive}, {"_id": False}
)
except Exception as e:
return {"message": "failed to login"}, 401
if target is not None:
session["user_id"] = target.get("user_id")
return {"user_data": target}, 200
# 로그아웃
@app.route("/logout", methods=["GET"])
def logout():
session.pop("user_id", None)
return redirect(url_for("init"))
# 회원가입
@app.route("/register/add", methods=["POST"])
def register():
userid_receive = request.form["userid_give"]
userpw_receive = request.form["userpw_give"]
usermail_receive = request.form["usermail_give"]
favorite = []
doc = {
"user_id": userid_receive,
"user_pw": userpw_receive,
"user_mail": usermail_receive,
"favorite": favorite,
}
db.users.insert_one(doc)
return jsonify({"msg": "가입완료"})
# ID 중복 검사
@app.route("/register/id_check", methods=["POST"])
def id_check():
userid_receive = request.form["userid_give"]
try:
target = db.users.find_one({"user_id": userid_receive}, {"_id": False})
except Exception as e:
return {"message": "failed to find"}, 401
if target is None:
msg = "pass"
else:
msg = "fail"
return jsonify({"result": msg})
# 즐겨찾기
@app.route("/favorite", methods=["POST"])
def favorite():
try:
recipe_receive = request.form["recipe_give"]
favorite = db.users.find_one({"user_id": session["user_id"]}, {"_id": False})[
"favorite"
]
if recipe_receive in favorite:
db.users.update_one(
{"user_id": session["user_id"]}, {"$pull": {"favorite": recipe_receive}}
)
return jsonify({"msg": "삭제 완료"})
else:
db.users.update_one(
{"user_id": session["user_id"]}, {"$push": {"favorite": recipe_receive}}
)
return jsonify({"msg": "추가 완료"})
except Exception:
return jsonify({"msg": "로그인이 필요합니다."})
# 마이페이지에서 즐겨찾기 삭제
@app.route("/favorite/delete", methods=["POST"])
def delete():
try:
recipe_receive = request.form["recipe_give"]
db.users.update_one(
{"user_id": session["user_id"]}, {"$pull": {"favorite": recipe_receive}}
)
except Exception:
return jsonify({"msg": "로그인이 필요합니다."})
return jsonify({"msg": "삭제 완료"})
# 마이페이지
@app.route("/my/user", methods=["GET"])
def call_user():
try:
target = db.users.find_one({"user_id": session["user_id"]}, {"_id": False})
except Exception as e:
return {"message": "failed to search"}, 401
# if target is not None:
# session["user_id"] = target.get("user_id")
return {"user_data": target}, 200
# 리뷰 등록
@app.route("/review", methods=["POST"])
def add_review():
try:
recipe_receive = request.form["recipe_give"]
comment_receive = request.form["comment_give"]
if session["user_id"] is None:
return jsonify({"msg": "로그인이 필요합니다."})
else:
doc = {
"user_id": session["user_id"],
"name": recipe_receive,
"comment": comment_receive,
"datetime": time.strftime("%Y-%m-%d %H:%M"),
}
db.reviews.insert_one(doc)
except Exception:
return jsonify({"msg": "로그인이 필요합니다."})
return jsonify({"user_id": session["user_id"]})
@app.route("/review/show", methods=["POST"])
def read_reviews():
name_receive = request.form["name_give"]
reviews = list(db.reviews.find({"name": name_receive}, {"_id": False}))
return jsonify({"all_reviews": reviews})
# 레시피 추가 요청
@app.route("/request", methods=["POST"])
def add_request():
try:
request_receive = request.form["request_give"]
if session["user_id"] is None:
return jsonify({"msg": "로그인이 필요합니다."})
else:
doc = {
"user_id": session["user_id"],
"request": request_receive,
"datetime": time.strftime("%Y-%m-%d %H:%M"),
}
db.requests.insert_one(doc)
except Exception:
return jsonify({"msg": "로그인이 필요합니다."})
return jsonify({"user_id": session["user_id"]})
@app.route("/request/show", methods=["GET"])
def read_request():
requests = list(db.requests.find({}, {"_id": False}))
return jsonify({"all_requests": requests})
# 추천 요리 표시
if __name__ == "__main__":
app.run("0.0.0.0", port=5000, debug=True)