-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
164 lines (126 loc) · 5.27 KB
/
main.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
import webapp2
import jinja2
import os
import random
import imagegen
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
from google.appengine.ext import db
class Vote(db.Model):
"""Individual Votes, dated and ID'd as much as poss"""
date = db.DateTimeProperty(auto_now_add=True)
class Option(db.Model):
"""Options to select"""
name = db.StringProperty()
voteCount = db.IntegerProperty()
colour = db.IntegerProperty()
image = db.BlobProperty()
class Question(db.Model):
"""Questions to ask"""
name = db.StringProperty()
voteCount = db.IntegerProperty()
closingDate = db.DateTimeProperty()
startDate = db.DateTimeProperty(auto_now_add=True)
textWidth = db.IntegerProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
questions_options = []
question_query = Question.all().order('-startDate')
questions = question_query.fetch(10)
for q in questions:
option_query = db.GqlQuery("SELECT voteCount "
"FROM Option "
"WHERE ANCESTOR IS :1 "
"LIMIT 20",
q.key())
option_query.run()
option_list = []
for option in option_query:
option_list.append('<a href="/vote/?q='
+ str(q.key().id()) +
'&v=' + str(option.key().id()) +
'"><img src="/img/'
+ str(q.key().id()) +
'/' + str(option.key().id())
+ '.png" /></a> - '
+ str(option.voteCount))
questions_options.append((q.name, option_list))
template_values = {
'questions': questions_options
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class CastVote(webapp2.RequestHandler):
def get(self):
"""pass me the db id of the question then option"""
q_id = self.request.get('q')
option_id = self.request.get('v')
q = Question.get_by_id(int(q_id))
option = Option.get_by_id(int(option_id), q)
vote = Vote(parent=option)
vote.put()
option.voteCount += 1
option.put()
q.voteCount += 1
"""Update graph(s)"""
width = q.textWidth
questionCount = float(q.voteCount)
q.put()
option_query = db.GqlQuery("SELECT * "
"FROM Option "
"WHERE ANCESTOR IS :1 "
"LIMIT 20",
q.key())
option_query.run()
for o in option_query:
colour = o.colour
text = o.name
optionCount = float(o.voteCount)
ratio = int((optionCount / questionCount)*100)
o.image = db.Blob(imagegen.create(width, colour, text, ratio))
o.put()
"""Save to the datastore!"""
q.put()
"""Display a page for that poll if from this host, else send back"""
self.redirect('/')
class LoadImage(webapp2.RequestHandler):
def get(self):
path = self.request.path
if path.endswith('.png') or path.endswith('.PNG'):
_, q_id, option_id = path.rsplit('/', 2)
q = Question.get_by_id(int(q_id))
option = Option.get_by_id(int(option_id.lower().rstrip('.png')), q)
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(option.image)
return
class Create(webapp2.RequestHandler):
def get(self):
template_values = {}
template = jinja_environment.get_template('new.html')
self.response.out.write(template.render(template_values))
class Create2(webapp2.RequestHandler):
def post(self):
poll = Question()
poll.name = self.request.get('question')
poll.voteCount = 0
options = self.request.get_all('option')
width = 0
for o in options:
tmpWidth = imagegen.width(o)
if tmpWidth > width:
width = tmpWidth
poll.textWidth = width
poll.put()
for o in options:
option = Option(parent=poll.key())
option.name = o
option.voteCount = 0
option.colour = random.randrange(0,255)
option.image = db.Blob(imagegen.create(width, option.colour, o, 0))
option.put()
self.redirect('/')
app = webapp2.WSGIApplication([('/', MainPage),
('/newpoll', Create2),
('/new', Create),
('/vote/', CastVote),
(r'\/img/[0-9]+/[0-9]+.png', LoadImage)],
debug=True)