Skip to content

Commit

Permalink
review pass 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Sep 11, 2024
1 parent 3ab7078 commit 4658a01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
11 changes: 9 additions & 2 deletions geoportal/c2cgeoportal_geoportal/lib/dbreflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,19 @@ def get_table(
# create table and reflect it
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Did not recognize type 'geometry' of column", SAWarning)
args = [tablename, metadata]
args = []
if primary_key is not None:
# Ensure we have a primary key to be able to edit views
args.append(Column(primary_key, Integer, primary_key=True))
with _get_table_lock:
table = Table(*args, schema=schema, autoload=True, autoload_with=engine, keep_existing=True)
table = Table(
tablename,
metadata,
*args,
schema=schema,
autoload_with=engine,
keep_existing=True,
)
return table


Expand Down
19 changes: 7 additions & 12 deletions geoportal/c2cgeoportal_geoportal/views/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ def update(self, request: pyramid.request.Request, obj: Any) -> None:
if last_update_user is not None:
setattr(obj, last_update_user, request.user.id)

def _get_geometry_check_base_query(self, request: pyramid.request.Request) -> sqlalchemy.orm.query.Query:
def _get_geometry_check_base_query(
self, request: pyramid.request.Request
) -> sqlalchemy.orm.query.RowReturningQuery[tuple[int]]:
from c2cgeoportal_commons.models.main import ( # pylint: disable=import-outside-toplevel
Layer,
RestrictionArea,
Role,
)

allowed = models.DBSession.query(func.count(RestrictionArea.id))
assert models.DBSession is not None
allowed = models.DBSession.query(func.count(RestrictionArea.id)) # pylint: disable=not-callable
allowed = allowed.join(RestrictionArea.roles)
allowed = allowed.join(RestrictionArea.layers)
allowed = allowed.filter(RestrictionArea.readwrite.is_(True))
Expand All @@ -104,12 +107,8 @@ def _get_geometry_check_base_query(self, request: pyramid.request.Request) -> sq
return allowed


class _CreateCallback(_BaseCallback):
class _InsertCallback(_BaseCallback):
def __call__(self, request: pyramid.request.Request, feature: Feature, obj: Any) -> None:
if obj is not None:
raise HTTPBadRequest("Feature id should not be provided")
del obj

from c2cgeoportal_commons.models.main import ( # pylint: disable=import-outside-toplevel
RestrictionArea,
)
Expand Down Expand Up @@ -270,7 +269,7 @@ def _get_protocol_for_layer(self, layer: "main.Layer") -> Protocol:
models.DBSession,
cls,
geom_attr,
before_create=_CreateCallback(layer),
before_insert=_InsertCallback(layer),
before_update=_UpdateCallback(layer),
before_delete=_DeleteCallback(layer),
)
Expand Down Expand Up @@ -397,14 +396,10 @@ def create(self) -> FeatureCollection | None:
self.request.response.cache_control.no_cache = True

protocol = self._get_protocol_for_request()
log_updater = _BaseCallback(self._get_layer_for_request())
try:
features = protocol.create(self.request)
if isinstance(features, HTTPException):
raise features
if features is not None:
for feature in features.features: # pylint: disable=no-member
log_updater.update(self.request, feature)
return features
except TopologicalError as e:
self.request.response.status_int = 400
Expand Down

0 comments on commit 4658a01

Please sign in to comment.