from flask import Flask, request, session, render_template
app = Flask(__name__)
from urllib import parse
import json
import pymysql
import time
json_data=open('set.json').read()
data = json.loads(json_data)
conn = pymysql.connect(host = data['host'], user = data['user'], password = data['pw'], db = data['db'], charset = 'utf8')
curs = conn.cursor(pymysql.cursors.DictCursor)
def getip(request):
if request.headers.getlist("X-Forwarded-For"):
ip = request.headers.getlist("X-Forwarded-For")[0]
else:
ip = request.remote_addr
return ip
def getnow():
now = time.localtime()
s = "%04d-%02d-%02d %02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
return s
def recent(title, ip, today, send, leng):
curs.execute("insert into rc (title, date, ip, send, leng, back) value ('" + title + "', '" + today + "', '" + ip + "', '" + send + "', '" + leng + "', '')")
conn.commit()
def history(number, title, data, date, ip, send, leng):
curs.execute("insert into history (id, title, data, date, ip, send, leng) value ()")
conn.commit()
def getleng(existing, change):
if(existing < change):
leng = change - existing
leng = '+' + str(leng)
elif(change < existing):
leng = existing - change
leng = '-' + str(leng)
else:
leng = '0'
return leng;
@app.route('/')
@app.route('/w/')
def redirect():
return ''
@app.route('/recentchanges')
def recentchanges():
i = 0
curs.execute("select * from rc order by date desc limit 50")
rows = curs.fetchall()
if(rows):
return render_template('index.html', logo = data['name'], rows = rows, tn = 3, title = '최근 변경내역')
else:
return render_template('index.html', logo = data['name'], rows = '', tn = 3, title = '최근 변경내역')
@app.route('/search', methods=['POST', 'GET'])
def search():
if(request.method == 'POST'):
print(request.form["search"])
return ''
else:
return ''
@app.route('/w/')
def w(name = None):
curs.execute("select * from data where title = '" + name + "'")
rows = curs.fetchall()
if(rows):
return render_template('index.html', title = name, logo = data['name'], page = parse.quote(name), data = rows[0]['data'], license = data['license'], tn = 1)
else:
return render_template('index.html', title = name, logo = data['name'], page = parse.quote(name), data = '문서 없음', license = data['license'], tn = 1)
@app.route('/edit/', methods=['POST', 'GET'])
def edit(name = None):
if(request.method == 'POST'):
curs.execute("select * from data where title = '" + name + "'")
rows = curs.fetchall()
if(rows):
ip = getip(request)
today = getnow()
leng = getleng(len(rows[0]['data']), len(request.form["content"]))
recent(name, ip, today, request.form["send"], leng)
curs.execute("update data set data = '" + request.form["content"] + "' where title = '" + name + "'")
conn.commit()
else:
ip = getip(request)
today = getnow()
leng = getleng(len(rows[0]['data']), len(request.form["content"]))
recent(name, ip, today, request.form["send"], leng)
curs.execute("insert into data (title, data, acl) value ('" + name + "', '" + request.form["content"] + "', '')")
conn.commit()
return ''
else:
curs.execute("select * from data where title = '" + name + "'")
rows = curs.fetchall()
if(rows):
return render_template('index.html', title = name, logo = data['name'], page = parse.quote(name), data = rows[0]['data'], tn = 2)
else:
return render_template('index.html', title = name, logo = data['name'], page = parse.quote(name), data = '', tn = 2)
@app.route('/setup')
def setup():
curs.execute("create table if not exists data(title text not null, data longtext not null, acl text not null)")
curs.execute("create table if not exists history(id text not null, title text not null, data longtext not null, date text not null, ip text not null, send text not null, leng text not null)")
curs.execute("create table if not exists rc(title text not null, date text not null, ip text not null, send text not null, leng text not null, back text not null)")
curs.execute("create table if not exists rd(title text not null, sub text not null, date text not null, ip text not null)")
curs.execute("create table if not exists user(id text not null, pw text not null, acl text not null)")
curs.execute("create table if not exists ban(block text not null, end text not null, why text not null, band text not null)")
curs.execute("create table if not exists topic(id text not null, title text not null, sub text not null, data longtext not null, date text not null, ip text not null, block text not null)")
return render_template('index.html', title = '설치 완료', logo = data['name'], data = '문제 없었음')
@app.route('/other')
def other():
return render_template('index.html', title = '기타 메뉴', logo = data['name'], data = '모든 문서문법 설명버전')
@app.route('/titleindex')
def titleindex():
curs.execute("select * from data")
rows = curs.fetchall()
if(rows):
return render_template('index.html', logo = data['name'], rows = rows, tn = 4, title = '모든 문서')
else:
return render_template('index.html', logo = data['name'], rows = '', tn = 4, title = '모든 문서')
@app.route('/grammar')
def grammar():
return render_template('index.html', title = '문법 설명', logo = data['name'], data = '아직 없음')
@app.route('/version')
def version():
return render_template('index.html', title = '버전', logo = data['name'], data = '0.1
문서 보기와 편집기타 문서랜덤 구현')
@app.route('/random')
def random():
curs.execute("select * from data order by rand() limit 1;")
rows = curs.fetchall()
if(rows):
return ''
else:
return ''
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 3000)