Skip to content

Commit

Permalink
Merge pull request #322 from camptocamp/backport/321-to-2.2
Browse files Browse the repository at this point in the history
[Backport 2.2] Fix the types in the abstract view
  • Loading branch information
sbrunner authored Jan 10, 2024
2 parents 078ca07 + a221064 commit 251bdb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions c2cgeoform/tests/views/test_abstract_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import TestCase
from unittest.mock import Mock

import pytest
from bs4 import BeautifulSoup
from pyramid.httpexceptions import HTTPFound, HTTPNotFound

Expand Down Expand Up @@ -143,9 +144,8 @@ def test_new_post_success(self):
self.request.POST["age"] = "37"

views = ConcreteViews(self.request)
response = views.save()

self.assertIsInstance(response, HTTPFound)
with pytest.raises(HTTPFound):
views.save()

class Matcher:
def __eq__(self, other):
Expand Down
24 changes: 14 additions & 10 deletions c2cgeoform/views/abstract_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from geoalchemy2.elements import WKBElement
from geoalchemy2.shape import to_shape
from geojson import Feature, FeatureCollection
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.response import Response
from pyramid.httpexceptions import HTTPFound, HTTPInternalServerError, HTTPNotFound
from sqlalchemy import desc, or_, types
from sqlalchemy.exc import DBAPIError
from sqlalchemy.inspection import inspect
Expand Down Expand Up @@ -220,14 +219,19 @@ def __str__(self) -> str:


# TODO for Python 3.11
# class _Index(TypedDict, Generic[T]):
# class Index(TypedDict, Generic[T]):
# grid_actions: list[ItemAction]
# list_fields: list[ListField[T]]
class _Index(TypedDict):
class Index(TypedDict):
grid_actions: list[ItemAction]
list_fields: list[ListField[Any]]


class Grid(TypedDict):
rows: JSONList
total: int


class AbstractViews(Generic[T]):
_model: Optional[type[T]] = None # sqlalchemy model
_list_fields: list[ListField[T]] = [] # Fields in list
Expand All @@ -249,13 +253,13 @@ def __init__(self, request: pyramid.request.Request) -> None:
self._appstruct: Optional[Dict[str, Any]] = None
self._obj: Optional[T] = None

def index(self) -> _Index:
def index(self) -> Index:
return {
"grid_actions": self._grid_actions(),
"list_fields": self._list_fields,
}

def grid(self) -> pyramid.response.Response:
def grid(self) -> Grid:
"""
API method which serves the JSON data for the Bootgrid table in the admin view.
"""
Expand All @@ -274,7 +278,7 @@ def grid(self) -> pyramid.response.Response:
return {"rows": self._grid_rows(query, offset, limit), "total": query.count()}
except DBAPIError as exception:
_LOGGER.error(str(exception), exc_info=True)
return Response(_DB_ERR_MSG, content_type="text/plain", status=500)
raise HTTPInternalServerError(_DB_ERR_MSG) from exception

def map(self, map_settings: Optional[JSONDict] = None) -> JSONDict:
map_settings = map_settings or {}
Expand Down Expand Up @@ -558,7 +562,7 @@ def duplicate(self) -> JSONDict:
src = self._get_object()
return self.copy(src)

def save(self) -> Union[JSONDict, pyramid.response.Response]:
def save(self) -> JSONDict:
obj = self._get_object()
try:
form = self._form()
Expand All @@ -569,7 +573,7 @@ def save(self) -> Union[JSONDict, pyramid.response.Response]:
obj = form.schema.objectify(self._appstruct, obj)
self._obj = self._request.dbsession.merge(obj)
self._request.dbsession.flush()
return HTTPFound(
raise HTTPFound(
self._request.route_url(
"c2cgeoform_item",
action="edit",
Expand All @@ -583,7 +587,7 @@ def save(self) -> Union[JSONDict, pyramid.response.Response]:
return {
"title": form.title,
"form": exception,
"form_render_args": tuple(),
"form_render_args": [],
"form_render_kwargs": kwargs,
"deform_dependencies": form.get_widget_resources(),
}
Expand Down

0 comments on commit 251bdb7

Please sign in to comment.