-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
151 lines (129 loc) · 4.04 KB
/
data.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
import math
from google.appengine.ext import db
from datetime import timedelta, datetime
from google.appengine.ext.db import polymodel
#from google.appengine.api import memcache
#http://code.google.com/appengine/docs/python/memcache/usingmemcache.html#Memcache
class Entry(polymodel.PolyModel):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
ups = db.IntegerProperty()
posts_limit = 800 # Hardcoded limit
comments_limit = 400 # Hardcoded limit
# Hack moche pour simuler un cron
test_cron = Entry.all()
test_cron.filter('content = ', 'FC')
if test_cron.count(): # is_saved()
cron = Entry.get(test_cron.fetch(1)[0].key())
else:
cron = Entry()
cron.content = "FC"
cron.put()
class Post(Entry):
url = db.StringProperty()
site = db.StringProperty()
rank = db.FloatProperty()
def get_comments(self):
com = Comment.all()
com.filter('post =', self)
# TODO Reply => recursively descend
com.order('-ups')
return com.fetch(comments_limit)
class Comment(Entry):
post = db.ReferenceProperty(Entry)
class Up(db.Model):
entry = db.ReferenceProperty(Entry)
voter = db.UserProperty()
def calc_hours(post):
diff = datetime.utcnow() - post.date
hours = diff.seconds/3600 # :)
if diff.days is not -1:
hours = hours + diff.days*24
return hours
def calc_minutes(post):
diff = datetime.utcnow() - post.date
minutes = diff.seconds/60 # :]
if diff.days is not -1:
minutes = minutes + diff.days*24
return minutes
def count_comments(key):
c = Comment.all()
c.filter('post =', Post.get(key))
return c.count()
#def count_comments(post):
# c = Comment.all()
# c.filter('post =', post)
# return c.count()
def update_rank(post):
#post.rank = (3*post.ups + count_comments(post)) / (4*calc_hours(post) + 1)
return (float(post.ups) / math.pow(calc_hours(post) + 1.0, 1.5))
def update_ranks():
posts_query = Post.all()
posts = posts_query.fetch(posts_limit)
for post in posts:
post.rank = update_rank(post)
post.put()
return 0
def update_check():
#if calc_minutes(cron) > 10:
cron.date = datetime.utcnow()
cron.put()
update_ranks()
def user_found(u, e):
# return 0 is not found and 1 if found #
v = Up.all()
v.filter('entry =', Entry.get(e))
v.filter('voter =', u)
return v.count()
def already(url):
test = Post.all()
test.filter("url = ", url)
return test.fetch(1)
class DisplayPost(Post):
def __init__(self, post, u):
self.hours = calc_hours(post)
self.minutes = calc_minutes(post)
self.comments_number = count_comments(post.key())
self.author = post.author
self.content = post.content
self.ups = post.ups
self.url = post.url
self.site = post.site
self.id = (post.key()).id
if (u is None):
self.varw = 0
else:
self.varw = 1 - user_found(u, post.key())
class DisplayPosts(list):
def __init__(self, posts, u):
for post in posts:
self.append(DisplayPost(post, u))
class DisplayComment(Comment):
def __init__(self, com, u):
self.hours = calc_hours(com)
self.minutes = calc_minutes(com)
# self.comments_number = TODO
self.author = com.author
self.content = com.content
self.ups = com.ups
self.id = (com.key()).id
if (u is None):
self.varw = 0
else:
self.varw = 1 - user_found(u, com.key())
class DisplayComments(list):
def __init__(self, comments, u):
for c in comments:
self.append(DisplayComment(c, u))
class FeedPost(Post):
def __init__(self, post):
self.content = post.content
self.ups = post.ups
self.url = post.url
self.site = post.site
self.fdate = post.date.strftime("%Y-%m-%dT%H:%M:%SZ")
class FeedPosts(list):
def __init__(self, posts):
for post in posts:
self.append(FeedPost(post))