forked from honmaple/maple-bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_count.py
executable file
·93 lines (79 loc) · 2.68 KB
/
upgrade_count.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **************************************************************************
# Copyright © 2017 jianglin
# File Name: upgrade_coount.py
# Author: jianglin
# Email: [email protected]
# Created: 2017-04-02 13:00:02 (CST)
# Last Update:星期日 2017-4-2 15:52:41 (CST)
# By:
# Description:
# **************************************************************************
from forums.api.topic.models import Topic, Reply
from forums.api.forums.models import Board
from forums.api.user.models import User
from forums.extension import redis_data
from runserver import app
def forums():
key = 'count:forums'
redis_data.hset(key, 'user', User.query.count())
redis_data.hset(key, 'topic', Topic.query.count())
redis_data.hset(key, 'post', Reply.query.count() + Topic.query.count())
def topic():
topics = Topic.query.all()
for t in topics:
print('topic', t.title)
key = 'count:topic:{}'.format(t.id)
reply_count = t.replies.count()
read_key = 'topic:{}'.format(t.id)
read_count = redis_data.hget(read_key, 'read')
if reply_count:
redis_data.hset(key, 'replies', reply_count)
if read_count:
redis_data.hset(key, 'read', read_count)
# 删除旧key
redis_data.delete(read_key)
def reply():
replies = Reply.query.all()
for t in replies:
print('reply', t.id)
key = 'count:reply:{}'.format(t.id)
liker_count = t.likers.count()
if liker_count:
redis_data.hset(key, 'liker', liker_count)
def user():
users = User.query.all()
for t in users:
print('user', t.username)
key = 'count:user:{}'.format(t.id)
topic_count = t.topics.count()
reply_count = t.replies.count()
if topic_count:
redis_data.hset(key, 'topic', topic_count)
if reply_count:
redis_data.hset(key, 'replies', topic_count)
# 删除旧key
old_user_key = 'user:{}'.format(t.id)
redis_data.delete(old_user_key)
def board():
boards = Board.query.all()
for b in boards:
print('board', b.name)
key = 'count:board:{}'.format(b.id)
topic_count = Topic.query.filter_by(board_id=b.id).count()
reply_count = Reply.query.filter_by(topic__board_id=b.id).count()
post_count = topic_count + reply_count
if topic_count:
redis_data.hset(key, 'topic', topic_count)
if post_count:
redis_data.hset(key, 'post', topic_count)
def main():
with app.app_context():
forums()
topic()
board()
user()
reply()
if __name__ == '__main__':
main()