This repository has been archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathweb.py
224 lines (176 loc) · 6.44 KB
/
web.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
# coding: utf8
import math
import os
import sys
from flask import Flask
from flask import abort, g, jsonify, redirect, request, session, url_for
from flask import render_template as flask_render
from playhouse.shortcuts import model_to_dict
from models import FetchedUser, User, Comment, Like, Status, Blog, Album, Photo, Gossip
from config import config
if getattr(sys, "frozen", False):
application_path = os.path.dirname(sys.executable)
template_folder = os.path.join(application_path, "templates")
static_folder = os.path.join(application_path, "static")
app = Flask(
"__main__", template_folder=template_folder, static_folder=static_folder
)
else:
app = Flask("__main__")
app.secret_key = "5e3d7125660f4793bfe15a87f59e23c1"
def render_template(template_name, **kwargs):
if request.accept_mimetypes.best == "application/json":
return jsonify(success=1, **kwargs)
return flask_render(template_name, **kwargs)
@app.before_request
def handle_session():
uid = 0
paths = request.path.split("/")
if len(paths) > 1 and paths[1].isdigit():
uid = int(paths[1])
if "user" in session and ((not uid) or (uid and session["user"]["uid"] == uid)):
g.user = session["user"] # pylint: disable=E0237
elif uid:
user = FetchedUser.get_or_none(FetchedUser.uid == uid)
if not user:
abort(404, "no such user")
session["user"] = model_to_dict(user)
g.user = session["user"] # pylint: disable=E0237
else:
g.user = None # pylint: disable=E0237
@app.route("/")
@app.route("/index")
def index_page():
users = list(FetchedUser.select().dicts())
return render_template("index.html", users=users)
@app.route("/user/<int:uid>")
def switch_user(uid=0):
return redirect(url_for("status_list_page", uid=uid, page=1))
@app.route("/comments/<int:entry_id>")
def entry_comments_api(entry_id=0):
comments = list(
Comment.select().where(Comment.entry_id == entry_id).order_by(Comment.t).dicts()
)
likes = list(Like.select().where(Like.entry_id == entry_id).dicts())
uids = list(
set([c["authorId"] for c in comments] + [like["uid"] for like in likes])
)
users = {}
u_start = 0
u_size = 64
while u_start < len(uids):
for u in (
User.select().where(User.uid.in_(uids[u_start : u_start + u_size])).dicts()
):
users[u["uid"]] = {"name": u["name"], "headPic": u["headPic"]}
u_start += u_size
for like in likes:
like["name"] = users.get(like["uid"], {}).get("name", "")
like["headPic"] = users.get(like["uid"], {}).get("headPic", "")
for comment in comments:
comment["headPic"] = users.get(comment["authorId"], {}).get("headPic", "")
ret = dict(comments=comments, likes=likes, users=users)
if request.path.split("/")[1] == "comments":
return jsonify(ret)
return ret
@app.route("/<int:uid>/status/page/<int:page>")
def status_list_page(uid, page=1):
if page <= 0:
abort(404)
total_page = int(math.ceil(g.user["status"] * 1.0 / config.ITEMS_PER_PAGE))
status_list = list(
Status.select()
.where(Status.uid == uid)
.order_by(Status.t.desc())
.paginate(page, config.ITEMS_PER_PAGE)
.dicts()
)
for status in status_list:
extra = entry_comments_api(entry_id=status["id"])
status.update(**extra)
return render_template(
"status_list.html", page=page, total_page=total_page, status_list=status_list
)
@app.route("/<int:uid>/blog/page/<int:page>")
def blog_list_page(uid, page=1):
if page <= 0:
abort(404)
total_page = int(math.ceil(g.user["blog"] * 1.0 / config.ITEMS_PER_PAGE))
blog_list = list(
Blog.select()
.where(Blog.uid == uid)
.order_by(Blog.id.desc())
.paginate(page, config.ITEMS_PER_PAGE)
.dicts()
)
return render_template(
"blog_list.html", page=page, total_page=total_page, blog_list=blog_list
)
@app.route("/blog/<int:blog_id>")
def blog_detail_page(blog_id=0):
blog = model_to_dict(Blog.get(Blog.id == blog_id))
if not blog:
abort(404)
extra = entry_comments_api(entry_id=blog_id)
return render_template("blog.html", blog=blog, **extra)
@app.route("/<int:uid>/album/page/<int:page>")
def album_list_page(uid, page=1):
if page <= 0:
abort(404)
total_page = int(math.ceil(g.user["album"] * 1.0 / config.ITEMS_PER_PAGE))
album_list = list(
Album.select()
.where(Album.uid == uid)
.order_by(Album.id.desc())
.paginate(page, config.ITEMS_PER_PAGE)
.dicts()
)
return render_template(
"album_list.html", page=page, total_page=total_page, album_list=album_list
)
@app.route("/album/<int:album_id>/page/<int:page>")
def album_detail_page(album_id=0, page=0):
if page <= 0:
abort(404)
album = model_to_dict(Album.get(Album.id == album_id))
if not album:
abort(404)
total_page = int(math.ceil(album["count"] * 1.0 / config.ITEMS_PER_PAGE))
extra = entry_comments_api(entry_id=album_id)
photos = list(
Photo.select()
.where(Photo.album_id == album_id)
.order_by(Photo.pos)
.paginate(page, config.ITEMS_PER_PAGE)
.dicts()
)
return render_template(
"album.html",
album=album,
page=page,
total_page=total_page,
photos=photos,
**extra
)
@app.route("/photo/<int:photo_id>")
def photo_detail_page(photo_id=0):
photo = model_to_dict(Photo.get(Photo.id == photo_id))
if not photo:
abort(404)
extra = entry_comments_api(entry_id=photo_id)
return render_template("photo.html", photo=photo, **extra)
@app.route("/<int:uid>/gossip/page/<int:page>")
def gossip_list_page(uid, page=1):
if page <= 0:
abort(404)
total_page = int(math.ceil(g.user["gossip"] * 1.0 / config.ITEMS_PER_PAGE))
gossip_list = list(
Gossip.select()
.where(Gossip.uid == uid)
.order_by(Gossip.t.desc(), Gossip.id.desc())
.paginate(page, config.ITEMS_PER_PAGE)
.dicts()
)
return render_template(
"gossip_list.html", page=page, total_page=total_page, gossip_list=gossip_list
)