Created
August 17, 2014 00:25
-
-
Save megafauna/0e0de51a6dfd8e9b7ad6 to your computer and use it in GitHub Desktop.
Math Artist App Engine code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import webapp2 | |
import os, os.path | |
import json | |
import logging | |
import time | |
import random | |
from google.appengine.ext.webapp import template, util | |
from google.appengine.ext import ndb | |
ltrs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
example_b64 = "UEsDBAoAAAAAAKh5DUV/2aO/DQEAAA0BAAAHAAAAemlwZmlsZWZvciAodmFyIHk9MDsgeTxtYS5IOyB5Kz0xKSB7CiAgZm9yICh2YXIgeD0wOyB4PG1hLlc7IHgrPTEpIHsKICAgIG1hLmltZ2Fyclt5XVt4XVswXSA9IE1hdGguY29zKC4wMDAxKiB5ICogeCkgJSAxOwogICAgbWEuaW1nYXJyW3ldW3hdWzFdID0gTWF0aC5jb3MoLjAwMDEqIHkgKiAobWEuVy14KSkgJSAxOwogICAgbWEuaW1nYXJyW3ldW3hdWzJdID0gTWF0aC5jb3MoLjAwMDEqIChtYS5ILXkpICogeCkgJSAxOwogICAgbWEuaW1nYXJyW3ldW3hdWzNdID0gMTsKICB9Cn0KUEsBAhQACgAAAAAAqHkNRX/Zo78NAQAADQEAAAcAAAAAAAAAAAAAAAAAAAAAAHppcGZpbGVQSwUGAAAAAAEAAQA1AAAAMgEAAAAA" | |
max_images_gallery = 128 | |
max_b64_length = 4096 | |
IS_LOCAL = False | |
if os.environ.get('SERVER_SOFTWARE','').startswith('Development'): | |
IS_LOCAL = True | |
logging.debug("[*] Local server info activated") | |
server = "http://localhost:10080" if IS_LOCAL else "http://math-artist.appspot.com" | |
class Image(ndb.Model): | |
date = ndb.DateTimeProperty(auto_now_add=True, indexed=False) | |
meta = ndb.TextProperty(indexed=False, default='') | |
b64 = ndb.TextProperty(indexed=False, default='') | |
class SaveHandler(webapp2.RequestHandler): | |
def post(self): | |
print("save\n", self.response) | |
meta = self.request.get("meta") | |
b64 = self.request.get("b64") | |
print("meta", meta, b64, "end") | |
if len(b64) > max_b64_length: | |
self.response.headers.add_header('Content-Type', 'application/json') | |
r = {"status_code":400, "error":"Your code is too long!"} | |
self.response.write(json.dumps(r)) | |
try: | |
json.loads(meta) | |
except ValueError: | |
self.response.headers.add_header('Content-Type', 'application/json') | |
r = {"status_code":400, "error":"Error with uploaded parameters!"} | |
self.response.write(json.dumps(r)) | |
if len(b64) == 0 or len(meta) == 0: | |
self.response.headers.add_header('Content-Type', 'application/json') | |
r = {"status_code":400, "error":"Error with uploaded parameters!"} | |
self.response.write(json.dumps(r)) | |
for _ in range(3): | |
random_id = ''.join(random.choice(ltrs) for _ in range(9)) | |
imk = ndb.Key(Image, random_id) | |
img = imk.get() | |
if img is None: | |
img = Image(id=random_id, meta=meta, b64=b64) | |
img.put() | |
break | |
self.response.headers.add_header('Content-Type', 'application/json') | |
r = {"status_code":200, "id":random_id} | |
self.response.write(json.dumps(r)) | |
class MainHandler(webapp2.RequestHandler): | |
def get(self, resolution=None, url_image_ids=None): | |
image_width = 640 | |
image_height = 480 | |
mode = "edit" | |
b64_codes = [] | |
titles = [] | |
if resolution is not None: # then we are in gallery mode | |
mode = "gallery" | |
image_width, image_height = [int(i) for i in resolution.split('x')] | |
if url_image_ids in (None, '', ' ', ','): | |
b64_codes = [example_b64] | |
else: | |
image_ids = [i.strip() for i in url_image_ids.split(',')][:max_images_gallery] | |
image_keys = [ndb.Key(Image, image_id) for image_id in image_ids] | |
images = ndb.get_multi(image_keys) | |
titles = [json.loads(image.meta)["title"] for image in images if image is not None] | |
b64_codes = [image.b64 for image in images if image is not None] | |
print("image", image_ids, image_keys, images) | |
print("b64", b64_codes) | |
print("titles", titles, json.dumps(titles)) | |
path = os.path.join(os.path.dirname(__file__), "index.html") | |
print("path", path) | |
index = template.render(path, {"server":server, "mode":mode, | |
"image_width":image_width, "image_height":image_height, "titles":json.dumps(titles), | |
"max_b64_length":max_b64_length, "b64_codes":json.dumps(b64_codes)}) | |
self.response.out.write(index) | |
app = webapp2.WSGIApplication([ | |
('/(\d+x\d+)/(.*)', MainHandler), | |
('/save', SaveHandler), | |
('/', MainHandler) | |
], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment