forked from ego008/gae-bbs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
206 lines (174 loc) · 7 KB
/
model.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
# -*- coding: utf-8 -*-
from google.appengine.ext import db
from google.appengine.api import memcache
from common import memcached, obj_runput
from yui import CachedProperty
from setting import *
##
class Counter(db.Model):
#key_name
name = db.StringProperty(default='')
value = db.IntegerProperty(default=0)
@staticmethod
@memcached('get_site_counts', 300)
def get_site_counts():
site_info = []
member_num = Counter.get_by_key_name('member_auto_increment')
if member_num:
if member_num.value>1:
site_info.append(('会员',member_num.value-1))
node_num = Counter.get_by_key_name('node_auto_increment')
if node_num:
if node_num.value>1:
site_info.append(('主题',node_num.value-1))
topic_num = Counter.get_by_key_name('all-topic-num')
if topic_num:
site_info.append(('帖子',topic_num.value))
comment_num = Counter.get_by_key_name('all-reply-num')
if comment_num:
site_info.append(('回复',comment_num.value))
return site_info
class Member(db.Model):
#key name: username
id = db.IntegerProperty(indexed=False,default=0)
flag = db.IntegerProperty(indexed=False,default=0)
add = db.IntegerProperty(indexed=False,default=0)
notic = db.StringProperty(indexed=False,default='')#max 16 '999-100000'
@property
def name(self):
return self.key().name()
@property
def notic_objs(self):
return Topic.get_by_key_name(self.notic.split(','))
class MemberInfo(db.Model):
#key name: username
topicn = db.IntegerProperty(indexed=False,default=0) #counter
replyn = db.IntegerProperty(indexed=False,default=0) #counter
topick = db.StringProperty(indexed=False,default='') #key
replyk = db.StringProperty(indexed=False,default='') #key
topict = db.StringProperty(indexed=False,default='') #time
replyt = db.StringProperty(indexed=False,default='') #time
@property
@memcached('topic_objs', 300, lambda self: self.key().name())
def topic_objs(self):
if self.topick:
return Topic.get_by_key_name(self.topick.split(','))
else:
return None
@property
@memcached('reply_objs', 300, lambda self: self.key().name())
def reply_objs(self):
if self.replyk:
return Topic.get_by_key_name(self.replyk.split(','))
else:
return None
class GoogleUser(db.Model):
#key name:id
name = db.StringProperty(indexed=False,default='')
class Node(db.Model):
#sorted id
name = db.StringProperty(indexed=False,default='')
count = db.IntegerProperty(default=0)
imgurl = db.StringProperty(indexed=False,default='')
about = db.StringProperty(indexed=False,default='')
@property
def id(self):
return self.key().id()
@staticmethod
@memcached('newest_add_node', 3600)
def get_newest():
nid_obj = Counter.get_or_insert('node_auto_increment', name='node_auto_increment', value = 1)
from_id = nid_obj.value - 1
to_id = from_id - 10
if to_id<0:
to_id = 0
objs = []
n_objs = Node.get_by_id([id for id in range(from_id, to_id, -1)])
for n_obj in n_objs:
if n_obj:
objs.append(('n-'+str(n_obj.key().id()), n_obj.name))
return objs
@staticmethod
@memcached('get_hot_node', 600)
def get_hot_node():
objs = []
for obj in Node.all().order('-count').fetch(10):
objs.append(('n-'+str(obj.key().id()), obj.name))
return objs
@staticmethod
@memcached('get_recent_node', 3600)
def get_recent_node():
k_obj = KeyStrValue.get_or_insert('rencent-view-node')
if k_obj.value:
objs = []
n_objs = Node.get_by_id([int(id) for id in k_obj.value.split(',')])
for n_obj in n_objs:
if n_obj:
objs.append(('n-'+str(n_obj.key().id()), n_obj.name))
return objs
return None
@staticmethod
@memcached('get_page_topic', 300, lambda nodeid, from_id, to_id: "%s:%s"%(nodeid, str(from_id)))
def get_page_topic(nodeid, from_id, to_id):
return Topic.get_by_key_name(['%s-%d'%(nodeid, i) for i in range(from_id, to_id, -1)])
class Topic(db.Model):
#keyname: 1-2
title = db.StringProperty(indexed=False,default='')
author = db.StringProperty(indexed=False,default='')
add = db.IntegerProperty(indexed=False,default=0)
edit = db.IntegerProperty(indexed=False,default=0)
cnum = db.IntegerProperty(indexed=False,default=0)
reply = db.StringProperty(indexed=False,default='')
con = db.TextProperty(default='') #conten
@CachedProperty
def node(self):
return Node.get_by_id(int(self.key().name().split('-')[0]))
class Comment(db.Model):
#keyname: 1-2-3
author = db.StringProperty(indexed=False,default='')
add = db.IntegerProperty(indexed=False,default=0)
con = db.TextProperty(default='') #conten
@staticmethod
@memcached('get_comments', 300, lambda t_key, from_id, to_id: "%s:%s"%(str(t_key), str(from_id)))
def get_comments(t_key, from_id, to_id):
return Comment.get_by_key_name(["%s-%d"%(t_key,i) for i in range(from_id, to_id+1)])
class KeyStrValue(db.Model):
value = db.StringProperty(indexed=False,default='')
@staticmethod
def add_node_key(nodeid):
k_obj = KeyStrValue.get_or_insert('rencent-view-node')
if k_obj.value:
t_list = k_obj.value.split(',')
if nodeid not in t_list:
t_list.insert(0, nodeid)
k_obj.value = ','.join(t_list[:100])
db.run_in_transaction(obj_runput,k_obj)
else:
k_obj.value = nodeid
db.run_in_transaction(obj_runput,k_obj)
@staticmethod
@memcached('get_topic_objs', 300, lambda key: key)
def get_topic_objs(key):
k_obj = KeyStrValue.get_by_key_name(key)
if k_obj and k_obj.value:
objs = []
t_objs = Topic.get_by_key_name(k_obj.value.split(',')[:RECENT_POST_NUM])
for obj in t_objs:
if obj:
objs.append(obj)
return objs
else:
return []
@staticmethod
@memcached('get_topic_key_title', 300, lambda key: key)
def get_topic_key_title(key):
k_obj = KeyStrValue.get_by_key_name(key)
if k_obj and k_obj.value:
objs = []
t_objs = Topic.get_by_key_name(k_obj.value.split(',')[:RECENT_COMMENT_NUM])
for obj in t_objs:
if obj:
objs.append(('t-'+obj.key().name(), obj.title))
return objs
else:
return None