forked from JoneXiong/YouMd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
378 lines (303 loc) · 12 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
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
# coding=utf-8
import os
import datetime
from mole import route, run, static_file, error, get, post, put, delete
from mole.template import template
from mole import request
from mole import response
from mole.mole import json_dumps
from mole import redirect
from mole.sessions import get_current_session, authenticator
import config
from __init__ import entryService
media_prefix = 'static'
auth_required = authenticator(login_url='/auth/login')
@route('/%s/:file#.*#' % media_prefix)
def media(file):
return static_file(file, root='./static')
@route('/new')
def editor():
return template('editor')
@route('/')
def Index():
limit = int(request.GET.get('limit', 10))
start = int(request.GET.get('start', 1))
params = entryService.search(entryService.types.index, config.index_url, '', start, limit)
return template('index', params=params, config=config)
@route('/language')
def language():
session = get_current_session()
session["language"] = str(request.GET.get('language', 'cn'))
if session["language"] == 'en':
config.en_trans.install()
else:
config.zh_trans.install()
return Index()
@route('/blog:url#.*#')
def Entry(url):
if not url in ['', '/']:
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
if params.entry == None:
return template('error', params=params, config=config)
else:
if params.entry.private:
return template('error', params=params, config=config)
return template('entry', params=params, config=config)
params = entryService.search(entryService.types.index, url)
return template('index', params=params, config=config)
@route('/archive:url#.*#')
def Archive(url):
url = config.archive_url + url
params = entryService.archive(entryService.types.entry, url)
if params.entries == None:
return template('error', params=params, config=config)
return template('archive', params=params, config=config)
@route('/about.html')
def About():
url = config.about_url
params = entryService.find_by_url(entryService.types.page, url)
if params.entry == None:
return template('error', params=params, config=config)
return template('entry', params=params, config=config)
@route('/atom.xml')
def Subscribe():
params = entryService.search(entryService.types.index, config.subscribe_url)
response.headers['Content-Type'] = 'text/xml'
return template('atom', params=params, config=config)
@route('/search')
def Search():
type = request.GET.get('type', entryService.types.query)
value = request.GET.get('value', '')
limit = int(request.GET.get('limit', config.limit))
start = int(request.GET.get('start', config.start))
url = '%s/?type=%s&value=%s&start=%d&limit=%d' % (config.search_url, type, value, start, limit)
params = entryService.search(type, url, value, start, limit)
if not params.entries == None:
return template('search', params=params, config=config)
return template('error', params=params, config=config)
@route('/raw:url#.*#')
@auth_required()
def Raw(url):
url = config.raw_url + url
raw = entryService.find_raw(url)
if not raw == None:
response.headers['Content-Type'] = 'text/plain'
response.headers['Content-Encoding'] = 'utf-8'
return raw.strip()
params = entryService.archive(entryService.types.raw, url)
if params.entries == None:
return template('error', params=params, config=config)
return template('archive', params=params, config=config)
@route('/get_head', method='POST')
def GetHead():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'code': -2, 'msg': '未登录'}
raw_url = request.POST.get("raw_url", '')
url = raw_url.replace(config.raw_url, '').replace(config.raw_suffix, config.url_suffix)
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
entry = params.entry
if entry:
tags = [e for e in entry.tags if not e.startswith('__')]
data = {
'title': entry.name,
'cat': entry.categories and ','.join(entry.categories) or '',
'tag': tags and ','.join(tags) or '',
'private': entry.private
}
ret = {'code': 0, 'msg': 'ok', 'data': data}
else:
ret = {'code': -1, 'msg': 'can not find'}
return ret
@route('/save_head', method='POST')
def SaveHead():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'code': -2, 'msg': '未登录'}
title = request.POST.get("title", '').strip()
cat = request.POST.get("cat", '').strip().replace(',', ',')
tag = request.POST.get("tag", '').strip().replace(',', ',')
private = request.POST.get("private", '')
content = request.POST.get("content", '').strip()
raw_url = request.POST.get("raw_url", '')
if not title:
return {'code': -3, 'msg': '请填写完整'}
url = raw_url.replace(config.raw_url, '').replace(config.raw_suffix, config.url_suffix)
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
entry = params.entry
if not entry:
return {'code': -4, 'msg': '对象不存在'}
tag = '__%s,%s' % (username, tag) if tag else '__' + username
head = '''---
layout: post
title: %s
category: %s
tags: [%s]
---
''' % (title, cat, tag)
m_file = open(entry.path, 'w+')
m_file.write('%s\n%s' % (head, content))
m_file.close()
entryService.update_entry(entry, {
'title': title,
'tags': tag and tag.split(',') or [],
'cats': cat and cat.split(',') or [],
'private': bool(private)
})
return {'code': 0, 'msg': '修改成功'}
@route('/private_raw:url#.*#')
@auth_required()
def PrivateRaw(url):
session = get_current_session()
username = session.get('username', '')
url = config.raw_url + url
raw = entryService.find_raw(url)
if not raw == None:
response.headers['Content-Type'] = 'text/plain'
response.headers['Content-Encoding'] = 'utf-8'
return raw.strip()
params = entryService.archive(entryService.types.raw, url, private=True, username=username)
if params.entries == None:
return template('error', params=params, config=config)
return template('private', params=params, config=config)
@route('/update:url#.*#')
@auth_required()
def Update(url):
url = config.raw_url + url
return template('update', raw_url=url)
@route('/update_save', method='POST')
# @auth_required()
def UpdateSave():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'code': -2, 'msg': '未登录'}
raw_url = request.POST.get("raw_url", '')
content = request.POST.get("content", '').strip()
entry_url = raw_url.replace(config.raw_url, config.entry_url).replace(config.raw_suffix, config.url_suffix)
entry = entryService.find_by_url(entryService.types.entry, entry_url).entry
if not entry:
page_url = raw_url.replace(config.raw_url, '').replace(config.raw_suffix, config.url_suffix)
entry = entryService.find_by_url(entryService.types.page, page_url).entry
m_file = open(entry.path, 'w')
m_file.write('%s\n%s' % (entry.header, content))
m_file.close()
file_name = entry.path.replace(config.entry_dir,'')
if file_name.startswith('/'):
file_name = file_name.replace('/','',1)
if config.use_git:
from git import Repo
repo = Repo('./entry')
#git_master = repo.heads.master
git_index = repo.index
git_index.add([file_name])
git_index.commit('Update %s'%file_name)
entryService.add_entry(False, entry.path, entry.private)
return {'code': 0, 'msg': '更新成功'}
@route('/delete_post', method='POST')
def DeletePost():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'code': -2, 'msg': '未登录'}
raw_url = request.POST.get("raw_url", '')
entry_url = raw_url.replace(config.raw_url, config.entry_url).replace(config.raw_suffix, config.url_suffix)
entry = entryService.find_by_url(entryService.types.entry, entry_url).entry
entryService.delete_entry(entry.path)
os.remove(entry.path)
if entry.private:
entryService.del_private(entry.path)
return {'code': 0, 'msg': '删除成功'}
@route('/publish', method='POST')
# @auth_required()
def publish():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'code': -2, 'msg': '未登录'}
name = request.POST.get("name", '').strip()
title = request.POST.get("title", '').strip()
cat = request.POST.get("cat", '').strip().replace(',', ',')
tag = request.POST.get("tag", '').strip().replace(',', ',')
private = request.POST.get("private", '') # '' or 'on'
content = request.POST.get("content", '').strip()
if not (name and title):
return {'code': -3, 'msg': '请填写完整'}
tag = '__%s,%s' % (username, tag) if tag else '__' + username
head = '''---
layout: post
title: %s
category: %s
tags: [%s]
---
''' % (title, cat, tag)
m_date = datetime.datetime.now().strftime('%Y-%m-%d')
file_name ='%s-%s.md'%(m_date, name)
path = os.path.join(config.entry_dir,file_name) #'raw/entry/%s-%s.md' % (m_date, name)
m_file = open(path, 'w+')
m_file.write('%s\n%s' % (head, content))
m_file.close()
if config.use_git:
from git import Repo
repo = Repo('./entry')
#git_master = repo.heads.master
git_index = repo.index
git_index.add([file_name])
git_index.commit('Add %s'%file_name)
entryService.add_entry(True, path, True if private else False)
return {'code': 0, 'msg': '发布成功'}
@route('/auth/login', method=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.POST.get("username", '')
password = request.POST.get("password", '')
if password == config.admin_pwd and (username == config.admin_user or username in config.multi_user):
session = get_current_session()
session['username'] = username
return {'code': 0, 'msg': 'OK'}
else:
return {'code': -1, 'msg': '用户名或密码错误'}
else:
return template('auth/login.html', config=config)
@route('/auth/logout')
def logout():
session = get_current_session()
del session['username']
return redirect(request.params.get('next') or '/')
@route('/robots.txt')
def robots():
response.headers['Content-Type'] = 'text/plain'
return template('robots.html', config=config)
@route('/sitemap.xml')
def sitemap():
params = entryService.search(entryService.types.index, config.subscribe_url, limit=10000)
response.headers['Content-Type'] = 'text/xml'
return template('sitemap.html', params=params, config=config)
if not os.path.exists(config.upload_path):
os.makedirs(config.upload_path)
@route('%s/:file#.*#' % config.file_url)
def getfile(file):
return static_file(file, root=config.upload_path)
@route('/upload', method='POST')
def upload():
session = get_current_session()
username = session.get('username', '')
if not username:
return {'success': 0, 'message': '请先登录', 'url': ''}
uploadfile = request.files.get('editormd-image-file')
tnow = datetime.datetime.now()
parent_path = os.path.join(config.upload_path, tnow.strftime('%Y%m'))
if not os.path.exists(parent_path):
os.makedirs(parent_path)
m_f = ("000" + str(tnow.microsecond / 1000))[-3:]
filename = tnow.strftime("%d%H%M%S") + m_f + "_" + uploadfile.filename
upload_path = os.path.join(parent_path, filename)
with open(upload_path, 'w+b') as f:
f.write(uploadfile.file.read())
url = '%s/%s/%s' % (config.file_url, tnow.strftime('%Y%m'), filename)
return {'success': 1, 'message': '上传成功', 'url': url}