This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
forked from two-doges/ika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.py
121 lines (111 loc) · 4.04 KB
/
frontend.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
'''the front-end blueprint'''
import datetime
import flask
from flask import render_template as render
from flask import url_for, redirect, request
from requests import post
import endpoint
frontend = flask.Blueprint('frontend', __name__)
@frontend.route('/')
def index():
'''the index page of ika'''
return render('index.html', topics=endpoint.get_topics())
@frontend.route('/t/<string:t>', methods=['GET'])
def ika_list(t): # t means topic
'''return the ika list under root'''
# if no topic endpoint will raise 404
topic = endpoint.get_topic(t)
# tid will always be negative
tid = topic.topic_id * -1
ika_number = endpoint.get_ika_number(tid)
# set the max page
if ika_number is 0:
max_page = 0
else:
# page range is [0, (target_ika.number-1)//20]
max_page = (ika_number-1) // 20
page = int(flask.request.args.get('page', '0'))
# next page
l_p = url_for('frontend.ika_list', t=t, page=page-1)
n_p = url_for('frontend.ika_list', t=t, page=page+1)
if page <= 0:
page = 0
l_p = None
if page >= max_page:
n_p = None
page = max_page
ikas = endpoint.get_reply(tid, 20, page*20)
# t means topic, again
return render('ika_list.html',
fid=tid, ikas=ikas, l_p=l_p, n_p=n_p,
t=topic.topic_title, topics=endpoint.get_topics())
@frontend.route('/ika/<int:ika_id>/')
def ika_page(ika_id):
'''return the spec page of ika'''
target_ika = endpoint.get_ika(ika_id)
# redir to the root is no this ika
if not target_ika:
flask.abort(404)
# redir to the forward_ika if this ika's fid is not root
if target_ika.forward_ika_id > 0:
nika_id = target_ika.forward_ika_id
# find the spec ika
url = url_for('frontend.ika_page', ika_id=nika_id)+'#'+str(ika_id)
return redirect(url)
# set the max page
ika_number = endpoint.get_ika_number(ika_id)
if ika_number == 0:
max_page = 0
else:
max_page = (ika_number-1) // 20
# resolve page
page = int(flask.request.args.get('page', '0'))
# if there is a next page n_p = next page
l_p = url_for('frontend.ika_page', ika_id=ika_id, page=page-1)
n_p = url_for('frontend.ika_page', ika_id=ika_id, page=page+1)
if page <= 0:
page = 0
l_p = None
if page >= max_page:
page = max_page
n_p = None
ikas = endpoint.get_reply(ika_id, 20, page*20)
return render('ika_list.html',
ikas=ikas, l_p=l_p, fid=ika_id, n_p=n_p,
topics=endpoint.get_topics())
@frontend.route('/ika/', methods=['POST'])
def ika_post():
'''post a new ika'''
user_id = flask.request.cookies.get('user_id', '')
try:
user_hash = endpoint.verify_user_id(user_id)
except RuntimeError:
# get user ip (bypass nginx)
user_id = endpoint.gen_user_id(request.remote_addr)
user_hash = endpoint.verify_user_id(user_id)
forward_id = request.form.get('forward_id')
name = request.form.get('name')
title = request.form.get('title')
comment = request.form.get('comment')
# resolve with image
image = request.files.get('image')
if image:
url = 'https://sm.ms/api/upload'
res = post(url, files={'smfile': image})
if res.json()['code'] == 'success':
image = res.json()['data']['url']
else:
raise flask.app.InternalServerError
if not comment and not image:
return render('post_error.html', error='请输入内容',
topics=endpoint.get_topics())
try:
endpoint.new_ika(forward_id, user_hash, name, title, image, comment)
except RuntimeError as err:
return render('post_error.html', error=err,
topics=endpoint.get_topics())
res = flask.make_response(render('post_success.html',
topics=endpoint.get_topics()))
outdate = datetime.datetime.today() + datetime.timedelta(days=30)
res.set_cookie('user_id', user_id, expires=outdate)
return res