-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodels.py
172 lines (141 loc) · 5.4 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
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
from google.appengine.ext import db
from google.appengine.api import urlfetch, memcache, users, mail
from datetime import datetime, timedelta
import logging
class Issue(db.Model):
"""Represents a single issue which is being voted on"""
status = db.StringProperty(required=True, default='active', choices=set(
['active', 'done', 'canceled']))
is_public = db.BooleanProperty(default=False) #is issue listed on site or just by sharing url?
creator = db.UserProperty(auto_current_user=True)
title = db.StringProperty(required=True)
description = db.TextProperty()
duration = db.IntegerProperty()
creation_date = db.DateTimeProperty(auto_now_add=True)
start_time = db.DateTimeProperty() #time when first vote is cast
end_time = db.DateTimeProperty() #time when vote will end
#Implicit Properties:
#choices = Implicitly created list of choice objects
#votes - implicitly created list of vote objects
def add_choice(self,choice_name):
new_choice = Choice(name=choice_name,issue=self)
new_choice.put()
def remove_choice(self,choice):
choice.delete()
def vote_count(self):
return self.votes.count(999999)
def vote_for_member(self,member=None):
if not member:
member = users.get_current_user()
logging.info('member:%s voted:%s' % (member.nickname(),self.votes.filter('member =',member).fetch(20)))
return self.votes.filter('member =',member).get()
def register_vote(self,choice,member=None):
if not member:
member = users.get_current_user()
member_vote = self.vote_for_member(member)
was_changed = False
if(member_vote):
member_vote.choice = choice
was_changed = True
else:
member_vote = Vote(member=member,choice=choice,issue=self)
member_vote.put()
if(not self.start_time):
self.start_time = datetime.now()
self.end_time = self.start_time + timedelta(hours=self.duration)
self.put()
return was_changed
def extend_duration(self,hours):
self.duration += hours
if self.start_time:
self.end_time = self.start_time + timedelta(hours=self.duration)
self.put()
def days_left(self):
delta = self.end_time - datetime.now()
return delta.days
def hours_left(self):
delta = self.end_time - datetime.now()
#days = delta.days
#hours = days*24 + delta.seconds/3600
hours = delta.seconds/3600
return hours
def is_active(self):
return self.status in ('active')
def has_results(self):
return self.status in ('done')
def member_is_creator(self,member=None):
if not member:
member = users.get_current_user()
return member == self.creator
def winning_choices(self):#returns list of keys of winning choices (may be a tie)
result = []
high_vote = 0
for choice in self.choices:
cnt = choice.vote_count()
if cnt == high_vote:
result.append(choice.key())
elif cnt > high_vote:
result = [choice.key()]
high_vote = cnt
return result
def update_status(self):
if self.is_active:
if self.end_time:
if self.end_time <= datetime.now():
logging.info('status changed for issue: %s' % (self.title))
self.status = 'done'
self.put()
@classmethod
def issues_created_by(cls, member=None,limit=20):
if not member:
member = users.get_current_user()
return cls.all().filter('creator =',member).order('-creation_date').fetch(limit)
@classmethod
def issues_voted_on(cls, member=None, limit=20):
if not member:
member = users.get_current_user()
if not member:#if logged out
return []
member_votes = Vote.all().filter('member =',member).order('-update_time').fetch(limit)
##logging.info('member_votes:%s' % (member_votes))
##logging.info('output:%s' % ([vote.issue for vote in member_votes]))
return [vote.issue for vote in member_votes]
@classmethod
def recent_results(cls, member=None,limit=20):#*** Need to fix, limit will be incorrect here because of filtering
if not member:
member = users.get_current_user()
if not member:#if logged out
return []
recent = cls.all().filter('status =','done').order('-end_time').fetch(limit)
return [issue for issue in recent if issue.vote_for_member()] #***this is probably slow
#member_votes = Vote.all().filter('member =',member).fetch(limit)
#return [vote.issue for vote in member_votes if vote.issue.has_results()]
class Choice(db.Model):
"""Represents a possible response to an issue (e.g. Yes)"""
name = db.StringProperty(required=True)
issue = db.ReferenceProperty(Issue,collection_name='choices')
#votes - implicitly created list of vote objects
def is_member_vote(self,member=None):
if not member:
member = users.get_current_user()
if self.votes.filter('member =',member).get():
logging.info("Yup member:%s (%s)" % (member.nickname(),users.get_current_user()))
return True
logging.info("Nope")
return False
def vote_count(self):
return self.votes.count(999999)
def percentage(self):
total_votes = float(self.issue.vote_count())
if(total_votes):
return round(float(self.vote_count()) / total_votes,3) * 100 #cnt/total
return 0
def is_winning(self):
logging.info('%s in result(s) %s: %s',self.name,self.issue.winning_choices()[0].name, self.issue.winning_choices())
return self.key() in self.issue.winning_choices()
class Vote(db.Model):
"""Represents a single vote by a member"""
member = db.UserProperty(auto_current_user=True)
choice = db.ReferenceProperty(Choice, collection_name='votes')
issue = db.ReferenceProperty(Issue,collection_name='votes')
update_time = db.DateTimeProperty(auto_now=True)