-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·179 lines (158 loc) · 6.22 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from google.appengine.api import images
from google.appengine.api import urlfetch
from models import Puzzle, Piece, Location, Image
logging.getLogger().setLevel(logging.DEBUG)
config = {"SITE_NAME": "HTML5 Hacks"}
IS_DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev') # Development server
logging.debug("IS_DEV: %s" % IS_DEV)
class MainHandler(webapp.RequestHandler):
def get(self):
template_values = {
"title": "HTML5 Hacks index",
"is_dev": IS_DEV,
"config": config
}
path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
self.response.out.write(template.render(path, template_values))
class SvgHandler(webapp.RequestHandler):
def get_pieces(self):
pieces = memcache.get("pieces")
if pieces is not None:
return pieces
else:
pieces = Piece.gql("ORDER BY address ASC")
if not memcache.add("pieces", pieces, 10):
logging.error("Memcache set failed.")
return pieces
def get(self):
puzzles = Puzzle.gql("ORDER BY name ASC")
pieces = self.get_pieces()
locations = Location.gql("ORDER BY x ASC")
svg_path = os.path.join(os.path.dirname(__file__), 'templates/puzzle.svg')
svg_values = {
"pieces": pieces,
"locations": locations,
"width": 641,
"height": 481
}
svg_text = template.render(svg_path, svg_values)
try:
first_puzzle = puzzles[0]
except IndexError:
first_puzzle = "None"
template_values = {
"title": "HTML5 Hacks: SVG",
"puzzles": puzzles,
"first_puzzle": first_puzzle,
"host": os.environ["HTTP_HOST"],
"config": config,
"svg_text": svg_text,
"is_dev": IS_DEV,
"rows": ["A", "B", "C", "D", "E", "F", "G", "H"],
"cols": ["1", "2", "3", "4", "5", "6", "7", "8"],
}
path = os.path.join(os.path.dirname(__file__), 'templates/svg.html')
self.response.out.write(template.render(path, template_values))
class ImageHandler(webapp.RequestHandler):
def get(self):
puzzle = Puzzle.get(self.request.get("img_id"))
thumb = self.request.get("thumb")
if puzzle.image:
self.response.headers['Content-Type'] = "image/png"
if thumb and thumb == "1":
self.response.out.write(puzzle.image.thumb_nail)
else:
self.response.out.write(puzzle.image.full_size)
else:
self.error(404)
class NewPuzzleHandler(webapp.RequestHandler):
def post(self):
url = self.request.get("img_url")
if url:
inputimage = urlfetch.fetch(url)
if inputimage.status_code == 200:
image = Image()
image.source_url = url
image.full_size = inputimage.content
image.title = "Test image"
# TODO move to task queue
image.thumb_nail = images.resize(image.full_size, 64, 64)
image.put()
puzzle = Puzzle()
puzzle.name = "Test puzzle"
puzzle.image = image
puzzle.put()
self.redirect("/svg")
class CanvasHandler(webapp.RequestHandler):
def get(self):
template_values = {
"title": "HTML5 Hacks: canvas",
"is_dev": IS_DEV,
"config": config
}
path = os.path.join(os.path.dirname(__file__), 'templates/canvas.html')
self.response.out.write(template.render(path, template_values))
class WebGLHandler(webapp.RequestHandler):
def get(self):
template_values = {
"title": "HTML5 Hacks: WebGL",
"is_dev": IS_DEV,
"config": config
}
path = os.path.join(os.path.dirname(__file__), 'templates/webgl.html')
self.response.out.write(template.render(path, template_values))
class CSS3Handler(webapp.RequestHandler):
def get(self):
template_values = {
"title": "HTML5 Hacks: CSS3",
"is_dev": IS_DEV,
"config": config
}
path = os.path.join(os.path.dirname(__file__), 'templates/css3.html')
self.response.out.write(template.render(path, template_values))
class VideoHandler(webapp.RequestHandler):
def get(self):
template_values = {
"title": "HTML5 Hacks: Video",
"is_dev": IS_DEV,
"config": config
}
path = os.path.join(os.path.dirname(__file__), 'templates/video.html')
self.response.out.write(template.render(path, template_values))
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/svg', SvgHandler),
('/canvas', CanvasHandler),
('/webgl', WebGLHandler),
('/video', VideoHandler),
('/css3', CSS3Handler),
# helper urls
('/new_puzzle', NewPuzzleHandler),
('/img', ImageHandler),
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()