Skip to content

Commit

Permalink
Merge branch 'release/0.32.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Jul 21, 2021
2 parents e69c5f3 + 93c62eb commit 730e362
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
SAModelView,
)

__version__ = '0.31.0'
__version__ = '0.32.0'

__all__ = [
'DEFAULT_KEY',
Expand Down
21 changes: 21 additions & 0 deletions aiohttp_sqlalchemy/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import aiohttp_things as ahth
from aiohttp.web import View
from aiohttp.web_urldispatcher import AbstractRoute
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql import Delete, Select, Update
Expand Down Expand Up @@ -57,6 +58,26 @@ async def execute_select_stmt(
)
return page

async def prepare_context(self) -> None:
page: Optional[OffsetPage] = await self.execute_select_stmt()

if page:
route: AbstractRoute = self.request.match_info.route

self.context['items'] = page.items

if page.next:
next_ = str(page.next)
self.context['next_url'] = route.url_for(page_key=next_)
else:
self.context['next_url'] = page.next

if page.previous:
previous_ = str(page.previous)
self.context['previous_url'] = route.url_for(page_key=previous_)
else:
self.context['previous_url'] = page.previous


class PrimaryKeyMixin(ahth.PrimaryKeyMixin, SAModelMixin, metaclass=ABCMeta):
sa_pk_attr: Any = getattr(SAModelMixin.sa_model, 'pk', None)
Expand Down
2 changes: 1 addition & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Instance mixins

List mixins
^^^^^^^^^^^
.. autoclass:: aiohttp_sqlalchemy.OffsetPagination
.. autoclass:: aiohttp_sqlalchemy.OffsetPaginationMixin
:inherited-members:
:members:
:show-inheritance:
Expand Down
7 changes: 6 additions & 1 deletion docs/releases.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
========
Releases
========
Version 0.32
------------
* Added ``prepare_context()`` method for ``OffsetPaginationMixin``.


Version 0.31
------------
**Added**

* Added default ``page_key`` and ``page_key_adapter`` attributes
for ``OffsetPaginationMixin``;
* Added ``execute_select_stmt`` method for ``OffsetPaginationMixin``.
* Added ``execute_select_stmt()`` method for ``OffsetPaginationMixin``.

**Deprecated**

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiohttp-sqlalchemy"
version = "0.31.0"
version = "0.32.0"
description = "SQLAlchemy 1.4 / 2.0 support for aiohttp."
authors = [
"Ruslan Ilyasovich Gilfanov <ri.gilfanov@yandex.ru>",
Expand Down
26 changes: 26 additions & 0 deletions tests/test_web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,37 @@ class OffsetPaginationHandler(web.View, OffsetPaginationMixin):

await init_db(middlewared_app, Model.metadata)

request = make_mocked_request(METH_GET, '/?page_key=1')
request[SA_DEFAULT_KEY] = session
handler = OffsetPaginationHandler(request)
page = await handler.execute_select_stmt()
isinstance(page, OffsetPage)
await handler.prepare_context()
assert list(handler.context['items']) == []
assert handler.context['previous_url'] is None
assert handler.context['next_url'] is None

page_size = handler.paginator.page_size

async with session.begin():
session.add_all([Model() for i in range(page_size + 1)])

page = await handler.execute_select_stmt()
isinstance(page, OffsetPage)
await handler.prepare_context()
assert len(list(handler.context['items'])) == page_size
assert handler.context['previous_url'] is None
assert handler.context['next_url'] is not None

request = make_mocked_request(METH_GET, '/?page_key=2')
request[SA_DEFAULT_KEY] = session
handler = OffsetPaginationHandler(request)
page = await handler.execute_select_stmt()
isinstance(page, OffsetPage)
await handler.prepare_context()
assert len(list(handler.context['items'])) == 1
assert handler.context['previous_url'] is not None
assert handler.context['next_url'] is None


def test_list_add(
Expand Down

0 comments on commit 730e362

Please sign in to comment.