Skip to content

Commit

Permalink
Merge branch 'release/0.9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Jun 9, 2021
2 parents c888470 + 33f91e7 commit 0bc247e
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[mypy]
files = aiohttp_sqlalchemy, tests
plugins = sqlalchemy.ext.mypy.plugin

[mypy-sqlalchemy.*]
ignore_missing_imports = True
6 changes: 3 additions & 3 deletions aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TSABinding = Tuple[TSessionFactory, str, bool]


__version__ = '0.9.0'
__version__ = '0.9.1'

__all__ = ['DuplicateAppKeyError', 'DuplicateRequestKeyError', 'SABaseView',
'sa_bind', 'sa_decorator', 'sa_middleware', 'SAView', 'setup',]
Expand All @@ -30,11 +30,11 @@ def sa_bind(factory: 'TSessionFactory', key: str = DEFAULT_KEY, *,

def setup(app: 'Application', bindings: 'Iterable[TSABinding]'):
""" Setup function for binding SQLAlchemy engines. """
for Session, key, middleware in bindings:
for factory, key, middleware in bindings:
if key in app:
raise DuplicateAppKeyError(key)

app[key] = Session
app[key] = factory

if middleware:
app.middlewares.append(sa_middleware(key))
11 changes: 6 additions & 5 deletions aiohttp_sqlalchemy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
from aiohttp_sqlalchemy.exceptions import DuplicateRequestKeyError

if TYPE_CHECKING:
from aiohttp.web import StreamResponse
from aiohttp.web import Request, StreamResponse
from typing import Awaitable, Callable, Union, Type


def sa_decorator(key: str = DEFAULT_KEY):
def sa_decorator(key: str = DEFAULT_KEY) \
-> 'Callable[..., Union[Type[AbstractView], Callable[[Request], Awaitable[StreamResponse]]]]':
""" SQLAlchemy asynchronous handler decorator. """
def wrapper(handler):
@wraps(handler)
async def wrapped(*args, **kwargs) -> 'StreamResponse':
print(*args)
request = args[0].request \
if isinstance(args[0], AbstractView) \
else args[-1]

if key in request:
raise DuplicateRequestKeyError(key)

Session = request.config_dict.get(key)
async with Session() as request[key]:
session_factory = request.config_dict.get(key)
async with session_factory() as request[key]:
return await handler(*args, **kwargs)

return wrapped
Expand Down
2 changes: 1 addition & 1 deletion aiohttp_sqlalchemy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AbstractDuplicateKeyError(ValueError):

class DuplicateAppKeyError(AbstractDuplicateKeyError):
def __init__(self, key: str):
self.message = f'Duplicated app key `{key}`. Check `engines` ' \
self.message = f'Duplicated app key `{key}`. Check `bindings` ' \
f' argument in `aiohttp_sqlalchemy.setup()` call.'
super().__init__(self.message)

Expand Down
8 changes: 4 additions & 4 deletions aiohttp_sqlalchemy/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

if TYPE_CHECKING:
from aiohttp.web import Request, StreamResponse
from typing import Callable
from typing import Awaitable, Callable


def sa_middleware(key: str = DEFAULT_KEY) -> 'Callable':
def sa_middleware(key: str = DEFAULT_KEY) -> 'Callable[..., Awaitable[StreamResponse]]':
""" SQLAlchemy asynchronous middleware factory. """
@middleware
async def sa_middleware_(request: 'Request', handler: 'Callable')\
-> 'StreamResponse':
if key in request:
raise DuplicateRequestKeyError(key)

Session = request.config_dict.get(key)
async with Session() as request[key]:
session_factory = request.config_dict.get(key)
async with session_factory() as request[key]:
return await handler(request)

return sa_middleware_
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Ruslan Ilyasovich Gilfanov'

# The full version, including alpha/beta/rc tags
release = '0.9.0'
release = '0.9.1'


# -- General configuration ---------------------------------------------------
Expand Down
32 changes: 16 additions & 16 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,21 @@ Nested apps
Change log
----------
Version 0.9.0
^^^^^^^^^^^^^
Version 0.9
^^^^^^^^^^^
Added
"""""
Added support of handlers in class for a ``sa_decorator(key)``. For example
``app.add_routes([web.get('/', MyClass().my_get_method)])``.
``sa_decorator(key)`` added support of `organized handlers in class
<https://docs.aiohttp.org/en/stable/web_quickstart.html#organizing-handlers-in-classes>`_.

Removed
"""""""
Removed support of ``AsyncEngine`` type in ``sa_bind()`` signature. Use
``sessionmaker(engine, AsyncSession)`` or custom session factory returning
``AsyncSession`` instance.

Version 0.8.0
^^^^^^^^^^^^^
Version 0.8
^^^^^^^^^^^
Changed
"""""""
Rename first argument from ``arg`` to ``factory`` in ``sa_bind()`` signature.
Expand All @@ -202,8 +202,8 @@ Deprecated
``sessionmaker(engine, AsyncSession)`` or custom session factory returning
``AsyncSession`` instance.

Version 0.7.0
^^^^^^^^^^^^^
Version 0.7
^^^^^^^^^^^
Changed
"""""""
Usage ``sqlalchemy.orm.sessionmaker`` instance is recomended as a first
Expand All @@ -216,8 +216,8 @@ Removed support of ``request.config_dict.get('sa_main')`` and
``request.app['sa_main']`` expressions. Use a ``request['sa_main'].bind``
expression.

Version 0.6.0
^^^^^^^^^^^^^
Version 0.6
^^^^^^^^^^^
Added
"""""
Add support ``sqlalchemy.orm.sessionmaker`` as a first argument in function
Expand All @@ -242,8 +242,8 @@ Deprecated class ``views.SAViewMixin`` is removed. Use
Deprecated attribute ``SAView.sa_main_session`` is removed. Use method
``SAView.sa_session(key: str = 'sa_main')``.

Version 0.5.0
^^^^^^^^^^^^^
Version 0.5
^^^^^^^^^^^
Removed
"""""""
Deprecated function ``aiohttp_sqlalchemy.sa_engine()`` is removed. Use
Expand All @@ -254,8 +254,8 @@ Deprecated
Undocumented class ``views.SAViewMixin`` is deprecated. Use
``views.SAAbstractView``.

Version 0.4.0
^^^^^^^^^^^^^
Version 0.4
^^^^^^^^^^^
Added
"""""
``SAView.sa_session(key: str = 'sa_main')`` function is added instead
Expand All @@ -266,8 +266,8 @@ Deprecated
``SAView.sa_main_session`` is deprecated. Use
``SAView.sa_session(key: str = 'sa_main')``.

Version 0.3.0
^^^^^^^^^^^^^
Version 0.3
^^^^^^^^^^^
Added
"""""
``aiohttp_sqlalchemy.sa_bind()`` function is added instead
Expand Down
13 changes: 9 additions & 4 deletions examples/hybrid_approach.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind
from datetime import datetime
from random import choice
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from random import choice
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
13 changes: 9 additions & 4 deletions examples/multiple_decorated_functions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind
from datetime import datetime
from random import choice
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from random import choice
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
13 changes: 9 additions & 4 deletions examples/multiple_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind
from datetime import datetime
from random import choice
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from random import choice
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
13 changes: 9 additions & 4 deletions examples/nested_app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind
from datetime import datetime
from random import choice
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from random import choice
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
11 changes: 8 additions & 3 deletions examples/single_decorated_cbv.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind, SAView
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator, SAView

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
11 changes: 8 additions & 3 deletions examples/single_decorated_class_method.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind, SAView
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
11 changes: 8 additions & 3 deletions examples/single_decorated_function.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_decorator

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down
11 changes: 8 additions & 3 deletions examples/single_middleware.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from typing import TYPE_CHECKING

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind

if TYPE_CHECKING:
from typing import Any


metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
Base: 'Any' = orm.declarative_base(metadata=metadata)


class Request(Base):
Expand Down

0 comments on commit 0bc247e

Please sign in to comment.