Beispiel #1
0
    def setUp(self):
        from sqlalchemy import Column, Integer, String, create_engine, orm
        from sqlalchemy.schema import MetaData
        from sqlalchemy.ext.declarative import declarative_base
        metadata = MetaData()
        Model = declarative_base(metadata=metadata)
        class User(Model):
            __tablename__ = 'users'
            id = Column(Integer, primary_key=True)
            login = Column(String(255), nullable=False, unique=True)
            password = Column(String(255), nullable=False)
        auth = SqlaModelAuth(User)

        class Env(web.AppEnvironment):

            @cached_property
            def db(self):
                return orm.sessionmaker(bind=create_engine('sqlite://'))()

            @cached_property
            def template(self):
                return MockTemplateManager()

        @web.request_filter
        def make_env(env, data, nxt):
            metadata.create_all(env.db.bind)
            user = User(login='******', password=encrypt_password('123'))
            env.db.add(user)
            env.db.commit()
            try:
                return nxt(env, data)
            finally:
                env.db.close()

        def anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user, None)
            return web.Response('ok')

        def no_anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user.login, 'user name')
            return web.Response('ok')

        app = make_env | web.cases(
            auth.login(),
            auth.logout(),
            auth | web.cases(
                web.match('/a', 'a') | anonymouse,
                web.match('/b', 'b') | auth_required | no_anonymouse,
            ),
        )
        self.app = web.Application(app, Env)
Beispiel #2
0
 def wsgi_app(self):
     return web.Application(self.app)
Beispiel #3
0
from iktomi import web
from iktomi.cli import app, fcgi, manage
import os
import sys
import logging

webapp = web.Application(
    web.cases(
        web.match('/', 'index') | (lambda e, d: web.Response('hello world'))),
    web.AppEnvironment)


# adding custom logging config to test bootstrapping
def bootstrap():
    logpath = os.path.join(os.path.dirname(__file__), 'hello.log')
    hellolog = logging.FileHandler(logpath)
    logging.root.handlers.append(hellolog)


devapp = app.App(webapp, bootstrap=bootstrap)
fcgi_sock_path = os.path.join(os.path.dirname(__file__), 'fcgi.sock')
fcgi_app = fcgi.Flup(webapp,
                     bind=fcgi_sock_path,
                     cwd=os.path.dirname(__file__))

if __name__ == '__main__':
    manage(dict(dev=devapp, fcgi=fcgi_app))
Beispiel #4
0
# -*- coding: utf-8 -*-
from iktomi import web
from iktomi.web.filters import *
from iktomi.templates import jinja2, Template
from environment import Environment

import cfg
import handlers as h

static = static_files(cfg.STATIC)
media = static_files(cfg.MEDIA_DIR, cfg.MEDIA_URL)
form_temp = static_files(cfg.FORM_TEMP, cfg.FORM_TEMP_URL)
template = Template(cfg.TEMPLATES,
                    jinja2.TEMPLATE_DIR,
                    engines={'html': jinja2.TemplateEngine})

app = web.cases(
    static,
    media,
    form_temp,
    match('/', 'files') | web.cases(
        # Playing REST ;)
        method('GET') | h.list_files,
        method('POST') | h.post_file,
        #method('DELETE') | h.delete_files,
    ),
)

wsgi_app = web.Application(app, env_class=Environment)