-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
428 lines (302 loc) · 15 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import certifi # mongodb 인증 라이브러리
import jwt
import datetime
import random
import hashlib
from flask import Flask, render_template, jsonify, request, redirect, url_for
from werkzeug.utils import secure_filename
from datetime import datetime, timedelta
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.campProject
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config['UPLOAD_FOLDER'] = "./static/profile_pics"
SECRET_KEY = 'SPARTA'
######################################## 페이지 이동 ########################################
# 메인 페이지
@app.route('/')
def home():
token_receive = request.cookies.get('mytoken') # 현재 토큰 정보(로그인한 유저의 토큰)
contents = list(db.citista_contents.find()) # 전체 컨텐츠 데이터
contents.reverse()
users = list(db.citista_users.find())# 전체 유저 데이터
print(db.citista_users.find_one({'username': 'tashahan'}))
user = db.citista_users.find_one({'token': token_receive})
if user in users:
random_s = users.remove(user)
else:
random_s = users
all3_choice = random.sample(random_s, 4)
r_user1 = all3_choice[0]
r_user2 = all3_choice[1]
r_user3 = all3_choice[2]
r_user4 = all3_choice[3]
try:
my_info = db.citista_users.find_one({'token': token_receive}) # 로그인 유저 정보
return render_template('index.html', contents=contents, users=users, my_info=my_info, r_user1=r_user1, r_user2=r_user2, r_user3=r_user3, r_user4=r_user4)
except jwt.ExpiredSignatureError:
return redirect(url_for("login", msg="로그인 시간이 만료되었습니다."))
except jwt.exceptions.DecodeError:
return redirect(url_for("login", msg="로그인 정보가 존재하지 않습니다."))
# 개인 페이지
@app.route('/profile_page')
def profile_page():
user_id = request.args.get('user_id')
token_receive = request.cookies.get('mytoken') # 현재 토큰 정보(로그인한 유저의 토큰)
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 로그인 유저 아이디
contents = list(db.citista_contents.find({'user_id': user_id}, {'_id': False})) # 지정 유저의 컨텐츠 정보
follower_count = len(list(db.citista_follows.find({'follower_id': user_id}, {'_id': False}))) #유저의 팔로워 개수
following_count = len(list(db.citista_follows.find({'following_id': user_id}, {'_id': False}))) # 유저의 팔로잉 개수
contents_count = len(list(db.citista_contents.find({'user_id': user_id}, {'_id': False}))) # 유저의 게시물 개수
doc = {"follower_count":follower_count, "following_count":following_count, "contents_count":contents_count}
return render_template('profile_page.html', user_id=user_id, my_id=my_id, contents=contents, doc=doc)
# 회원가입 페이지
@app.route('/sign_up_page')
def sign_up_page():
return render_template('sign_up.html')
# 로그인 페이지
@app.route('/login_page')
def login_page():
msg = request.args.get("msg")
return render_template('login.html')
# 로그인 페이지(로그인 세션 만료용)
@app.route('/login')
def login():
msg = request.args.get("msg")
return render_template('login.html', msg=msg)
# dm 페이지
@app.route('/dm_page')
def dm_page():
return render_template('dm.html')
# DB 자료 응답 (화면 구현용)
@app.route("/get_data", methods=["GET"])
def get_data():
contents = list(db.citista_contents.find({}, {'_id': False}))
return jsonify({'contents': contents})
######################################## 회원가입 ########################################
# 아이디 중복 확인
@app.route('/sign_up/check_dup', methods=['POST'])
def check_dup():
username_receive = request.form['username_give']
exists_id = bool(db.citista_users.find_one({"username": username_receive}))
return jsonify({'result': 'success', 'exists': exists_id})
# 닉네임 중복 확인
@app.route('/sign_up/check_nickname_dup', methods=['POST'])
def check_nickname_dup():
nickname_receive = request.form['nickname_give']
exists_nickname = bool(db.citista_users.find_one({"nickname": nickname_receive}))
return jsonify({'result': 'success', 'exists': exists_nickname})
# 회원가입
@app.route('/sign_up/save', methods=['POST'])
def sign_up():
username_receive = request.form['username_give']
password_receive = request.form['password_give']
nickname_receive = request.form['nickname_give']
password_hash = hashlib.sha256(password_receive.encode('utf-8')).hexdigest()
doc = {
"username": username_receive, # 아이디
"password": password_hash, # 비밀번호
"nickname": nickname_receive, # 프로필 이름 기본값은 아이디
"profile_pic": "p_image.png", # 프로필 사진 파일 이름
"profile_pic_real": "profile_pics/profile_placeholder.png", # 프로필 사진 기본 이미지
"profile_info": "", # 프로필 한 마디
"token": 0 # 로그인 시 토큰 임시 저장
}
db.citista_users.insert_one(doc)
return jsonify({'result': 'success'})
######################################## 로그인 / 로그아웃 ########################################
# 로그인
@app.route('/sign_in', methods=['POST'])
def sign_in():
username_receive = request.form['username_give']
password_receive = request.form['password_give']
pw_hash = hashlib.sha256(password_receive.encode('utf-8')).hexdigest() # 패스워드 암호화
result = db.citista_users.find_one({'username': username_receive, 'password': pw_hash})
# 로그인 정보 일치
if result is not None:
payload = {
'id': username_receive,
'exp': datetime.utcnow() + timedelta(seconds=60 * 60 * 24) # 로그인 24시간 유지
}
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256') # 토큰 생성
db.citista_users.update_one({'username': username_receive}, {'$set': {'token': token}}) # 유저 정보에 토큰 저장
return jsonify({'result': 'success', 'token': token})
# 로그인 정보 불일치
else:
return jsonify({'result': 'fail', 'msg': '아이디/비밀번호가 일치하지 않습니다.'})
# 로그아웃
@app.route('/logout', methods=['POST'])
def log_out():
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
db.citista_users.update_one({'username': my_id}, {'$set': {'token': 0}}) # 유저 정보 토큰 리셋(= 0)
return jsonify({'result': 'success', 'msg': '로그아웃 완료'})
######################################## 게시물 생성/구현 ########################################
# 게시물 생성
@app.route("/content", methods=["POST"])
def content_post():
desc_receive = request.form['desc_give'] # 입력된 소개글
file = request.files['file_give'] # 업로드한 이미지 파일
image_receive = request.form['image_give'] # 업로드한 이미지명
ext = image_receive.split('.')[-1] # 확장자 추출
current_time = datetime.now()
filename = f"{current_time.strftime('%Y%m%d%H%M%S')}.{ext}"
save_to = f'static/img/post_contents/{filename}' # 경로지정
file.save(save_to) # 이미지 파일 저장
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
profile_pic = user['profile_pic'] # 현재 로그인 유저 프로필 이미지
content_count = db.citista_contents.find({}, {'_id': False}).collection.estimated_document_count() # 전체 게시물 개수
doc = {
'user_id': my_id,
'post_id': content_count + 1,
'img': image_receive,
'f_name': filename,
'desc': desc_receive,
'timestamp': current_time,
'profile_pic': profile_pic
}
db.citista_contents.insert_one(doc)
return jsonify({'msg': '게시물 생성 완료'})
# 게시물 타임스탬프
@app.route("/timestamp", methods=["GET"])
def timestamp_get():
contents = list(db.citista_contents.find({}, {'_id': False}))
timestamps = []
for i in range(len(contents)):
result = datetime.now() - (contents)[i]['timestamp']
if 'day' in str(result):
time = (str(result).split('d')[0] + '일 전')
elif int(str(result).split(':')[0]) > 0:
time = (str(result).split(':')[0] + '시간 전')
else:
time = (str(result).split(':')[1] + '분 전')
timestamps.append({'post_id': i + 1, 'time': time})
return jsonify({'timestamps': timestamps})
######################################## 댓글 생성/구현 ########################################
# 댓글 생성
@app.route("/comment", methods=["POST"])
def comment_post():
post_receive = request.form['post_give'] # 해당 게시물 번호
comment_receive = request.form['comment_give'] # 입력한 코멘트
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
comment_count = db.citista_comments.find({}, {'_id': False}).collection.estimated_document_count() # 전체 코멘트 개수
doc = {
'comment_id': comment_count + 1,
'user_id': my_id,
'post_id': post_receive,
'comment': comment_receive
}
db.citista_comments.insert_one(doc)
return jsonify({'msg': '댓글 작성 완료'})
# 댓글 구현
@app.route("/comment", methods=["GET"])
def comment_get():
comments = list(db.citista_comments.find({}, {'_id': False}))
return jsonify({'comments': comments})
######################################## 좋아요 처리/구현 ########################################
# 좋아요 하기
@app.route("/like", methods=["POST"])
def like_up():
post_receive = int(request.form['post_give']) # 해당 게시물 번호
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
like_count = db.citista_likes.find({}, {'_id': False}).collection.estimated_document_count() # 전체 좋아요 개수
doc = {
'like_id': like_count+1,
'user_id': my_id,
'post_id': post_receive,
}
db.citista_likes.insert_one(doc)
return jsonify({'msg': '좋아요 감사합니다.'})
# 좋아요 취소
@app.route("/like_cancel", methods=["POST"])
def like_cancel():
post_receive = int(request.form['post_give']) # 해당 게시물 번호
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
db.citista_likes.delete_one({'user_id': my_id, 'post_id': post_receive})
return jsonify({'msg': '좋아요 취소.'})
# 좋아요 개수/표시 구현
@app.route("/like", methods=["GET"])
def like_get():
likes = list(db.citista_likes.find({}, {'_id': False})) # 전체 좋아요 정보(개수 확인용)
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디(표시 확인용)
return jsonify({'likes': likes, 'user_login': my_id})
######################################## 팔로우 올리기/구현 ########################################
# 팔로우 하기
@app.route("/follow", methods=["POST"])
def follow():
user_receive = request.form['user_give'] # 해당 페이지의 유저 아이디
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
follow_count = db.citista_follows.find({}, {'_id': False}).collection.estimated_document_count() # 전체 좋아요 개수
doc = {
'follow_id': follow_count+1,
'following_id': my_id,
'follower_id': user_receive
}
db.citista_follows.insert_one(doc)
return jsonify({'msg': '팔로우'})
# 팔로우 취소
@app.route("/follow_cancel", methods=["POST"])
def follow_cancel():
user_receive = request.form['user_give'] # 해당 페이지의 유저 아이디
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
db.citista_follows.delete_one({'following_id': my_id, 'follower_id': user_receive})
return jsonify({'msg': '팔로우'})
# 좋아요 개수/표시 구현
@app.route("/follow", methods=["GET"])
def follow_get():
follows = list(db.citista_follows.find({}, {'_id': False})) # 전체 팔로우 정보
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username'] # 현재 로그인 유저 아이디
return jsonify({'follows': follows, 'user_login': my_id})
######################################## 프로필 편집 ########################################
# 프로필 편집 (이미지 저장)
@app.route("/uploader", methods=["POST"])
def uploader_file():
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
my_id = user['username']
if request.method == 'POST':
f = request.files['file']
ext = f.filename.split('.')[-1]
save_to = f'static/img/profiles/{my_id}.{ext}'
f.save(save_to)
doc = {'profile_pic': f'{my_id}.{ext}', 'profile_pic_real': save_to}
db.citista_users.update_one({'token': token_receive}, {'$set': doc})
return redirect(url_for("profile_page"))
# 프로필 편집 (닉네임 or 소개글)
@app.route("/citista_users", methods=["POST"])
def citista_users():
token_receive = request.cookies.get('mytoken')
user = db.citista_users.find_one({'token': token_receive})
nickname = request.form['nickname_give']
profile_info = request.form['desc_give']
if nickname == "": # 닉네임칸에 쓰여있지 않으면 기존 닉네임 유지
nickname = user['nickname']
if profile_info == "": # 내용칸에 쓰여있지 않으면 기존 내용 유지
profile_info = user['profile_info']
doc = {
'nickname': nickname,
'profile_info': profile_info,
}
db.citista_users.update_one({'token': token_receive}, {'$set': doc})
return jsonify({'result': 'success'})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)