-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.py
More file actions
93 lines (77 loc) · 1.71 KB
/
post.py
File metadata and controls
93 lines (77 loc) · 1.71 KB
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
from google.appengine.ext import db
from datetime import datetime
cachetop = []
last = None
COUNT = 10
time = None
class Post(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
def toDict(self):
return {"content" : self.content.encode('utf-8'), "created" : (self.created.isoformat()), "last_modified" : (self.created.isoformat()), "subject" : self.subject.encode('utf-8')}
def id(self):
return self.key().id()
def add(subject,content):
global cachetop
global last
global time
p = Post(subject = subject, content = content)
p.put()
last = p
posts = top()
c = []
c.append(p)
for post in posts:
if post.id() != p.id():
c.append(post)
if len(c) == COUNT:
break
cachetop = c
time = datetime.now()
return p.id()
def top():
global time
global cachetop
if len(cachetop) == 0:
posts = db.GqlQuery("SELECT * FROM Post ORDER BY created DESC LIMIT %i"%COUNT)
time = datetime.now()
c = []
for post in posts:
c.append(post)
cachetop = c
return cachetop
def get(pid):
global time
global last
if last and str(last.id()) == pid:
return [last]
time = datetime.now()
posts = db.GqlQuery("SELECT * FROM Post WHERE __key__ = KEY('Post',%s)"%pid)
for post in posts:
last = post
if last:
return [last]
else:
return []
def delete(pid):
global time
global last
global cachetop
posts = get(pid)
for post in posts:
post.delete()
del cachetop[:]
last = None
time = datetime.now()
def seconds():
if not time:
return 0
return (datetime.now() - time).seconds
def flush():
global time
global last
global cachetop
cachetop = []
last = None
time = None