-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathroutes.py
207 lines (165 loc) · 6.27 KB
/
routes.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
# coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
from flask import Flask, render_template, jsonify,request,url_for,g
from werkzeug import secure_filename
from vdisksdk import *
UPLOAD_FOLDER = 'uploadDir/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
import MySQLdb as db
#from getGithubBlog import sqldb
from getGithubBlog import getGithubBlog
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/favicon.ico")
def addFavicon():
return app.send_static_file("favicon.ico")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/update")
def update():
getGithubBlog.updatemain()
return "ok"
### for db
@app.before_request
def before_request():
### sae has the APP_NAME but local does not
### APP_NAME:应用名
### APP_VERSION: 当前应用使用的版本号
### SERVER_SOFTWARE: 当前server的版本
from os import environ
if environ.get("SERVER_SOFTWARE", ""):
g.db = get_db_sae()
else :
g.db = get_db()
def get_db_sae():
import sae.const
# sae.const.MYSQL_DB # 数据库名
# sae.const.MYSQL_USER # 用户名
# sae.const.MYSQL_PASS # 密码
# sae.const.MYSQL_HOST # 主库域名(可读写)
# sae.const.MYSQL_PORT # 端口,类型为,请根据框架要求自行转换为int
# sae.const.MYSQL_HOST_S # 从库域名(只读)
### stupid the port
com=db.connect(sae.const.MYSQL_HOST,sae.const.MYSQL_USER,sae.const.MYSQL_PASS,sae.const.MYSQL_DB,charset='utf8',port=int(sae.const.MYSQL_PORT))
return com.cursor()
def get_db():
com=db.connect("localhost","root","","blog",charset='utf8',port=3306)
return com.cursor()
# import sqlite3
# com=sqlite3.connect("getGithubBlog/getGithubBlog.db")
# return com
def query_db(query, args=(), one=False):
cur = g.db.execute(query,args)
#for sqllite3
# rv = [dict((cur.description[idx][0], value)
# for idx, value in enumerate(row)) for row in cur.fetchall()]
# for mysql
rv = [dict((g.db.description[idx][0], value)
for idx, value in enumerate(row)) for row in g.db.fetchall()]
return (rv[0] if rv else None) if one else rv
@app.teardown_appcontext
def close_db_connection(exception):
if hasattr(g, 'db'):
g.db.close()
## 每一页显示的数目
itemsOnPage=5
@app.route('/')
def home():
import base64
blog_entries=query_db("select * from blog_entries order by datetime desc limit %s" % itemsOnPage)
## 这块需要blog 总的数量..
blog_all=query_db("select * from blog_entries")
blog=[]
# blog=[{'des':base64.decodestring(blog_entry['des']),'id':blog_entry['id'] } for blog_entry in blog_entries]
blog=[{'des':base64.decodestring(blog_entry['des']),'id':blog_entry['id'] ,'datetime':blog_entry['datetime']} for blog_entry in blog_entries]
# return "ok"
return render_template('getGithubBlog/home.html',blog=blog,itemsOnPage=itemsOnPage,items=len(blog_all),currentPage=1)
## 分页显示的 route
@app.route('/page/<pageName>')
def page(pageName):
import base64
blog_entries=query_db("select * from blog_entries order by datetime desc limit %s,%s" % ((int(pageName)-1)*itemsOnPage,itemsOnPage))
blog_all=query_db("select * from blog_entries")
blog=[]
blog=[{'des':base64.decodestring(blog_entry['des']),'id':blog_entry['id'] ,'datetime':blog_entry['datetime']} for blog_entry in blog_entries]
return render_template('getGithubBlog/home.html',blog=blog,itemsOnPage=itemsOnPage,items=len(blog_all),currentPage=pageName)
## about blog
@app.route('/blog/<blogId>')
def blog(blogId):
import base64
#for sqllite3
# blog_entries=query_db("select * from blog_entries where id=?",[blogId],one=True)
blog_entries=query_db("select * from blog_entries where id=%s",[blogId],one=True)
return render_template('blog.html',blog=base64.decodestring(blog_entries['text']))
@app.route('/about',methods=['post','get'])
def about():
return render_template('about.html')
from flask.ext.mail import Message, Mail
import config
mail=Mail()
## config for mail
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = getattr(config,'mailName','')
app.config["MAIL_PASSWORD"] = getattr(config,'mailpasswd','')
from forms import ContactForm
app.secret_key = 'xiyoulaoyuanjia'
mail.init_app(app)
@app.route("/contact",methods=['post','get'])
def contact():
form = ContactForm()
if request.method == "POST":
if form.validate() == False:
return render_template("contact.html",form=form)
else:
msg = Message(form.subject.data, sender='[email protected]', recipients=['[email protected]'])
msg.body = """
From: %s <%s>
%s
""" % (form.name.data.encode("utf-8"), form.email.data.encode("utf-8"), form.message.data.encode("utf-8"))
mail.send(msg)
return render_template("contact.html",success=True)
# return "ok"
elif request.method == "GET":
return render_template("contact.html",form=form)
@app.route('/getlink')
def getlink():
return render_template('getLink.html')
##
@app.route('/upload_file', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print request.files
files = request.files.getlist("images[]")
print files
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# return jsonify(res="<h2>Successfully Uploaded Images</h2>")
return "<h4>Successfully Uploaded Images</h4>"
# return redirect(url_for('uploaded_file',filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/getlink/get_token')
def get_token():
client = VDiskAPIClient('[email protected]', 'yuanjia')
client.post.auth__get_token()
return jsonify(token=client.access_token)
@app.route('/markdownEditor')
def markdownEditor():
return render_template('markdownEditor/markdownEditor.html')
if __name__ == '__main__':
app.run(debug=True)