Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 2bf4573885
Fetching contributors…

Cannot retrieve contributors at this time

182 lines (154 sloc) 5.85 KB
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)
visibility = db.StringProperty()
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
urlcode = db.StringProperty(required=True)
#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)
def remove_choice(self,choice):
def vote_count(self):
return self.votes.count(999999)
def vote_for_member(self,member=None):
if not member:
member = users.get_current_user()'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 self.has_results:
raise Exception('Cannot vote in closed polls.')
if not member:
member = users.get_current_user()
member_vote = self.vote_for_member(member)
was_changed = False
member_vote.choice = choice
was_changed = True
member_vote = Vote(member=member,choice=choice,issue=self)
if(not self.start_time):
self.start_time =
self.end_time = self.start_time + timedelta(hours=self.duration)
return was_changed
def extend_duration(self,hours):
if hours <= 0:
raise Exception('Negative extensions are not valid.')
if self.has_results:
raise Exception('Cannot extend closed polls.')
self.duration += hours
if self.start_time:
self.end_time = self.start_time + timedelta(hours=self.duration)
def days_left(self):
delta = self.end_time -
return delta.days
def hours_left(self):
delta = self.end_time -
#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:
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 <='status changed for issue: %s' % (self.title))
self.status = 'done'
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)
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)'member_votes:%s' % (member_votes))'output:%s' % ([vote.issue for vote in member_votes]))
return [vote.issue for vote in member_votes]
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()]
def get_issue_by_urlcode(cls, urlcode):
return cls.all().filter('urlcode =',urlcode).get()
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():"Yup member:%s (%s)" % (member.nickname(),users.get_current_user()))
return True"Nope")
return False
def vote_count(self):
return self.votes.count(999999)
def percentage(self):
total_votes = float(self.issue.vote_count())
return round(float(self.vote_count()) / total_votes,3) * 100 #cnt/total
return 0
def is_winning(self):'%s in result(s) %s: %s',,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)
Jump to Line
Something went wrong with that request. Please try again.