-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
315 lines (241 loc) · 9.42 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
import app as app
from flask import Flask, render_template, redirect, request, url_for, flash, session, jsonify
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import or_
import pymysql, hashlib
from adda import Adda
# app.secret_key = Adda().get_secret_key()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SuperSecretKey'
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:123123@localhost:3306/adda"
db = SQLAlchemy(app)
class user(db.Model):
memberid = db.Column(db.Integer, primary_key = True)
ID = db.Column(db.String(255))
password = db.Column(db.String(255))
selfname = db.Column(db.String(255))
phone = db.Column(db.String(255))
# 메인화면
@app.route('/')
def home():
print('home() access')
print(session)
if 'id' in session:
return render_template('11_main_login.html')
return render_template('01_main.html')
class RegisterForm(FlaskForm):
memberid = IntegerField('Memberid ID:')
ID = StringField('id:', validators=[DataRequired()])
password = StringField('PASSWORD:', validators=[DataRequired()])
selfname = StringField('SELFNAME:', validators=[DataRequired()])
phone = StringField('PHONE:', validators=[DataRequired()])
def log(user_id, api_uri, method):
Adda().log(user_id, api_uri, method)
# 로그인화면
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template('02_login.html')
@app.route('/logout')
def logout():
return redirect(url_for(''))
# 회원가입화면
@app.route('/signup')
def signup():
return render_template('03_signup.html')
# 추가화면
@app.route('/about')
def about():
return '김범석, 송지훈, 이호승, 전진영, 선승우'
# Aside
@app.route('/game')
def game():
return render_template('04_game.html')
# mypage
@app.route('/mypage')
def mypage():
return render_template('12_mypage.html')
# mypage
@app.route('/writting')
def writting():
return render_template('13_writting.html')
# 회원가입 구현
@app.route('/api/register', methods=['GET','POST'])
def register():
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor()
if request.method == 'POST':
register_info = request.form
id = register_info['id']
password = register_info['password']
password = hashlib.sha256(password.encode()).hexdigest()
print(password)
selfname = register_info['selfname']
phone = register_info['phone']
sql = """
INSERT INTO user (id, password, selfname, phone)
VALUES (%s, %s, %s, %s);
"""
cursor.execute(sql,(id, password, selfname, phone))
db.commit()
Adda().log(id, '/add_register', 'POST')
print(id, password, selfname, phone)
#return redirect(request.url)
return render_template('03_signup.html')
return render_template('03_signup.html')
# 이건 망함
@app.route('/add_register', methods=['GET', 'POST'])
def add_register():
form = RegisterForm()
if form.validate_on_submit():
register = user(ID=form.ID.data, password=form.password.data, selfname= form.selfname.data, phone = form.phone.data)
db.session.add(register)
db.session.commit()
return redirect('/')
return render_template('add_register.html', form=form, pageTitle='회원등록하기')
@app.route('/delete_register/<int:member_id>', methods=['GET','POST'])
def delete_register(member_id):
if request.method == 'POST': # 만약 POST를 요청 받을 시, 데이터베이스에서 친구를 삭제한다
register = user.query.get_or_404(member_id)
db.session.delete(register)
db.session.commit()
return redirect('/')
else: # 만약 GET을 요청 받을 시, 메인페이지로 보내준다.
return redirect('/')
@app.route('/register/register/<int:member_id>', methods=['GET','POST'])
def get_register(member_id):
register = user.query.get_or_404(member_id)
return render_template('register.html', form=register, pageTitle='회원정보 세부사항', legend="회원정보 세부사항")
@app.route('/register/register/<int:member_id>/update', methods=['GET','POST'])
def update_register(member_id):
register = user.query.get_or_404(member_id)
form = RegisterForm()
if form.validate_on_submit():
register.ID = form.ID.data
register.password = form.password.data
register.selfname = form.selfname.data
register.phone = form.phone.data
db.session.commit()
return redirect(url_for('get_register', member_id=register.memberid))
form.memberid.data = register.memberid
form.ID.data = register.ID
form.password.data = register.password
form.selfname.data = form.selfname
form.phone.data = register.phone
return render_template('update_register.html', form=form, pageTitle='Update Friend', legend="Update A Friend")
@app.route('/api/user_img', methods=['POST'])
def api_usr_img_upload():
print('api_usr_img_upload() access')
Adda().log(session['id'], '/api/user_img', 'POST')
f = request.files['file']
extension = f.filename.split('.')[-1]
print(f.filename.split('.'))
filename = f'{session["id"]}.{extension}'
print(filename)
f.save(f'static/profile_image/{filename}')
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute('use adda;')
cursor.execute(f'update user set profile = "{filename}" where id = "{session["id"]}";')
db.commit()
db.close()
return redirect(url_for('mypage'))
@app.route('/api/user_modi', methods=['POST'])
def api_user_modi():
print('api_user_modi() accecc')
Adda().log(session['id'], '/api/user_modi', 'POST')
modi_password= request.json['password']
modi_password = hashlib.sha256(modi_password.encode()).hexdigest()
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute('use adda;')
cursor.execute(f'update user set password = "{modi_password}" where id = "{session["id"]}";')
db.commit()
db.close()
return jsonify({'msg':'success'})
@app.route('/api/withdrawal', methods=['GET'])
def api_withdrawal():
print('api_withdrawal() accescc')
Adda().log(session['id'], '/api/withdrawal', 'GET')
print(session)
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
print('test here')
cursor.execute('use adda;')
cursor.execute(f'delete from user where id = "{session["id"]}";')
db.commit()
db.close()
# return redirect(url_for('api_logout'))
return jsonify({'msg':'success'})
@app.route('/api/user_info', methods=['GET'])
def api_user_img_load():
print('api user img load() access')
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute('use adda;')
cursor.execute(f'select id, selfname, phone, profile from user where id = "{session["id"]}";')
user = cursor.fetchone()
print(user)
db.commit()
db.close()
return user
# 로그인
@app.route('/api/login', methods=['POST'])
def api_login():
print('api_login() access')
id_receive = request.form['id_give']
pass_receive = request.form['pass_give']
print(id_receive)
print(pass_receive)
pass_receive = hashlib.sha256(pass_receive.encode()).hexdigest()
# user={
# 'id' : id_receive,
# 'pass' : pass_receive
# }
db = pymysql.connect(host='localhost',
port=3306,
user='root',
password='123123',
db='adda',
charset='utf8'
)
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(
f'select * from user where id = "{id_receive}"')
user = cursor.fetchone()
print(user)
if user == None:
print('유저가 없습니다.')
db.close()
return '유저가 없습니다.'
elif user and user["password"] != pass_receive:
print('비밀번호가 틀렸습니다.')
db.close()
return '비밀번호가 없습니다.'
elif user and user["password"] == pass_receive:
print('로그인 완료!')
session['id'] = id_receive
db.commit()
db.close()
if 'id' in session:
Adda().log(session['id'], '/api/login', 'POST')
return redirect(url_for('home'))
# 로그아웃
@app.route('/api/logout')
def api_logout():
if 'id' in session:
print(session)
db = pymysql.connect(host='localhost',port=3306,user='root',password='123123',db='adda',charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(
f'select * from user where id = "{session["id"]}"')
user = cursor.fetchone()
if user:
Adda().log(session['id'], '/api/logout', 'GET')
session.clear()
return redirect(url_for('home'))
# 서버실행
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)