def setUp(self): from torext.app import TorextApp from torext.handlers import BaseHandler app = TorextApp() class APIParams(params.ParamSet): id = params.IntegerField(PARAMS_ID_MSG, required=True, min=1) token = params.Field(PARAMS_TOKEN_MSG, required=True, length=32) tag = params.WordField(PARAMS_TAG_MSG, length=8, default='foo') from_ = params.WordField(PARAMS_FROM, key='from', required=False, length=16) text_anyway = params.WordField() text_not_null = params.WordField(null=False) @app.route('/api') class APIHandler(BaseHandler): @APIParams.validation_required def get(self): print 'arguments', self.request.arguments print 'params', self.params self.write_json(self.params.to_dict(include_none=True)) def post(self): self.write('ok') # let exceptions raised in handler be rethrowed in test function self.c = app.test_client(raise_handler_exc=True)
def setup(self): app = TorextApp() app.settings['DEBUG'] = False app.settings['SQLALCHEMY'] = { 'uri': MYSQL_URI, 'echo': True } app.setup() self.create_database() db = SQLAlchemy(app=app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) class Hub(db.Model): id = db.Column(db.Integer, primary_key=True) self.User = User self.Hub = Hub db.create_all() self.app = app self.db = db
def setUp(self): from torext.app import TorextApp from torext.handlers import BaseHandler app = TorextApp() #app.settings.update(LOG_REQUEST=True) class APIParams(params.ParamSet): __datatype__ = 'json' id = params.IntegerField(required=True, min=1) token = params.Field(required=True, length=32) headers = params.Field() tag = params.WordField(PARAMS_TAG_MSG, length=8, default='foo') from_ = params.WordField(PARAMS_FROM, key='from', required=False, length=16) text_anyway = params.WordField() text_not_null = params.WordField(null=False) @app.route('/api') class APIHandler(BaseHandler): @APIParams.validation_required def post(self): print 'arguments', self.request.arguments print 'params', self.params if 'headers' in self.params.data: print 'get headers' assert self.params.headers == [[1, 2]] self.write_json(self.params.to_dict(include_none=True)) # let exceptions raised in handler be rethrowed in test function self.c = app.test_client(raise_handler_exc=True)
def make_app(): app = TorextApp() @app.route('/') class HomeHdr(BaseHandler): def get(self): self.write(GET_RESULT) def post(self): self.write(POST_RESULT) @app.route('/withdata') class WithdataHdr(BaseHandler): def get(self): self.write(self.get_argument('p')) def post(self): self.write(self.get_argument('d')) @app.route('/header') class HeaderHdr(BaseHandler): def get(self): header_name = self.get_argument('h') header_value = self.request.headers.get(header_name) self.set_header(header_name, header_value) self.write(header_value) # although this is not needed, it's good to be set explicitly app.update_settings({ 'TESTING': True }) return app
def setUp(self): app = TorextApp() app.settings['DEBUG'] = False app.settings['SQLALCHEMY'] = { 'uri': 'mysql://*****:*****@localhost/torext_test', 'echo': True } app.setup() db = SQLAlchemy(app=app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) self.User = User db.create_all() self.app = app self.db = db
def setUp(self): app = TorextApp() app.settings['DEBUG'] = False app.settings['SQLALCHEMY'] = { 'uri': 'mysql+mysqlconnector://test:123456@localhost/testdb', 'echo': True } app.setup() db = SQLAlchemy(app=app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) self.User = User db.create_all() self.app = app self.db = db
def setup(self): app = TorextApp() app.settings['DEBUG'] = False app.settings['SQLALCHEMY'] = {'uri': MYSQL_URI, 'echo': True} app.setup() self.create_database() db = SQLAlchemy(app=app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) class Hub(db.Model): id = db.Column(db.Integer, primary_key=True) self.User = User self.Hub = Hub db.create_all() self.app = app self.db = db
import copy import time from hashlib import md5 # import datetime import logging from bson.objectid import ObjectId import tornado.web from torext import errors, params from torext.app import TorextApp from torext.utils import _json from torext.handlers import BaseHandler as _BaseHandler import pymongo.errors from pymongo import Connection app = TorextApp() app.set_root_path() print 'root', app.root_path app.update_settings(dict( COOKIE_SECRET='P0UTa5iuRaaVlV8QZF2uVR7hHwTOSkQhg2Fol18OKwc=', TEMPLATE_PATH='templates', PORT=os.environ.get('PORT', '8001') )) app.setup() mongodb_uri = os.getenv('MONGOHQ_URL', 'mongodb://localhost:27017/chatroom') logging.info('mongodb uri: %s', mongodb_uri) try: db = Connection(mongodb_uri)[mongodb_uri.split('/')[-1]] except pymongo.errors.ConnectionFailure, e:
class AuthenticationNotPass(errors.TorextException): pass class ValidationError(errors.TorextException): pass def same_dict(d1, d2): return set(d1.keys()) == set(d2.keys()) and\ not [i for i in d1 if d1[i] != d2[i]] app = TorextApp() app.settings['COOKIE_SECRET'] = generate_cookie_secret() @app.route('/') class HomeHandler(BaseHandler): EXCEPTION_HANDLERS = { AuthenticationNotPass: '******', (ValidationError, IndexError): '_handle_400' } def get(self): exc = int(self.get_argument('exc')) if exc == 0: raise AuthenticationNotPass(EXC_MSG_0) elif exc == 1:
import time import logging from hashlib import md5 from bson.objectid import ObjectId from tornado.web import asynchronous from torext import errors, params from torext.app import TorextApp from torext.handlers import BaseHandler as _BaseHandler import pymongo.errors from pymongo import Connection import json app = TorextApp(extra_settings={ 'COOKIE_SECRET': 'P0UTa5iuRaaVlV8QZF2uVR7hHwTOSkQhg2Fol18OKwc=', 'TEMPLATE_PATH': 'templates', 'PORT': os.environ.get('PORT', '8001') }) #app.set_root_path() #print 'root', app.root_path app.setup() def _handle_object_for_json(obj): if isinstance(obj, ObjectId): return str(obj) @app.register_json_encoder
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp from torext.handlers import BaseHandler app = TorextApp() @app.route('/') class HomeHandler(BaseHandler): def get(self): self.write('Hello World!') if __name__ == '__main__': app.command_line_config() app.run()
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from raven import Client from gevent.pool import Pool from gevent import local from torext.app import TorextApp from loki import settings as _settings from loki.mail import Mail from loki.utils.gevent import CustomGeventHTTPTransport app = TorextApp(_settings) settings = app.settings # NOQA mail = Mail(app) greenlet_pool = Pool(settings.GREENLET_POOL_SIZE) sentry_client = Client(settings.SENTRY_SERVER_URL, install_sys_hook=False, transport=CustomGeventHTTPTransport) """ g object similar to flask.g but in request context""" g = None DEFAULT_ROUTES = [ ('', 'dashboard.web'), ('', 'misc.web'), ('/draw', 'draw.web'),
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings) if '__main__' == __name__: app.command_line_config() app.run()
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings, { 'LOG_RESPONSE': True }) app.route_many([ ('', 'views'), ('/api', 'api.views') ]) print app.host_handlers if __name__ == '__main__': app.command_line_config() app.run()
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings) app.setup() app.route_many([ ('', 'handlers') ]) if __name__ == '__main__': app.command_line_config() app.run()
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings, {'LOG_RESPONSE': True}) app.route_many([('', 'views'), ('/api', 'api.views')]) print app.host_handlers if __name__ == '__main__': app.command_line_config() app.run()
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings) import subprocess import random import string import json from torext.handlers import BaseHandler as _BaseHandler from pi_router.zipdb import ZipDB db = ZipDB() def get_mac(ip): p = subprocess.Popen(['arp', '-n', ip], stdout=subprocess.PIPE) out, _ = p.communicate() try: mac = out.split('\n')[1].split()[2] except IndexError: import uuid mac = str(uuid.getnode()) return mac
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.app import TorextApp import settings app = TorextApp(settings) app.setup() app.route_many([ ('', 'views'), ('/users', 'user.views'), #('/skills', 'skill.views'), ('/specs', 'spec.views'), #('/courses', 'course.views'), ('/admin', 'admin.views'), ]) if __name__ == '__main__': app.command_line_config() app.run()
import os import re import time import json import logging from torext.app import TorextApp from torext.handlers import BaseHandler as _BaseHandler from tornado.httpclient import AsyncHTTPClient from tornado.web import StaticFileHandler from tornado import gen from squared.auth import SpotifyMixin import settings app = TorextApp(settings) app.update_settings({"STATIC_PATH": None}) # For local dev app.route_many( [ (r"/bower_components/(.*)", StaticFileHandler, {"path": os.path.join(app.root_path, "../bower_components")}), (r"/js/(.*)", StaticFileHandler, {"path": os.path.join(app.root_path, "../js")}), (r"/css/(.*)", StaticFileHandler, {"path": os.path.join(app.root_path, "../css")}), ] ) class HTTPRequestFailed(Exception): pass
# -*- coding: utf-8 -*- import os import re import time import json import logging from torext.app import TorextApp from torext.handlers import BaseHandler as _BaseHandler from tornado.httpclient import AsyncHTTPClient from tornado.web import StaticFileHandler from tornado import gen from squared.auth import SpotifyMixin import settings app = TorextApp(settings) app.update_settings({'STATIC_PATH': None}) # For local dev app.route_many([ (r'/bower_components/(.*)', StaticFileHandler, { 'path': os.path.join(app.root_path, '../bower_components') }), (r'/js/(.*)', StaticFileHandler, { 'path': os.path.join(app.root_path, '../js') }), (r'/css/(.*)', StaticFileHandler, { 'path': os.path.join(app.root_path, '../css') }), ])
#!/usr/bin/env python # -*- coding: utf-8 -*- from torext.handlers import oauth from torext.app import TorextApp from torext.handlers import BaseHandler from tornado.web import asynchronous import settings app = TorextApp(settings) _url_prefix = 'http://127.0.0.1:8000' class MyBaseHandler(BaseHandler): def _on_auth(self, user): print 'user', user if not user: self.write('failed, retry') return self.finish() self.write_json(user) self.finish() @app.route('/') class HomeHandler(MyBaseHandler): def get(self): self.write_json(app.settings)