Beispiel #1
0
 def build(cls, settings: Settings):
     if not cls._loaded:
         django_settings.configure(INSTALLED_APPS=settings.get('INSTALLED_APPS'),
                                   DATABASES=settings.get('DATABASES'))
         setup()
         cls._loaded = True
     dj = cls()
     for model in apps.get_models():
         setattr(dj, model.__name__, model)
     return dj
Beispiel #2
0
 def build(cls, settings: Settings):
     if not cls._loaded:
         django_settings.configure(
             INSTALLED_APPS=settings.get('INSTALLED_APPS'),
             DATABASES=settings.get('DATABASES'))
         setup()
         cls._loaded = True
     dj = cls()
     for model in apps.get_models():
         setattr(dj, model.__name__, model)
     return dj
Beispiel #3
0
    def __init__(self,
                 routes: routing.RoutesConfig = None,
                 commands: List[Callable] = None,
                 settings: Dict[str, Any] = None) -> None:
        from apistar.settings import Settings
        initial_types = [App, routing.Router]  # type: List[type]

        routes = [] if (routes is None) else routes
        commands = [] if (commands is None) else commands

        self.routes = routes
        self.commands = list(self.built_in_commands) + commands
        self.settings = Settings(settings or {})
        self.router = routing.Router(self.routes, initial_types)

        self.preloaded = {
            'app': self,
            'router': self.router,
        }
        preload_state(self.preloaded, self.routes)
        if 'sql_alchemy' in self.preloaded:
            self.commands += [cmd.create_tables]

        self.wsgi = get_wsgi_server(app=self)
        self.click = get_click_client(app=self)
Beispiel #4
0
    def __init__(self,
                 routes: List[routing.Route] = None,
                 commands: List[Callable] = None,
                 settings: Dict[str, Any] = None) -> None:
        from apistar.settings import Settings
        from apistar.templating import Templates
        from apistar.backends.sqlalchemy import SQLAlchemy

        routes = [] if (routes is None) else routes
        commands = [] if (commands is None) else commands

        self.routes = routes
        self.commands = list(self.built_in_commands) + commands
        self.settings = Settings(settings or {})

        initial_types = [App]  # type: List[type]
        self.preloaded = {
            'app': self
        }
        if 'TEMPLATES' in self.settings:
            initial_types.append(Templates)
            self.preloaded['templates'] = Templates.build(self.settings)
        if 'DATABASE' in self.settings:
            initial_types.append(SQLAlchemy)
            self.preloaded['sql_alchemy'] = SQLAlchemy.build(self.settings)
            self.commands += [cmd.create_tables]

        self.router = routing.Router(self.routes, initial_types)
        self.wsgi = get_wsgi_server(app=self)
        self.click = get_click_client(app=self)
Beispiel #5
0
def test_settings_lookup():
    settings = Settings(ABC=123, DEF={'XYZ': 456})
    assert settings.get('ABC') == 123
    assert settings.get(['DEF']) == {'XYZ': 456}
    assert settings.get(['DEF', 'XYZ']) == 456
    assert settings.get('missing') is None
    assert settings.get(['ABC', 'missing']) is None
    assert settings.get(['DEF', 'missing']) is None
    assert settings.get(['DEF', 'missing'], '') == ''
Beispiel #6
0
    def build(cls, settings: Settings):
        template_dirs = settings.get(['TEMPLATES', 'DIRS'])
        loader = None  # type: jinja2.BaseLoader
        if len(template_dirs) == 1:
            loader = jinja2.FileSystemLoader(template_dirs[0])
        else:
            loader = jinja2.ChoiceLoader([
                jinja2.FileSystemLoader(template_dir)
                for template_dir in template_dirs
            ])

        return Templates(loader=loader)
Beispiel #7
0
def test_render_template():
    with tempfile.TemporaryDirectory() as tempdir:
        path = os.path.join(tempdir, 'index.html')
        with open(path, 'w') as index:
            index.write('<html><body>Hello, {{ username }}</body><html>')

        settings = Settings(TEMPLATES={'DIRS': [tempdir]})
        app = App(routes=routes, settings=settings)
        client = TestClient(app)
        response = client.get('/render_template/?username=tom')

        assert response.status_code == 200
        assert response.text == '<html><body>Hello, tom</body><html>'
Beispiel #8
0
def test_settings_lookup():
    settings = Settings(
        ABC=123,
        DEF={'XYZ': 456}
    )
    assert settings.get('ABC') == 123
    assert settings.get(['DEF']) == {'XYZ': 456}
    assert settings.get(['DEF', 'XYZ']) == 456
    assert settings.get('missing') is None
    assert settings.get(['ABC', 'missing']) is None
    assert settings.get(['DEF', 'missing']) is None
    assert settings.get(['DEF', 'missing'], '') == ''
Beispiel #9
0
def test_multiple_dirs():
    with tempfile.TemporaryDirectory() as tempdir1:
        with tempfile.TemporaryDirectory() as tempdir2:

            path = os.path.join(tempdir2, 'index.txt')
            with open(path, 'w') as index:
                index.write('Hello, {{ username }}.')

            settings = Settings(TEMPLATES={'DIRS': [tempdir1, tempdir2]})
            app = App(routes=routes, settings=settings)
            client = TestClient(app)
            response = client.get('/render_template/?username=tom')

            assert response.status_code == 200
            assert response.text == 'Hello, tom.'
Beispiel #10
0
    def build(cls, settings: Settings):
        template_dirs = settings.get(['TEMPLATES', 'DIRS'])

        package_loaders = [
            jinja2.PrefixLoader(
                {'apistar': jinja2.PackageLoader('apistar', 'templates')})
        ]  # type: List[jinja2.BaseLoader]

        if template_dirs is None:
            filesystem_loaders = []  # type: List[jinja2.BaseLoader]
        else:
            filesystem_loaders = [
                jinja2.FileSystemLoader(template_dir)
                for template_dir in template_dirs
            ]

        loader = jinja2.ChoiceLoader(package_loaders + filesystem_loaders)
        return Templates(loader=loader)
Beispiel #11
0
    def build(cls, settings: Settings):
        template_dirs = settings.get(['TEMPLATES', 'DIRS'])

        package_loaders = [
            jinja2.PrefixLoader({
                'apistar': jinja2.PackageLoader('apistar', 'templates')
            })
        ]  # type: List[jinja2.BaseLoader]

        if template_dirs is None:
            filesystem_loaders = []  # type: List[jinja2.BaseLoader]
        else:
            filesystem_loaders = [
                jinja2.FileSystemLoader(template_dir)
                for template_dir in template_dirs
            ]

        loader = jinja2.ChoiceLoader(package_loaders + filesystem_loaders)
        return Templates(loader=loader)
Beispiel #12
0
def test_template_not_found():
    settings = Settings(TEMPLATES={'DIRS': []})
    app = App(routes=routes, settings=settings)
    client = TestClient(app)
    with pytest.raises(ConfigurationError):
        client.get('/render_template/?username=tom')
Beispiel #13
0
 def build(cls, settings: Settings):
     root_dir = settings.get(['STATICS', 'DIR'])
     return cls(root_dir)
Beispiel #14
0
 def build(cls, settings: Settings):
     root_dir = settings.get(['STATICS', 'DIR'])
     return cls(root_dir)