-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
51 lines (42 loc) · 1.61 KB
/
log.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
import os
import cgi
import datetime
import re
import models
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
QUERY_CGI_PARAM = "q"
USER_ID_PARAM = "u"
ADD_FN = "addNewEntry"
class LogHandler(webapp2.RequestHandler):
def getExerciseName(self, query):
words = [s for s in query.split() if not s.isdigit()]
return words[0]
def getExercise(self, query):
return models.exercises[self.getExerciseName(query)]
def getNumber(self, query):
numbers = [int(s) for s in query.split() if s.isdigit()]
return numbers[0]
def get(self):
user_id = self.request.get(USER_ID_PARAM)
parent_key = db.Key.from_path("User", user_id)
contest = models.Contest.all().ancestor(parent_key).get()
nickname = contest.user.nickname()
entry = models.Entry(parent=parent_key)
query = self.request.get(QUERY_CGI_PARAM)
exercise = self.getExercise(query)
entry.date = datetime.datetime.now()
entry.reps = self.getNumber(query)
entry.points = exercise[2] * entry.reps
entry.name = exercise[0]
entry.put()
if user_id:
# Render JSONP Response
self.response.headers['Content-Type'] = 'applicaton/javascript'
self.response.out.write(ADD_FN + '(' + str(entry.points) + ',"' + entry.name + '", "' + nickname + '", "' + str(entry.key()) + '")')
app = webapp2.WSGIApplication([
('/log', LogHandler)
], debug=True)