forked from dothiv/clickcounter-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.py
143 lines (107 loc) · 4.26 KB
/
handlers.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
# app handlers
import webapp2
from settings import JINJA_ENVIRONMENT
from models import Domain, StaticFile, UserData
from decorators import basic_auth
def get_domain_or_404(name, allow_none=False):
"""Gets a domain entity for the given name.
Aborts with 404 if entity doesn't exist and if that is not allowed.
"""
if not name:
webapp2.abort(404)
domain = Domain.query(Domain.name==name).get()
if not domain and not allow_none:
webapp2.abort(404)
return domain
class Index(webapp2.RequestHandler):
"""
Handler Index
Implements GET only and renders a simple page template
"""
def get(self):
template = JINJA_ENVIRONMENT.get_template('page.html')
self.response.write(template.render())
class Config(webapp2.RequestHandler):
"""
Handler Config
GET: show a domain's configuration as JSON (text/plain)
POST: edit or create a domain's configuration, responds with 204
DELETE: delete a domain's configuration, responds with 204
All HTTP methods are protected by HTTP Basic Auth.
"""
@basic_auth
def get(self, domain_name):
domain = get_domain_or_404(domain_name)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(domain.get_json())
@basic_auth
def post(self, domain_name):
domain = get_domain_or_404(domain_name, allow_none=True)
if not domain:
domain = Domain(name=domain_name, clickcount=0, money=0., status=0.)
# example content:
# "firstvisit":"center","secondvisit":"center","heading":"Vielen Dank!","subheading":"Dein Klick auf domain.hiv hat soeben einen Gegenwert von 1 ct ausgelöst.","claim":"Wir sind Teil der Bewegung","about":"Über dotHIV","vote":"Vote","activated":"Bisher aktiviert:","currency":"€","corresponding":"entspricht","clicks":"Klicks"
domain.content = self.request.body
domain.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.set_status(204)
@basic_auth
def delete(self, domain_name):
domain = Domain.query(Domain.name==domain_name).get()
if domain:
domain.key.delete()
self.response.headers['Content-Type'] = 'text/plain'
self.response.set_status(204)
class Count(webapp2.RequestHandler):
"""
Handler Count
POST: increment click count if certain parameters are valid
"""
def post(self):
params = self.request.params
if not 'domain' in params:
self.abort(404)
domain = get_domain_or_404(params['domain'])
if 'from' in params and 'firstvisit' in params:
if params['from'] == 'inside' and params['firstvisit'] == 'true':
# maybe use a transaction for these two independent database
# operations? maybe not: it doesn't really matter if an entity of
# UserData could be put while Domain failed or vice versa.
domain.increment_counter()
UserData.add(self.request, params['domain'])
# explicit request to have content-type application/json
self.response.headers['Content-Type'] = 'application/json'
self.response.write(domain.get_json())
class StaticServe(webapp2.RequestHandler):
@basic_auth
def post(self, file_name):
static_file = StaticFile.query(StaticFile.name==file_name).get()
if not static_file:
static_file = StaticFile(name=file_name)
static_file.content = self.request.body
static_file.content_type = self.get_content_type(
self.request.headers, file_name)
static_file.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.set_status(204)
def get(self, file_name):
static_file = StaticFile.query(StaticFile.name==file_name).get()
if not static_file:
self.abort(404)
else:
self.response.write(static_file.content)
self.response.set_status(200)
self.response.headers['Content-Type'] = str(static_file.content_type)
self.response.headers['Access-Control-Allow-Origin'] = '*'
def get_content_type(self, headers, file_name):
if 'Content-Type' in headers:
content_type = headers['Content-Type']
else:
file_extension = file_name.split('.')[-1]
if file_extension == 'js':
content_type = 'application/javascript'
elif file_extension == 'html':
content_type = 'text/html'
else:
content_type = 'text/plain'
return content_type