From 4ff0450632373e3958e56f143f6a0100ef6a85f3 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 3 Aug 2022 14:44:38 -0500 Subject: [PATCH] Stop using the old `rattail.db.api.settings` module --- tailbone/grids/core.py | 10 ++++++---- tailbone/util.py | 23 ++++++++++++++++++----- tailbone/views/email.py | 19 ++++++++++--------- tailbone/views/settings.py | 5 +++-- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/tailbone/grids/core.py b/tailbone/grids/core.py index 95465b1e..5360c894 100644 --- a/tailbone/grids/core.py +++ b/tailbone/grids/core.py @@ -34,7 +34,6 @@ from six.moves import urllib import sqlalchemy as sa from sqlalchemy import orm -from rattail.db import api from rattail.db.types import GPCType from rattail.util import prettify, pretty_boolean, pretty_quantity, pretty_hours from rattail.time import localtime @@ -743,7 +742,8 @@ class Grid(object): # User defaults should have all or nothing, so just check one key. key = 'tailbone.{}.grid.{}.sortkey'.format(user.uuid, self.key) - return api.get_setting(session, key) is not None + app = self.request.rattail_config.get_app() + return app.get_setting(Session(), key) is not None def apply_user_defaults(self, settings): """ @@ -751,7 +751,8 @@ class Grid(object): """ def merge(key, normalize=lambda v: v): skey = 'tailbone.{}.grid.{}.{}'.format(self.request.user.uuid, self.key, key) - value = api.get_setting(Session(), skey) + app = self.request.rattail_config.get_app() + value = app.get_setting(Session(), skey) settings[key] = normalize(value) if self.filterable: @@ -929,7 +930,8 @@ class Grid(object): def persist(key, value=lambda k: settings[k]): if to == 'defaults': skey = 'tailbone.{}.grid.{}.{}'.format(self.request.user.uuid, self.key, key) - api.save_setting(Session(), skey, value(key)) + app = self.request.rattail_config.get_app() + app.save_setting(Session(), skey, value(key)) else: # to == session skey = 'grid.{}.{}'.format(self.key, key) self.request.session[skey] = value(key) diff --git a/tailbone/util.py b/tailbone/util.py index 38b9a0c2..c7eabae6 100644 --- a/tailbone/util.py +++ b/tailbone/util.py @@ -174,8 +174,6 @@ def set_app_theme(request, theme, session=None): This also saves the setting for the new theme, and updates the running app registry settings with the new theme. """ - from rattail.db import api - theme = get_effective_theme(request.rattail_config, theme=theme, session=session) theme_path = get_theme_template_path(request.rattail_config, theme=theme, session=session) @@ -190,7 +188,16 @@ def set_app_theme(request, theme, session=None): # clear template cache for lookup object, so it will reload each (as needed) lookup._collection.clear() - api.save_setting(session, 'tailbone.theme', theme) + app = request.rattail_config.get_app() + close = False + if not session: + session = app.make_session() + close = True + app.save_setting(session, 'tailbone.theme', theme) + if close: + session.commit() + session.close() + request.registry.settings['tailbone.theme'] = theme @@ -209,10 +216,16 @@ def get_effective_theme(rattail_config, theme=None, session=None): Validates and returns the "effective" theme. If you provide a theme, that will be used; otherwise it is read from database setting. """ - from rattail.db import api + app = rattail_config.get_app() if not theme: - theme = api.get_setting(session, 'tailbone.theme') or 'default' + close = False + if not session: + session = app.make_session() + close = True + theme = app.get_setting(session, 'tailbone.theme') or 'default' + if close: + session.close() # confirm requested theme is available available = rattail_config.getlist('tailbone', 'themes', diff --git a/tailbone/views/email.py b/tailbone/views/email.py index a5687254..8a03f8a2 100644 --- a/tailbone/views/email.py +++ b/tailbone/views/email.py @@ -31,7 +31,7 @@ import re import six from rattail import mail -from rattail.db import api, model +from rattail.db import model from rattail.config import parse_list import colander @@ -213,15 +213,16 @@ class EmailSettingView(MasterView): def save_edit_form(self, form): key = self.request.matchdict['key'] data = self.form_deserialized + app = self.get_rattail_app() session = self.Session() - api.save_setting(session, 'rattail.mail.{}.prefix'.format(key), data['prefix']) - api.save_setting(session, 'rattail.mail.{}.subject'.format(key), data['subject']) - api.save_setting(session, 'rattail.mail.{}.from'.format(key), data['sender']) - api.save_setting(session, 'rattail.mail.{}.replyto'.format(key), data['replyto']) - api.save_setting(session, 'rattail.mail.{}.to'.format(key), (data['to'] or '').replace('\n', ', ')) - api.save_setting(session, 'rattail.mail.{}.cc'.format(key), (data['cc'] or '').replace('\n', ', ')) - api.save_setting(session, 'rattail.mail.{}.bcc'.format(key), (data['bcc'] or '').replace('\n', ', ')) - api.save_setting(session, 'rattail.mail.{}.enabled'.format(key), six.text_type(data['enabled']).lower()) + app.save_setting(session, 'rattail.mail.{}.prefix'.format(key), data['prefix']) + app.save_setting(session, 'rattail.mail.{}.subject'.format(key), data['subject']) + app.save_setting(session, 'rattail.mail.{}.from'.format(key), data['sender']) + app.save_setting(session, 'rattail.mail.{}.replyto'.format(key), data['replyto']) + app.save_setting(session, 'rattail.mail.{}.to'.format(key), (data['to'] or '').replace('\n', ', ')) + app.save_setting(session, 'rattail.mail.{}.cc'.format(key), (data['cc'] or '').replace('\n', ', ')) + app.save_setting(session, 'rattail.mail.{}.bcc'.format(key), (data['bcc'] or '').replace('\n', ', ')) + app.save_setting(session, 'rattail.mail.{}.enabled'.format(key), six.text_type(data['enabled']).lower()) return data def template_kwargs_view(self, **kwargs): diff --git a/tailbone/views/settings.py b/tailbone/views/settings.py index 7b10f1d0..6fc0e66f 100644 --- a/tailbone/views/settings.py +++ b/tailbone/views/settings.py @@ -31,7 +31,7 @@ import re import json import six -from rattail.db import model, api +from rattail.db import model from rattail.settings import Setting from rattail.util import import_module_path @@ -273,7 +273,8 @@ class AppSettingsView(View): value = ', '.join(entries) else: value = six.text_type(value) - api.save_setting(Session(), legacy_name, value) + app = self.get_rattail_app() + app.save_setting(Session(), legacy_name, value) def clean_list_entry(self, value): value = value.strip()