-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
134 lines (102 loc) · 3.72 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
from flask import Flask, render_template, jsonify, request
from bson.objectid import ObjectId
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient('mongodb://test:[email protected]', 27017)
db = client.animalLost
# HTML 화면 보여주기
@app.route('/')
def home():
return render_template('index.html')
# nav1-content
# API 역할을 하는 부분
@app.route('/center', methods=['GET'])
def view_center():
bohocenter = list(db.bohocenter.find({}, {'_id': False}))
return jsonify({'bohocenters': bohocenter})
# nav2-content
# API 역할을 하는 부분
@app.route('/dogs', methods=['GET'])
def view_abdogs():
dogs_notice = list(db.dogs.find({"SPECIES_NM": {'$regex': '개'}}, {'_id': False}).limit(30))
return jsonify({'dogs_notices': dogs_notice})
# nav3-content
@app.route('/blog', methods=['GET'])
def read_blogs():
blogs = list(db.blogs.find({}, {'_id': False}))
return jsonify({'blog_texts': blogs})
# nav4-content
# 적재된 후기를 DB에서 불러오는 부분
@app.route('/review', methods=['GET'])
def getReview():
data = list(db.reviews.find({}))
resData = []
for d in data:
idx = d['_id']
nickname = d['nickname']
title = d['title']
comment = d['comment']
password = d['password']
dset = {
'id': str(idx),
'nickname': nickname,
'title': title,
'comment': comment,
'password': password,
}
resData.append(dset)
return jsonify({'all_reviews': resData})
# 후기작성 후 등록버튼 클릭시 DB에 정보저장하는 부분
@app.route('/review', methods=['POST'])
def saving():
nickname_receive = request.form['nickname_give']
title_receive = request.form['title_give']
comment_receive = request.form['comment_give']
password_receive = request.form['password_give']
doc = {
'nickname': nickname_receive,
'title': title_receive,
'comment': comment_receive,
'password': password_receive
}
db.reviews.insert_one(doc)
return jsonify({'msg': '후기가 등록완료되었습니다.'})
# 후기 수정, 삭제 버튼 클릭 시 DB저장된 ID와 입력한 아이디가 같으면 update, delete 하는 부분
@app.route('/update', methods=['POST'])
def update():
print("수정진입")
id = request.form['id_give']
reviseContent = request.form['reviseContent_give']
confirmPassword = request.form['confirmPassword_give']
print(id)
print(confirmPassword)
print(reviseContent)
data = db.reviews.find_one({'_id': ObjectId(id)})
currentPassword = data['password']
originContent = data['comment']
print(data)
print(currentPassword)
print(originContent)
if currentPassword != confirmPassword:
return jsonify({'msg': '비밀번호가 일치하지 않습니다. 다시 입력해주세요!'})
else:
db.reviews.update_one({'_id': ObjectId(id)}, {'$set': {'comment': reviseContent} })
return jsonify({'msg': '수정되었습니다.'})
@app.route('/delete', methods=['POST'])
def delete():
print("삭제진입")
id = request.form['id_give']
confirmPassword = request.form['confirmPassword_give']
print(id)
print(confirmPassword)
data = db.reviews.find_one({'_id': ObjectId(id)})
currentPassword = data['password']
print(data)
print(currentPassword)
if currentPassword != confirmPassword:
return jsonify({'msg': '비밀번호가 일치하지 않습니다. 다시 입력해주세요!'})
else:
db.reviews.delete_one({'_id': ObjectId(id)})
return jsonify({'msg': '삭제되었습니다.'})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True, use_reloader=False)