-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (81 loc) · 2.63 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
from flask import Flask
from flask import session
from flask import render_template
from flask import redirect
from flask import request
from flask import url_for
import pymysql
import OpenSSL
app = Flask(__name__)
# Fake
conn = pymysql.connect(
host='localhost',
user='root',
password='',
db='',
charset='utf8',
port=5088
)
curs = conn.cursor()
# 페이지 템플릿
pagination = {
}
# mysql> CREATE TABLE posts(
# -> title VARCHAR(100) DEFAULT '',
# -> author VARCHAR(20) DEFAULT '',
# -> content VARCHAR(100) DEFAULT '');
# Query OK, 0 rows affected (0.03 sec)
@app.route('/', methods=['GET', 'POST'])
def main():
if request.method == 'POST':
title = request.form['title']
author = request.form['author']
content = request.form['content']
if title != '' and author != '' and content != '':
sql = "INSERT INTO posts VALUES ('%s', '%s', '%s')" % (title, author, content)
curs.execute(sql)
data = curs.fetchall()
curs.close()
conn.close()
if not data:
conn.commit()
print('게시글 업로드 완료!')
return ''
else:
conn.rollback()
return "알 수 없는 작업입니다."
elif request.method == 'GET':
title = request.args.get('title', '')
author = request.args.get('author', '')
content = request.args.get('content', '')
if title != '' and author != '' and content != '':
sql = "INSERT INTO posts VALUES ('%s', '%s', '%s')" % (title, author, content)
curs.execute(sql)
data = curs.fetchall()
curs.close()
conn.close()
if not data:
conn.commit()
print('게시글 업로드 완료!')
return ''
else:
conn.rollback()
return "알 수 없는 작업입니다."
return render_template('main.html')
else:
return "GET 또는 POST 접근만 허용"
@app.route('/list/', methods=['GET', 'POST'])
def listFun():
# 게시글 목록으로 이동
# TODO.. obj 안에 모든 게시글 담기
curs.execute("SELECT title, author, content FROM posts")
data = curs.fetchall()
data_list = []
for obj in data:
mapping = {
'title': obj[0],
'author': obj[1],
'content': obj[2]
}
data_list.append(mapping)
return render_template('list.html', data_list=data_list)