Skip to content

Commit

Permalink
Merge pull request #73 from benoitblanc/use-context-manager
Browse files Browse the repository at this point in the history
Use context manager
  • Loading branch information
manisandro authored Apr 11, 2024
2 parents e1dba94 + 1568cb4 commit 17777e0
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 247 deletions.
9 changes: 4 additions & 5 deletions src/config_generator/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,10 @@ def write_permissions(self):
for role in permissions['roles']:

res_permissions = OrderedDict()
session = self.config_models.session()
permitted_resources = permissions_query.permitted_resources
resources = permitted_resources(resource_type, role['role'], session).keys()
res_permissions[resource_type] = sorted(list(resources))
session.close()
with self.config_models.session() as session:
permitted_resources = permissions_query.permitted_resources
resources = permitted_resources(resource_type, role['role'], session).keys()
res_permissions[resource_type] = sorted(list(resources))

permissions_config.merge_service_permissions(
role['permissions'], res_permissions
Expand Down
10 changes: 4 additions & 6 deletions src/config_generator/data_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def config(self):

resources = OrderedDict()

session = self.config_models.session()
resources['datasets'] = self._datasets(config, session)
with self.config_models.session() as session:
resources['datasets'] = self._datasets(config, session)

config['resources'] = resources
return config
Expand All @@ -55,12 +55,10 @@ def permissions(self, role):
permissions = OrderedDict()

# collect permissions from ConfigDB
session = self.config_models.session()

permissions['data_datasets'] = self._dataset_permissions(role, session)
with self.config_models.session() as session:
permissions['data_datasets'] = self._dataset_permissions(role, session)

# collect feature reports
session.close()

return permissions

Expand Down
36 changes: 18 additions & 18 deletions src/config_generator/dnd_form_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def __create_editor_widget(self, maplayer, field, prefix=""):
if editWidget.get("type") == "CheckBox":
# Don't translate NOT NULL constraint into required for checkboxes
required = False
conn = self.db_engine.db_engine(self.metadata['database']).connect()

widget = ElementTree.Element("widget")
widget.set("name", prefix + field)
Expand Down Expand Up @@ -166,24 +165,25 @@ def __create_editor_widget(self, maplayer, field, prefix=""):
GROUP BY defined_type
LIMIT 1;
""").format(schema = self.metadata['schema'], table = self.metadata['table_name'], column = field))
result = conn.execute(sql)
for row in result:
defined_type = row['defined_type']
try :
widget.set("class", "QComboBox")
sql = sql_text("SELECT unnest(enum_range(NULL:: %s))::text as values ;" % defined_type)
with self.db_engine.db_engine(self.metadata['database']).connect() as conn:
result = conn.execute(sql)
for row in result :
values['value'] = row['values']
values['name'] = row['values']
item = ElementTree.Element("item")
self.__add_widget_property(item, "value", values, "value")
self.__add_widget_property(item, "text", values, "name")
widget.append(item)
except Exception:
widget.set("class", "QLineEdit")
self.logger.warning("Failed to add Enumeration widget in %s for %s" % (self.metadata['table_name'], field))
pass
for row in result:
defined_type = row['defined_type']
try :
widget.set("class", "QComboBox")
sql = sql_text("SELECT unnest(enum_range(NULL:: %s))::text as values ;" % defined_type)
result = conn.execute(sql)
for row in result :
values['value'] = row['values']
values['name'] = row['values']
item = ElementTree.Element("item")
self.__add_widget_property(item, "value", values, "value")
self.__add_widget_property(item, "text", values, "name")
widget.append(item)
except Exception:
widget.set("class", "QLineEdit")
self.logger.warning("Failed to add Enumeration widget in %s for %s" % (self.metadata['table_name'], field))
pass
return widget
elif editWidget.get("type") == "ValueRelation":
widget.set("class", "QComboBox")
Expand Down
21 changes: 9 additions & 12 deletions src/config_generator/ext_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ def permissions(self, role):
permissions = OrderedDict()

# collect permissions from ConfigDB
session = self.config_models.session()

# helper method alias
permitted_resources = self.permissions_query.permitted_resources

# collect role permissions from ConfigDB
external_links = permitted_resources(
'external_links', role, session
).keys()
permissions['external_links'] = sorted(list(external_links))

session.close()
with self.config_models.session() as session:
# helper method alias
permitted_resources = self.permissions_query.permitted_resources

# collect role permissions from ConfigDB
external_links = permitted_resources(
'external_links', role, session
).keys()
permissions['external_links'] = sorted(list(external_links))

return permissions
16 changes: 7 additions & 9 deletions src/config_generator/feature_info_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ def permissions(self, role):
# NOTE: use ordered keys
permissions = OrderedDict()

# NOTE: basic WMS service permissions collected by OGC service config
# get additional permissions
permissions['wms_services'] = self.additional_wms_permissions(role)
# collect permissions from ConfigDB
with self.config_models.session() as session:
# NOTE: basic WMS service permissions collected by OGC service config
# get additional permissions
permissions['wms_services'] = self.additional_wms_permissions(role, session)

return permissions

Expand Down Expand Up @@ -206,21 +208,19 @@ def get_child_layers(entry, result):

return available_info_layers

def additional_wms_permissions(self, role):
def additional_wms_permissions(self, role, session):
"""Collect additional WMS Service permissions from ConfigDB
or service config.
These are permissions, e.g. for external info layers, which cannot be
collected from capabilities.
:param str role: Role name
:param Session session: DB session
"""
wms_services = []

if 'permissions' not in self.service_config:
# collect permissions from ConfigDB
session = self.config_models.session()

# helper method alias
non_public_resources = self.permissions_query.non_public_resources
permitted_resources = self.permissions_query.permitted_resources
Expand Down Expand Up @@ -396,8 +396,6 @@ def additional_wms_permissions(self, role):

if layers:
wms_services.append(wms_service)

session.close()
else:
# use permissions from additional service config if present
self.logger.debug("Reading permissions from tenantConfig")
Expand Down
11 changes: 4 additions & 7 deletions src/config_generator/legend_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ def permissions(self, role):
permissions = OrderedDict()

# collect permissions from ConfigDB
session = self.config_models.session()

# TODO: Collect permissions
# permissions['wms_services'] = self.wms_permissions(role, session)
permissions['wms_services'] = []

session.close()
with self.config_models.session() as session:
# TODO: Collect permissions
# permissions['wms_services'] = self.wms_permissions(role, session)
permissions['wms_services'] = []

return permissions

Expand Down
105 changes: 49 additions & 56 deletions src/config_generator/map_viewer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,23 @@ def permissions(self, role):
permissions = OrderedDict()

# collect permissions from ConfigDB
session = self.config_models.session()

# NOTE: WMS service permissions collected by OGC service config
permissions['wms_services'] = []
permissions['background_layers'] = self.permitted_background_layers(
role
)
# NOTE: Data permissions collected by Data service config
permissions['data_datasets'] = []
permissions['viewer_tasks'] = self.permitted_viewer_tasks(
role, session
)
permissions['theme_info_links'] = self.permitted_theme_info_links(
role, session
)
permissions['plugin_data'] = self.permitted_plugin_data_resources(
role, session
)

session.close()
with self.config_models.session() as session:
# NOTE: WMS service permissions collected by OGC service config
permissions['wms_services'] = []
permissions['background_layers'] = self.permitted_background_layers(
role
)
# NOTE: Data permissions collected by Data service config
permissions['data_datasets'] = []
permissions['viewer_tasks'] = self.permitted_viewer_tasks(
role, session
)
permissions['theme_info_links'] = self.permitted_theme_info_links(
role, session
)
permissions['plugin_data'] = self.permitted_plugin_data_resources(
role, session
)

return permissions

Expand Down Expand Up @@ -198,11 +195,10 @@ def qwc2_config(self):

def restricted_viewer_tasks(self):
"""Collect restricted viewer tasks from ConfigDB."""
session = self.config_models.session()
viewer_tasks = self.permissions_query.non_public_resources(
'viewer_task', session
)
session.close()
with self.config_models.session() as session:
viewer_tasks = self.permissions_query.non_public_resources(
'viewer_task', session
)

return sorted(list(viewer_tasks))

Expand Down Expand Up @@ -789,37 +785,34 @@ def edit_config(self, map_name, cfg_item, assets_dir):
Permission = self.config_models.model('permissions')
Resource = self.config_models.model('resources')

session = self.config_models.session()

# find map resource
query = session.query(Resource) \
.filter(Resource.type == 'map') \
.filter(Resource.name == map_name)
map_id = None
for map_obj in query.all():
map_id = map_obj.id

if map_id is None:
# map not found
return edit_config

# query writable data permissions
resource_types = [
'data',
'data_create', 'data_read', 'data_update', 'data_delete'
]
datasets_query = session.query(Permission) \
.join(Permission.resource) \
.filter(Resource.parent_id == map_obj.id) \
.filter(Resource.type.in_(resource_types)) \
.distinct(Resource.name, Resource.type) \
.order_by(Resource.name)

edit_datasets = []
for permission in datasets_query.all():
edit_datasets.append(permission.resource.name)

session.close()
with self.config_models.session() as session:
# find map resource
query = session.query(Resource) \
.filter(Resource.type == 'map') \
.filter(Resource.name == map_name)
map_id = None
for map_obj in query.all():
map_id = map_obj.id

if map_id is None:
# map not found
return edit_config

# query writable data permissions
resource_types = [
'data',
'data_create', 'data_read', 'data_update', 'data_delete'
]
datasets_query = session.query(Permission) \
.join(Permission.resource) \
.filter(Resource.parent_id == map_obj.id) \
.filter(Resource.type.in_(resource_types)) \
.distinct(Resource.name, Resource.type) \
.order_by(Resource.name)

edit_datasets = []
for permission in datasets_query.all():
edit_datasets.append(permission.resource.name)

if not edit_datasets:
# no edit datasets for this map
Expand Down
9 changes: 3 additions & 6 deletions src/config_generator/ogc_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ def permissions(self, role):
permissions = OrderedDict()

# collect permissions from ConfigDB
session = self.config_models.session()

permissions['wms_services'] = self.wms_permissions(role, session)
permissions['wfs_services'] = self.wfs_permissions(role, session)

session.close()
with self.config_models.session() as session:
permissions['wms_services'] = self.wms_permissions(role, session)
permissions['wfs_services'] = self.wfs_permissions(role, session)

return permissions

Expand Down
13 changes: 5 additions & 8 deletions src/config_generator/permissions_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ def base_config(self):
# NOTE: use ordered keys
permissions = OrderedDict()

session = self.config_models.session()

permissions['$schema'] = self.schema
permissions['users'] = self.users(session)
permissions['groups'] = self.groups(session)
permissions['roles'] = self.roles(session)

session.close()
with self.config_models.session() as session:
permissions['$schema'] = self.schema
permissions['users'] = self.users(session)
permissions['groups'] = self.groups(session)
permissions['roles'] = self.roles(session)

return permissions

Expand Down
15 changes: 6 additions & 9 deletions src/config_generator/permissions_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,12 @@ def load_resources_lookup(self):
"""Load resources for lookup from ConfigDB."""
resources_lookup = {}

session = self.config_models.session()

# collect resources lookup from ConfigDB
Resource = self.config_models.model('resources')
query = session.query(Resource).order_by(Resource.type)
for resource in query.all():
resources_lookup[resource.id] = resource

session.close()
with self.config_models.session() as session:
# collect resources lookup from ConfigDB
Resource = self.config_models.model('resources')
query = session.query(Resource).order_by(Resource.type)
for resource in query.all():
resources_lookup[resource.id] = resource

return resources_lookup

Expand Down
Loading

0 comments on commit 17777e0

Please sign in to comment.