/
store.py
383 lines (319 loc) · 10.1 KB
/
store.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python2.7
#coding:utf-8
"""
@description:mysql of python
"""
import MySQLdb
from settings import logger,cache
from DBUtils.PooledDB import PooledDB
import uuid
import datetime
import web
from sqlbean.db import connection
dbpool = PooledDB(creator=MySQLdb,
maxusage=1000,
host='localhost',
user='root',
passwd='',
db='talkincode_db1',
charset="utf8")
get_conn = lambda : dbpool.connection()
DATABASE = dbpool.connection()
DATABASE.b_commit = True
def get_db_by_table(table_name):
return DATABASE
connection.get_db_by_table = get_db_by_table
from sqlbean.shortcut import Model
from sqlbean.db.query import Query
class User(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="users"
class Post(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="posts"
class Code(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="codes"
class Project(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="projects"
class Comment(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="comments"
class Tag(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table="tags"
class Lang(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "id"
table = "langs"
class Authkey(Model):
def __getitem__(self,key):
return getattr(self,key)
class Meta:
pk = "authkey"
table = "authkeys"
post_page = lambda: Query(model=Post)
code_page = lambda: Query(model=Code)
def todict(row,rowdesc):
d = {}
for idx, col in enumerate(rowdesc):
d[col[0]] = row[idx]
return d
def nextid():
return uuid.uuid4().hex
def currtime():
return datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
def register(username,password,email):
if not username or not password or not email:
raise Exception("username,password,email not empty")
try:
if User.count(username=username):
raise Exception("username already exists")
if User.count(email=email):
raise Exception("email already exists")
authkey = Authkey()
authkey.authkey = uuid.uuid4().hex
authkey.consumer = username
authkey.description = ""
authkey.hits = 0
authkey.status = 1
authkey.create_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
user = User()
user.id = authkey.authkey
user.username = username
user.nicename = username
user.email = email
user.password = password
user.authkey = authkey.authkey
user.created = authkey.create_time
user.lastlogin = authkey.create_time
User.begin()
authkey.save()
user.save()
User.commit()
return user
except Exception,e:
User.rollback()
logger.error("register error,%s"%e)
raise e
def login(username,password):
if not username or not password :
raise Exception("username,password not empty")
try:
user = User.get(username=username,password=password)
if not user:
raise Exception("user not exists")
user.lastlogin = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
user.save()
return user
except Exception,e:
logger.error("login error,%s"%e)
raise e
def sitemap_data():
urlset = []
try:
for post in Post.where(status=1):
lastmod_tmp = datetime.datetime.strptime(post.modified,"%Y-%m-%d %H:%M:%S")
lastmod = lastmod_tmp.strftime("%Y-%m-%dT%H:%M:%SZ")
url = dict(loc="http://www.talkincode.org/news/post/view/%s"%post.id,
lastmod=lastmod,chgfreq="daily")
urlset.append(url)
for code in Code.where():
lastmod_tmp = datetime.datetime.strptime(code.create_time,"%Y-%m-%d %H:%M:%S")
lastmod = lastmod_tmp.strftime("%Y-%m-%dT%H:%M:%SZ")
url = dict(loc="http://www.talkincode.org/code/view/%s"%code.id,
lastmod=lastmod,chgfreq="monthly")
urlset.append(url)
for proj in Project.where():
lastmod_tmp = datetime.datetime.strptime(proj.created,"%Y-%m-%d %H:%M:%S")
lastmod = lastmod_tmp.strftime("%Y-%m-%dT%H:%M:%SZ")
url = dict(loc="http://www.talkincode.org/open/proj/view/%s"%proj.id,
lastmod=lastmod,chgfreq="monthly")
urlset.append(url)
return urlset
except Exception,e:
logger.error("login error,%s"%e)
return []
def get_tags(limit):
tagset = {}
try:
for code in Code.where():
tag = code.tags
if not tag:
continue
ts = tag.split(",")
for t in ts:
if tagset.has_key(t):
tagset[t] += 1
else:
tagset[t] = 1
for post in Post.where(status=1):
tag = post.tags
if not tag:
continue
ts = tag.split(",")
for t in ts:
if tagset.has_key(t):
tagset[t] += 1
else:
tagset[t] = 1
sort_tags = sorted( tagset.items(),key=lambda d:d[1],reverse=True)
print sort_tags
return sort_tags[:limit]
except Exception,e:
logger.error("get tags error,%s"%e)
raise e
#@cache.cache('get_code_tags_func', expire=3600)
def get_code_tags(limit):
tagset = {}
try:
for code in Code.where():
tag = code.tags
if not tag:
continue
ts = tag.split(",")
for t in ts:
if tagset.has_key(t):
tagset[t] += 1
else:
tagset[t] = 1
sort_tags = sorted( tagset.items(),key=lambda d:d[1],reverse=True)
return sort_tags[:limit]
except Exception,e:
logger.error("get post tags error,%s"%e)
raise e
def get_post_tags(limit):
tagset = {}
try:
for post in Post.where(status=1):
tag = post.tags
if not tag:
continue
ts = tag.split(",")
for t in ts:
if tagset.has_key(t):
tagset[t] += 1
else:
tagset[t] = 1
sort_tags = sorted( tagset.items(),key=lambda d:d[1],reverse=True)
return sort_tags[:limit]
except Exception,e:
logger.error("get post tags error,%s"%e)
raise e
def get_project_tags(limit):
tagset = {}
try:
for proj in Project.where():
tag = proj.tags
if not tag:
continue
ts = tag.split(",")
for t in ts:
if tagset.has_key(t):
tagset[t] += 1
else:
tagset[t] = 1
sort_tags = sorted( tagset.items(),key=lambda d:d[1],reverse=True)
return sort_tags[:limit]
except Exception,e:
logger.error("get post tags error,%s"%e)
raise e
#@cache.cache('get_post_stats_func', expire=3600)
def get_post_stats(tag):
try:
if tag:
cur = Post.raw_sql("""
SELECT COUNT(*) AS total, SUM(hits) AS hits_total
FROM posts
where tags like %s
""",'%%%s%%'%tag)
else:
cur = Post.raw_sql("""
SELECT COUNT(*) AS total, SUM(hits) AS hits_total
FROM posts
""")
if web.config.debug:
logger.info("execute sql: %s "%cur._executed)
ones = cur.fetchone()
if ones :
return todict(ones,cur.description)
else:
raise Exception("no result")
except:
raise
@cache.cache('get_user_func', expire=3600)
def get_user(id):
return User.get(id)
# def initdata():
# conn = get_conn()
# cur = conn.cursor()
# grps = {"python":"python编程",
# "php":"php编程",
# "html":"html&css",
# "func":"函数式编程",
# "st2":"sublime text 2小站",
# "vim":"vim小站",
# "emacs":"emacs小站"}
# langs = {"c":"c",
# "c++":"cpp",
# "java":"java",
# "c#":"cs",
# "python":"py",
# "ruby":"rb",
# "php":"php",
# "perl":"pl",
# "objective-c":"c",
# "vb":"vb",
# "javascript":"js",
# "pascal":"pas",
# "lisp":"lsp",
# "sql":"sql",
# "lua":"lua",
# "matlab":"m",
# "shell":"sh",
# "golang":"go"}
# try:
# count = 0
# cur.execute("delete from langs")
# for k,l in langs.items():
# count += 1
# cur.execute("insert into langs (id,name,ext,hits) values(%s,%s,%s,%s)",(count,k,l,0))
# cur.execute("delete from groups")
# count = 0
# for k,g in grps.items():
# count += 1
# cur.execute("insert into groups (id,name,guid,posts) values(%s,%s,%s,%s)",(count,g,k,0))
# cur.execute("insert into groups (id,name,guid,posts) values(%s,%s,%s,%s)",(0,"综合讨论","all",0))
# conn.commit()
# except Exception, e:
# raise e
# finally:
# cur.close()
# conn.close()
if __name__ == "__main__":
"""pass"""
p0 = post_page().where()[0:20]
p1 = Post.where()[15:300]
print len(p0)
print len(p1)