Permalink
Browse files

Initial commit of cleaned-up base site

  • Loading branch information...
0 parents commit 36bd5a6f06da899361c3555253fc4415f8e3d57e @bricef committed Apr 19, 2012
Showing with 3,871 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +87 −0 app.py
  3. +9 −0 config.ini
  4. +16 −0 config.py
  5. +14 −0 database.py
  6. +54 −0 jsonapi.py
  7. +3,207 −0 modules/bottle.py
  8. +10 −0 modules/pystache/__init__.py
  9. +47 −0 modules/pystache/loader.py
  10. +176 −0 modules/pystache/template.py
  11. +94 −0 modules/pystache/view.py
  12. +97 −0 sitetools.py
  13. +5 −0 templates/default.html
  14. +4 −0 templates/noauth.html
  15. +50 −0 users.py
@@ -0,0 +1 @@
+*.pyc
87 app.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+
+import os
+import sys
+mypath = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0,mypath)
+sys.path.insert(0,os.path.join(mypath, "modules"))
+os.chdir(mypath)
+
+# Standard lib modules
+import argparse
+
+# Third party modules
+import bottle
+from bottle import route, run, request, response, get, post, redirect, abort, static_file, error, mount
+
+# My modules
+from sitetools import *
+from config import config
+
+
+
+#########################
+#
+# Configuration
+#
+#########################
+
+bottle.debug(True)
+
+###############################
+#
+# Route Handlers
+#
+###############################
+
+@route("/")
+def home():
+ return renderfile("root", {})
+
+
+@route("/fail/<report>/<graph>")
+def show_fragment(report, graph):
+ abort(404, "This page does not exist")
+
+@route("/static/<filepath:path>")
+def static(filepath):
+ return static_file(filepath, root="./static/")
+
+
+###############################
+#
+# Mount API on /api/
+#
+###############################
+from jsonapi import app as api_app
+mount("/api/", api_app)
+
+
+###############################
+#
+# Handle Errors
+#
+###############################
+@error(404)
+def error404(error):
+ return renderfile("404")
+
+
+#
+# Main. To run in developer mode simply pass --run
+#
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description='Run appliation')
+ parser.add_argument(
+ "--run",
+ action='store_true',
+ help='Run the webapp in developer mode'
+ )
+ args = parser.parse_args()
+
+ if args.run:
+ run(host="0.0.0.0", port="8009", debug=True, reloader=True)
+
+else:
+ application = bottle.default_app()
+
@@ -0,0 +1,9 @@
+
+[app]
+templatepath = ./templates/
+cache = False
+secret = bbda8uasi7782337bbdbo63o62b2b82d86d63d225nd926db02d6
+
+[db]
+host = localhost
+port = 27017
@@ -0,0 +1,16 @@
+
+
+import ConfigParser
+
+config = ConfigParser.SafeConfigParser({
+ "debug":"False",
+ "production":"True",
+ "templatepath":"./templates/",
+ "cache":"False"})
+
+config.read("config.ini")
+
+DEBUG = config.getboolean("app", "debug")
+PRODUCTION = config.getboolean("app", "production")
+TEMPLATEPATH = config.get("app", "templatepath")
+
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+from functools import *
+
+import pymongo
+
+from sitetools import *
+from config import config
+
+conn = pymongo.Connection("localhost", 27017)
+db = conn.bookdb
+db.users.ensure_index("name")
+users = db.users
+books = db.books
+
@@ -0,0 +1,54 @@
+import os
+import sys
+mypath = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0,mypath)
+sys.path.insert(0,os.path.join(mypath, "modules"))
+os.chdir(mypath)
+
+#stdlib modules
+import json
+import time
+
+# third party modules
+from bottle import *
+
+# my modules
+from config import config
+from database import books
+from sitetools import *
+import users
+
+app = Bottle()
+
+def document(func):
+ func.document = True
+ return func
+
+#
+# API Functions
+#
+@app.route("/books")
+@users.CheckPermission("READ")
+def json_books():
+ response.set_header('Content-type', 'application/json')
+ return json.dumps(books.find())
+
+
+#
+# Automagic utilities
+#
+@document
+@app.route('/status')
+def api_status():
+ """returns the server status and server time."""
+ return {'status':'online', 'servertime':time.time()}
+
+@app.route('/docs')
+def autodoc():
+ #the idea is to extract the route information out of the functions
+ return {"functions": [ {"route": None, "doc":v.__doc__} for k,v in globals().items() if hasattr(v, "document") and v.document == True]}
+
+@app.error(500)
+def error(error):
+ response.set_header("Content-Type", "application/json")
+ return json.dumps({"error": str(error.output)})
Oops, something went wrong.

0 comments on commit 36bd5a6

Please sign in to comment.