-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (53 loc) · 2.14 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
import jinja2
import os
import webapp2
import datetime
from google.appengine.ext import ndb
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Error(Exception):
"""Generic error class."""
class RapidVoterError(Error):
"""Raised when someone votes too quickly."""
class Vote(ndb.Model):
source_ip = ndb.StringProperty() # source IP
timestamp = ndb.DateTimeProperty(auto_now_add=True)
class Dodger(ndb.Model):
"""The ID should be the username."""
votes = ndb.StructuredProperty(Vote, repeated=True)
count = ndb.ComputedProperty(lambda self: len(self.votes))
@classmethod
def Increment(cls, dodger_name, source_ip):
limit = datetime.datetime.now() - datetime.timedelta(days=1)
dodger_name = dodger_name.lower()
dodger = cls.get_by_id(dodger_name) or cls(id=dodger_name)
for vote in dodger.votes:
if vote.source_ip == source_ip and vote.timestamp > limit:
raise RapidVoterError('You must wait: %s' % (vote.timestamp-limit))
dodger.votes.append(Vote(source_ip=source_ip))
dodger.put()
return dodger.count
class DDHandler(webapp2.RequestHandler):
def RenderTemplate(self, filename, vars):
template = jinja_environment.get_template(filename)
self.response.out.write(template.render(vars))
class MainPageHandler(DDHandler):
def get(self):
self.RenderTemplate('templates/home.html', {'got_it': 'a dog!'})
class IncrementHandler(DDHandler):
def get(self):
dodger_name = self.request.get('username')
try:
count = Dodger.Increment(dodger_name, self.request.remote_addr)
self.response.out.write(count)
except RapidVoterError as err:
self.response.out.write(err)
class DirtyDodgersHandler(DDHandler):
def get(self):
results = Dodger.query().order(-Dodger.count).fetch(limit=10)
for dodger in results:
self.response.out.write(str(dodger.count) + ' ' + dodger.key.id() + '<br>')
app = webapp2.WSGIApplication([('/', MainPageHandler),
('/add', IncrementHandler),
('/dodgers', DirtyDodgersHandler)],
debug=True)