Permalink
Browse files

Refactoring needed for Registry #66

  • Loading branch information...
PaulKinlan committed Nov 20, 2011
1 parent 94189d8 commit debf7a38120403fed51124d909b07b877f2b2a4c
View
@@ -1,10 +1,6 @@
-import webapp2
-import jinja2
-import os
-from google.appengine.api import urlfetch
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the base demo site.
"""
@@ -1,6 +1,6 @@
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the Image Studio app
"""
@@ -1,10 +1,8 @@
import webapp2
-import jinja2
-import os
from google.appengine.api import urlfetch
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the Imgur app
"""
@@ -1,9 +1,8 @@
import webapp2
-import jinja2
from google.appengine.api import urlfetch
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the Instapaper app
"""
@@ -1,11 +1,8 @@
import webapp2
-import jinja2
-import os
from google.appengine.api import urlfetch
+import handlers_base
-import handlers
-
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the Mememator app
"""
@@ -1,19 +1,15 @@
import webapp2
-import jinja2
-import os
import json
from google.appengine.api import urlfetch
-import logging
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The handler for the Shortener app
"""
class ShortenHandler(webapp2.RequestHandler):
def post(self):
- logging.info("in request")
url = "https://www.googleapis.com/urlshortener/v1/url"
headers = { "Content-Type": "application/json" }
payload = { "key": "AIzaSyAl280d28vAJd_6431Uc0N5AwHMowThy8c", "longUrl": "%s" % self.request.get("url") }
@@ -1,6 +1,6 @@
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The base page handler for the examples site
"""
@@ -12,36 +12,43 @@
'.html' : 'text/html',
'.css' : 'text/css',
'.ico' : 'image/x-icon',
- '.png' : 'image/png'
+ '.png' : 'image/png',
+ '.jpg' : 'image/jpeg',
+ '.jpeg' : 'image/jpeg',
+ '.txt' : 'text/plain'
}
-
-
class PageHandler(webapp2.RequestHandler):
- def render_file(self, file):
+ def render_file(self, file, domain):
self.response.headers['X-Content-Security-Policy'] = "allow 'self'; img-src *; script-src www.google-analytics.com apis.google.com;"
-
+ request_path = self.request.path
+
if file is None or file == "":
file = "index.html"
name, extension = os.path.splitext(file)
- content_type = file_types.get(extension, "text/html")
+ content_type = file_types.get(extension)
+ # Only display known file types
+ if content_type is None and extension is not None:
+ self.error(404)
+ return
+
self.response.headers['Content-Type'] = content_type
# test if the file exists in the static
- path = os.path.join(os.path.dirname(__file__), "static", file)
+ path = os.path.join(os.path.dirname(__file__), domain, request_path , "static", file)
if os.path.exists(path):
f = open(path, "r")
self.response.out.write(f.read())
return
- path = os.path.join(os.path.dirname(__file__), "pages", file)
+ path = os.path.join(os.path.dirname(__file__), domain, request_path, "pages", file)
if os.path.exists(path):
- template = jinja_environment.get_template(os.path.join("pages", file))
+ template = jinja_environment.get_template(path)
self.response.out.write(template.render())
else:
self.error(404)
def get(self, file):
- self.render_file(file)
+ self.render_file(file, self.route.name)
@@ -3,7 +3,7 @@
import os
from google.appengine.api import urlfetch
from google.appengine.ext import db
-import handlers
+import handlers_base
class Intent(db.Model):
href = db.StringProperty(indexed = False)
@@ -21,12 +21,12 @@ class IndexHistory(db.Model):
created_on = db.DateTimeProperty(auto_now = False, auto_now_add = True)
status = db.TextProperty()
-class PageHandler(handlers.PageHandler):
- """
- The base page handler for the registry
- """
+class PageHandler(handlers_base.PageHandler):
+ """
+ Basic Page Handler for Registry
+ """
-class IndexerHandler(PageHandler):
+class IndexerHandler(handlers_base.PageHandler):
def post(self):
'''
Creates a new task to go off and fetch the data from the page.
@@ -74,6 +74,6 @@ def get(self):
path = os.path.join(os.path.dirname(__file__), "results", "results.%s" % alt)
if os.path.exists(path):
template = jinja_environment.get_template(os.path.join("results", "results.%s" % alt))
- self.response.out.write(template.render({intents = results}))
+ self.response.out.write(template.render({intents : results}))
else:
self.error(404)
View
@@ -0,0 +1,2 @@
+#!/usr/bin/bash
+dev_appserver.py --admin_console_server= --port=8099 .
View
@@ -2,6 +2,7 @@
from webapp2 import Route
from webapp2_extras import routes
+import handlers_base
import examples.handlers
import demos.handlers
import registry.handlers
@@ -14,20 +15,21 @@
import demos.instapaper.handlers
import demos.imgur.handlers
+
exampleRoutes = [ Route('/<:.*>', examples.handlers.PageHandler, 'examples')]
demoRoutes = [
- Route('/mememator/proxy', demos.mememator.handlers.ProxyHandler, 'mememator-proxy'),
- Route('/mememator/<:.*>', demos.mememator.handlers.PageHandler, 'mememator'),
- Route('/imagestudio/<:.*>', demos.imagestudio.handlers.PageHandler, 'imagestudio'),
- Route('/twitpic/proxy', demos.twitpic.handlers.ProxyHandler, 'twitpic-proxy'),
- Route('/twitpic/upload', demos.twitpic.handlers.UploadHandler, 'twitpic-proxy'),
- Route('/twitpic/<:.*>', demos.twitpic.handlers.PageHandler, 'twitpic'),
- Route('/shortener/shorten', demos.shortener.handlers.ShortenHandler, 'shortener-proxy'),
- Route('/shortener/<:.*>', demos.shortener.handlers.PageHandler, 'shortener'),
- Route('/instapaper/add', demos.instapaper.handlers.AddHandler, 'instapaper-proxy'),
- Route('/instapaper/<:.*>', demos.instapaper.handlers.PageHandler, 'instapaper'),
- Route('/imgur/save', demos.imgur.handlers.SaveHandler, 'imgur-proxy'),
- Route('/imgur/<:.*>', demos.imgur.handlers.PageHandler, 'imgur'),
+ Route('/mememator/proxy', demos.mememator.handlers.ProxyHandler, 'demos'),
+ Route('/mememator/<:.*>', demos.mememator.handlers.PageHandler, 'demos'),
+ Route('/imagestudio/<:.*>', demos.imagestudio.handlers.PageHandler, 'demos'),
+ Route('/twitpic/proxy', demos.twitpic.handlers.ProxyHandler, 'demos'),
+ Route('/twitpic/upload', demos.twitpic.handlers.UploadHandler, 'demos'),
+ Route('/twitpic/<:.*>', demos.twitpic.handlers.PageHandler, 'demos'),
+ Route('/shortener/shorten', demos.shortener.handlers.ShortenHandler, 'demos'),
+ Route('/shortener/<:.*>', demos.shortener.handlers.PageHandler, 'demos'),
+ Route('/instapaper/add', demos.instapaper.handlers.AddHandler, 'demos'),
+ Route('/instapaper/<:.*>', demos.instapaper.handlers.PageHandler, 'demos'),
+ Route('/imgur/save', demos.imgur.handlers.SaveHandler, 'demos'),
+ Route('/imgur/<:.*>', demos.imgur.handlers.PageHandler, 'demos'),
Route('/<:.*>', demos.handlers.PageHandler, 'demos')
]
@@ -46,5 +48,3 @@
Route('/<:.*>', widgets.handlers.PageHandler, 'widgets')
])
])
-
-
@@ -2,9 +2,9 @@
import jinja2
import os
from google.appengine.api import urlfetch
-import handlers
+import handlers_base
-class PageHandler(handlers.PageHandler):
+class PageHandler(handlers_base.PageHandler):
"""
The main page handler for the server
"""

0 comments on commit debf7a3

Please sign in to comment.