forked from phith0n/Minos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
140 lines (128 loc) · 4.05 KB
/
dashboard.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
#coding=utf-8
import tornado.web, re
from controller.base import BaseHandler
from tornado import gen
import time, pymongo
from util.function import intval
from bson.objectid import ObjectId
class AdminHandler(BaseHandler):
def initialize(self):
super(AdminHandler, self).initialize()
self.topbar = "admin"
def prepare(self):
super(AdminHandler, self).prepare()
if self.power != "admin":
self.redirect("/")
def render(self, template_name, **kwargs):
if self.power == "admin":
render = "admin/%s" % template_name
else:
render = template_name
super(AdminHandler, self).render(render, **kwargs)
def get(self, *args, **kwargs):
action = args[0] if len(args) else "index"
method = "_view_%s" % action
arg = args[2] if len(args) == 3 else None
if hasattr(self, method):
getattr(self, method)(arg)
else:
self._view_index(arg)
@tornado.web.asynchronous
@gen.coroutine
def _view_index(self, arg):
user = self.db.member.find()
article = self.db.article.find()
sort = self.db.sort.find()
active = self.db.member.find({
"logintime": {"$gt": time.time() - 3 * 24 * 60 * 60}
})
count = {
"user": (yield user.count()),
"article": (yield article.count()),
"sort": (yield sort.count()),
"active": (yield active.count())
}
user.sort([('time', pymongo.DESCENDING)]).limit(10)
newusers = yield user.to_list(10)
self.render("index.htm", count = count, newusers = newusers)
@tornado.web.asynchronous
@gen.coroutine
def _view_user(self, arg):
username = self.get_query_argument("username", default=None)
where = {"username": {"$regex": ".*"+re.escape(username)+".*"}} if username else {}
limit = 15
page = intval(arg)
page = page if page > 1 else 1
user = self.db.member.find(where)
count = yield user.count()
user.sort([('time', pymongo.ASCENDING)]).limit(limit).skip((page - 1) * limit)
users = yield user.to_list(limit)
if username:
search = "?username=%s" % username
else:
search = ""
self.render("userlist.htm", page = page, users = users, count = count, each = limit, search = search)
@tornado.web.asynchronous
@gen.coroutine
def _view_userdetail(self, arg):
username = arg
user = yield self.db.member.find_one({
"username": username
})
if not user:
self.custom_error("不存在这个用户")
self.render("userdetail.htm", user = user)
@tornado.web.asynchronous
@gen.coroutine
def _view_sort(self, arg):
limit = 15
page = intval(arg)
page = page if page > 1 else 1
cursor = self.db.sort.find()
count = yield cursor.count()
cursor.limit(limit).skip((page - 1) * limit)
sorts = yield cursor.to_list(limit)
self.render("sort.htm", sorts = sorts, count = count, each = limit, page = page)
@tornado.web.asynchronous
@gen.coroutine
def _view_sortdetail(self, arg):
id = arg
sort = yield self.db.sort.find_one({
"_id": ObjectId(id)
})
if not sort:
self.custom_error("不存在这个板块")
self.render("sortdetail.htm", sort = sort)
@tornado.web.asynchronous
@gen.coroutine
def _view_newsort(self, arg):
self.render("newsort.htm")
@tornado.web.asynchronous
@gen.coroutine
def _view_setting(self, arg):
site = self.settings.get("site")
init_money = self.settings.get("init_money")
captcha = self.settings.get("captcha")
register = self.settings.get("register")
self.render("setting.htm", site = site, init_money = init_money, captcha = captcha, register = register)
@tornado.web.asynchronous
@gen.coroutine
def _view_invite(self, arg):
where = self.get_query_argument("act", default=None)
act = {
"nouse": {"used": False},
"used": {"used": True},
"expire": {
"time": {"$lt": time.time() - self.settings["invite_expire"]},
"used": False
}
}
where = act.get(where) if (where in act) else {}
limit = 15
page = intval(arg)
page = page if page > 1 else 1
cursor = self.db.invite.find(where)
count = yield cursor.count()
cursor.sort([('time', pymongo.DESCENDING)]).limit(limit).skip((page - 1) * limit)
invites = yield cursor.to_list(limit)
self.render("invite.htm", invites = invites, count = count, each = limit, page = page)