Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (114 sloc) 4.303 kb
a533f0b @dustball first commit
dustball authored
1 #!/usr/bin/env python
2
3 import hashlib, random
4 from google.appengine.ext import webapp
5 from google.appengine.ext.webapp import util
6 from google.appengine.ext.webapp import template
7 from google.appengine.ext import db
8 from django.utils import simplejson
9 from google.appengine.api import users
10 from datetime import datetime
11
12 METRICS = ["Overall","Cleanliness","Value","Community"]
13 GRADES = {'A':100.0,'B':90.0,'C':80.0,'D':70.0,'F':60.0}
14 REVSERSE_GRADES = dict((v,k) for k, v in GRADES.iteritems())
15
16 class SatisfactionMetric(db.Model):
17 metric = db.StringProperty()
18 grade = db.FloatProperty()
19 who = db.UserProperty()
20 when = db.DateTimeProperty(auto_now=True)
21
22
23 class MainHandler(webapp.RequestHandler):
24 def get(self):
25 user = users.get_current_user()
26 if not user:
27 self.redirect(users.create_login_url('/'))
28 else:
29 survey = []
30 for metric in METRICS:
31 survey_item = {}
32 survey_item['metric'] = metric
33 survey_item['grade'] = None
34 existing_metric = None
35 existing_metric_all_months = SatisfactionMetric.all().filter('who =', user).filter('metric =', metric).fetch(500)
36 for m in existing_metric_all_months:
37 if m.when.month == datetime.now().month and m.when.year == datetime.now().year:
38 existing_metric = m
39 if existing_metric:
40 survey_item['grade'] = REVSERSE_GRADES[existing_metric.grade]
41 survey.append(survey_item)
42 grades = sorted(GRADES.keys())
43 self.response.out.write(template.render('templates/main.html', locals()))
44
45 def post(self):
46 user = users.get_current_user()
47 if not user:
48 self.redirect(users.create_login_url('/'))
49 else:
50 for metric in METRICS:
51 try:
52 grade = GRADES[self.request.get(metric)]
53 sm = None
54 existing_metric_all_months = SatisfactionMetric.all().filter('who =', user).filter('metric =', metric).fetch(500)
55 for m in existing_metric_all_months:
56 if m.when.month == datetime.now().month and m.when.year == datetime.now().year:
57 sm = m
58 if not sm:
59 sm = SatisfactionMetric(metric=metric,who=user)
60 sm.grade = grade
61 sm.put()
62 except KeyError, e:
63 existing_metric_all_months = SatisfactionMetric.all().filter('who =', user).filter('metric =', metric).fetch(500)
64 for m in existing_metric_all_months:
65 if m.when.month == datetime.now().month and m.when.year == datetime.now().year:
66 m.delete()
67 self.response.out.write(template.render('templates/thanks.html', locals()))
68
69 # Sample Output:
70 #
71 # [
72 # {
73 # "grade":90.0,
74 # "metric":"General",
75 # "who":"96f81e4886",
76 # "exact_time":"01-20-2012 00:56:30",
77 # "month":"2012-01"
78 # },
79
80 class AllDataHandler(webapp.RequestHandler):
81 def get(self):
82 seed = str(random.random())
83
84 def to_dict(data):
85 m = hashlib.md5()
86 m.update(data.who.email())
87 m.update(seed)
88
89 return dict(
90 metric=data.metric,
91 grade=data.grade,
92 who=m.hexdigest()[0:10],
ebc47a0 @dustball Stats tweeks & smaller BG
dustball authored
93 month=data.when.strftime("%Y-%m"),
94 exact_time=data.when.strftime("%m-%d-%Y %H:%M:%S"),)
a533f0b @dustball first commit
dustball authored
95
96 self.response.out.write(simplejson.dumps([to_dict(data) for data in SatisfactionMetric.all()]))
97
ebc47a0 @dustball Stats tweeks & smaller BG
dustball authored
98 # How many people answered each month
99 #
100 # Sample:
101 #
102 # {"2012-01": 12}
103
104 class BasicDataHandler(webapp.RequestHandler):
105 def get(self):
106
107 monthly_count = {}
108 monthly_users = {}
109
110 for sm in SatisfactionMetric.all():
111 month = sm.when.strftime("%Y-%m")
112 user = sm.who.email()
113 if month in monthly_count:
114 if user not in monthly_users[month]:
115 monthly_count[month] += 1
116 monthly_users[month][user] = 1
117 else:
118 monthly_count[month] = 0
119 monthly_users[month] = {}
120
121 self.response.out.write(simplejson.dumps(monthly_count))
122
a533f0b @dustball first commit
dustball authored
123
124 def main():
125 application = webapp.WSGIApplication([
126 ('/', MainHandler),
127 ('/api/all', AllDataHandler),
ebc47a0 @dustball Stats tweeks & smaller BG
dustball authored
128 ('/api/basic', BasicDataHandler),
a533f0b @dustball first commit
dustball authored
129 ],debug=True)
130 util.run_wsgi_app(application)
131
132
133 if __name__ == '__main__':
134 main()
Something went wrong with that request. Please try again.