-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py(2).py
69 lines (54 loc) · 2.01 KB
/
app.py(2).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
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:[email protected]/?retryWrites=true&w=majority')
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/reviews", methods=["POST"])
def reviews_post():
name_receive = request.form['name_give']
star_receive = request.form['star_give']
comment_receive = request.form['comment_give']
img_receive = request.form['img_give']
pw_receive = request.form['pw_give']
doc = {
'name' : name_receive,
'star' : star_receive,
'comment' : comment_receive,
'img' : img_receive,
'pw' : pw_receive
}
db.reviews.insert_one(doc)
return jsonify({'msg':'게시글 작성 완료!'})
@app.route("/reviews/update", methods=["POST"])
def modify_reviews():
name_receive = request.form['name_give']
star_receive = request.form['star_give']
comment_receive = request.form['comment_give']
img_receive = request.form['img_give']
pw_receive = request.form['pw_give']
id_receive = request.form['_id_give']
doc = {
'name' : name_receive,
'star' : star_receive,
'comment' : comment_receive,
'img' : img_receive,
'pw' : pw_receive
}
print("가나다", id_receive, doc)
db.reviews.update_one({"_id": ObjectId(id_receive)}, {"$set" : doc})
return jsonify({'msg':'게시글 수정 완료!'})
from bson.objectid import ObjectId
@app.route("/reviews", methods=["GET"])
def reviews_get():
all_reviews = list(db.reviews.find({}))
result = []
for review in all_reviews:
review['_id'] = str(ObjectId(review['_id'])) # convert the ObjectId to a string
# print(review)
result.append(review)
return jsonify({'result': result})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)