Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Jun 2, 2021
2 parents 7de5908 + 973252d commit fc5e0ae
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 82 deletions.
20 changes: 8 additions & 12 deletions aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import TYPE_CHECKING
import warnings

from aiohttp_sqlalchemy.constants import DEFAULT_KEY
from aiohttp_sqlalchemy.decorators import sa_decorator
from aiohttp_sqlalchemy.exceptions import DuplicateAppKeyError
from aiohttp_sqlalchemy.exceptions import DuplicateAppKeyError, DuplicateRequestKeyError
from aiohttp_sqlalchemy.middlewares import sa_middleware
from aiohttp_sqlalchemy.views import SAView, SAViewMixin
from aiohttp_sqlalchemy.views import SABaseView, SAView, SAViewMixin


if TYPE_CHECKING:
Expand All @@ -15,7 +15,11 @@
TSABinding = Tuple[AsyncEngine, str, bool]


__version__ = '0.4.0'
__version__ = '0.5.0'

__all__ = ['DuplicateAppKeyError', 'DuplicateRequestKeyError', 'SABaseView',
'sa_bind', 'sa_decorator', 'sa_middleware', 'SAView',
'SAViewMixin', 'setup',]


def sa_bind(engine: 'AsyncEngine', key: str = DEFAULT_KEY, *,
Expand All @@ -24,14 +28,6 @@ def sa_bind(engine: 'AsyncEngine', key: str = DEFAULT_KEY, *,
return engine, key, middleware


def sa_engine(engine: 'AsyncEngine', key: str = DEFAULT_KEY, *,
middleware: bool = True) -> 'TSABinding':
msg = "aiohttp_sqlalchemy.sa_engine() is deprecated. " \
"Use aiohttp_sqlalchemy.sa_bind()."
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return engine, key, middleware


def setup(app: 'Application', bindings: 'Iterable[TSABinding]'):
""" Setup function for binding SQLAlchemy engines. """
for engine, key, middleware in bindings:
Expand Down
1 change: 1 addition & 0 deletions aiohttp_sqlalchemy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import wraps
from sqlalchemy.ext.asyncio import AsyncSession
from typing import TYPE_CHECKING

from aiohttp_sqlalchemy.constants import DEFAULT_KEY
from aiohttp_sqlalchemy.exceptions import DuplicateRequestKeyError

Expand Down
1 change: 1 addition & 0 deletions aiohttp_sqlalchemy/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aiohttp.web import middleware
from sqlalchemy.ext.asyncio import AsyncSession
from typing import TYPE_CHECKING

from aiohttp_sqlalchemy.constants import DEFAULT_KEY
from aiohttp_sqlalchemy.exceptions import DuplicateRequestKeyError

Expand Down
24 changes: 19 additions & 5 deletions aiohttp_sqlalchemy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class SAViewMixin:
""" SQLAlchemy class based view mixin. """
request: 'Request'

def __init__(self):
msg = "SAViewMixin is deprecated. Use SAAbstractView."
warnings.warn(msg, DeprecationWarning, stacklevel=2)

@property
def sa_main_session(self) -> 'AsyncSession':
msg = "SAViewMixin.sa_main_session is deprecated. " \
Expand All @@ -27,15 +31,25 @@ def sa_session(self, key: str = DEFAULT_KEY) -> 'AsyncSession':
return self.request[key]


class SAOneModelMixin(SAViewMixin):
sa_model: 'Any' # Not all developers use declarative mapping
class SAAbstractView(AbstractView, metaclass=ABCMeta):
""" SQLAlchemy view based on aiohttp.abc.AbstractView """

@property
def sa_main_session(self) -> 'AsyncSession':
msg = "SAAbstractView.sa_main_session is deprecated. " \
"Use SAAbstractView.sa_session()."
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return self.request[DEFAULT_KEY]

def sa_session(self, key: str = DEFAULT_KEY) -> 'AsyncSession':
return self.request[key]

class SAAbstractView(AbstractView, SAViewMixin, metaclass=ABCMeta):
""" SQLAlchemy view based on aiohttp.abc.AbstractView """

class SAOneModelMixin(SAAbstractView, metaclass=ABCMeta):
sa_model: 'Any' # Not all developers use declarative mapping


class SABaseView(View, SAViewMixin):
class SABaseView(View, SAAbstractView):
""" Simple SQLAlchemy view based on aiohttp.web.View """


Expand Down
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.4.0'
release = '0.5.0'


# -- General configuration ---------------------------------------------------
Expand Down
34 changes: 23 additions & 11 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ Copy and paste this code in a file and run:
SQLAlchemy and Asyncio
----------------------

See `Asynchronous I/O (asyncio) <https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html>`_
section in SQLAlchemy 1.4 Documentation.


Binding multiple engines
------------------------

.. code-block:: python
main_engine = create_async_engine('postgresql+asyncpg://user:password@host/database')
Expand All @@ -107,11 +105,6 @@ Binding multiple engines
Class based views
-----------------
.. warning::

For use a ``SAView`` in class based view inheritance, you must bind an
``AsyncEngine`` with default key.

.. code-block:: python
from aiohttp_sqlalchemy import SAView
Expand All @@ -121,8 +114,15 @@ Class based views
async with self.sa_session().begin():
# some your code
engine = create_async_engine('sqlite+aiosqlite:///')
aiohttp_sqlalchemy.setup(app, [sa_bind(engine)])
async with self.sa_session('sa_second').begin():
# some your code
main_engine = create_async_engine('sqlite+aiosqlite:///')
second_engine = create_async_engine('sqlite+aiosqlite:///')
aiohttp_sqlalchemy.setup(app, [
sa_bind(main_engine),
sa_bind(second_engine, 'sa_second'),
])
Decorating handlers
Expand Down Expand Up @@ -179,17 +179,29 @@ Access to ``AsyncSession`` object from nested app has no differences.
Change log
----------
Version 0.5.0
^^^^^^^^^^^^^
Removed
"""""""
Deprecated function ``aiohttp_sqlalchemy.sa_engine()`` is removed. Use
``aiohttp_sqlalchemy.sa_bind()``.

Deprecated
""""""""""
Undocumented class ``views.SAViewMixin`` is deprecated. Use
``views.SAAbstractView``.

Version 0.4.0
^^^^^^^^^^^^^
Added
"""""
``SAView.sa_session(key: str ='sa_main')`` function is added instead
``SAView.sa_session(key: str = 'sa_main')`` function is added instead
``SAView.sa_main_session``.

Deprecated
""""""""""
``SAView.sa_main_session`` is deprecated. Use
``SAView.sa_session(key: str ='sa_main')``.
``SAView.sa_session(key: str = 'sa_main')``.

Version 0.3.0
^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions examples/single_decorated_cbv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_decorator, sa_bind
from aiohttp_sqlalchemy import sa_decorator, sa_bind, SAView
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
Expand All @@ -17,13 +17,13 @@ class Request(Base):
timestamp = sa.Column(sa.DateTime(), default=datetime.now)


class Main(web.View):
class Main(SAView):
@sa_decorator()
async def get(self):
async with self.request.app['sa_main'].begin() as conn:
await conn.run_sync(Base.metadata.create_all)

async with self.request['sa_main'].begin():
async with self.sa_session().begin():
self.request['sa_main'].add_all([Request()])
result = await self.request['sa_main'].execute(sa.select(Request))
data = {r.id: r.timestamp.isoformat() for r in result.scalars()}
Expand Down

0 comments on commit fc5e0ae

Please sign in to comment.