-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
134 lines (92 loc) · 4.06 KB
/
models.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
from google.appengine.ext import db
#from google.appengine.api import users
import random
import logging
import datetime
class Paper(db.Model):
title = db.StringProperty(required=True, multiline=False)
description = db.TextProperty()
tags = db.StringListProperty()
def spaced_tags(self, spacer=', '):
return spacer.join(self.tags)
date = db.DateTimeProperty(auto_now_add=True)
# reference for future properties
#creator = db.UserProperty()
#author(s).. = db.ReferenceProperty(Hat)
points = db.IntegerProperty(default=0)
# TODO: points should track who gave points so each user can only add one point
# eg. a Point model w/ references to Paper & User objects
link = db.LinkProperty() # for fully qualified links
pdf_link = db.LinkProperty()
# Links aren't required. We support linking to papers or just discussing
# them or some part of them submitted via the description.
#To be auto-filled:
#name = db.StringProperty()
#abstract = db.TextProperty()
#references = ..?
#keywords = db.StringListProperty()
#authors = ...? (add Author model?)
def permalink(self):
return "/papers/%(id)s/" % {'id' : self.key().id()}
def age_in_hours(self):
delta = datetime.datetime.now() - self.date
return delta.days * 24 + delta.seconds / 3600
def score(self):
return int((self.points - 1) / (self.age_in_hours() + 2) ** 1.5)
@classmethod
def get_random(cls):
papers = Paper.all().fetch(1000)
if not papers:
logging.info('random paper returning None')
return None
else:
return random.choice(papers)
@classmethod
def sample_init(debug=False):
import datetime
import time
import string
fp = open('english_dict.txt')
words = fp.read().split('\n')
def strTimeProp(start, end, format, prop):
# from http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return datetime.datetime.fromtimestamp(ptime)#.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%m/%d/%Y %I:%M %p', prop)
def random_text(min, max, sentences=False, join=' '):
txt = ""
tlen = random.randint(min, max)
for x in range(tlen):
txt += random.choice(words)
if sentences and random.randint(0,25) == 0:
txt += '. '
else:
txt += join
if sentences:
txt += '.'
return txt
def add_paper():#title, desc, tags, date, points, link="", pdf_link=""):
title = string.capwords(random_text(5,12))
desc = string.capwords(random_text(0,100,True), sep='. ')
tags = filter(lambda x: x, random_text(0,8).split(' '))
date = randomDate("2/14/2009 1:30 PM", "2/28/2009 6:45 PM", random.random())
points = random.randint(0,500)
link = None
if random.randint(0,10) < 8:
link = 'http://'+random_text(1,1,join='')+'.com/'+random_text(0,5,join='/')
pdf_link = None
if random.randint(0,4) == 0:
pdf_link = 'http://'+random_text(1,1,join='')+'.com/'+random_text(0,5,join='/')+random_text(1,1,join='')+'.pdf'
if debug:
print "%s\n%s\n%s\n%s\n%s\n%s\n%s\n" % (title, desc, tags, date, points, link, pdf_link)
p = Paper(title=title, description=desc, tags=tags, date=date,
points=points, link=link, pdf_link=pdf_link)
p.put()
for i in range(random.randint(5,30)):
add_paper()
def init():
logging.info('initializing db')
# nothing to do yet..