Changeset - 5cafa406378e
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 months ago 2024-08-06 18:51:58
lance@edbob.org
fix: move logic upstream for `save_setting()`, `delete_setting()`
1 file changed with 22 insertions and 34 deletions:
0 comments (0 inline, 0 general)
rattail/app.py
Show inline comments
 
@@ -366,60 +366,48 @@ class AppHandler(WuttaAppHandler):
 
                                    tzinfo=True if tzinfo is None else tzinfo)
 
            return dt
 

	
 
    def save_setting(self, session, name, value, typ=None,
 
                     force_create=False, **kwargs):
 
    def save_setting(
 
            self,
 
            session,
 
            name,
 
            value,
 
            typ=None,
 
            **kwargs,
 
    ):
 
        """
 
        Save a setting value to the DB.
 

	
 
        :param session: Current DB session.
 
        Save a :term:`config setting` value to the DB.
 

	
 
        :param name: Name of the setting to save.
 

	
 
        :param value: Value to be saved for the setting.
 
        Rattail overrides
 
        :meth:`wuttjamaican:wuttjamaican.app.AppHandler.save_setting()`
 
        to add the ``typ`` param.
 

	
 
        :param typ: Most values are treated as simple strings, but if
 
           you specify ``'utctime'`` here, then the ``value`` is
 
           assumed to be a UTC-based ``datetime`` object, and the
 
           final setting value will be formatted appropriately.
 

	
 
        :param force_create: If ``False`` (the default) then logic
 
           will first try to locate an existing setting of the given
 
           name, and update it if found, or create if not.  But if
 
           this param is ``True`` then logic will only try to create a
 
           new record, and not bother checking to see if it exists.
 
        """
 
        model = self.model
 

	
 
        # maybe coerce value
 
        if typ == 'utctime':
 
            if value:
 
                value = value.strftime(self.setting_utctime_format)
 
            else:
 
                value = None
 

	
 
        # create or update the setting
 
        setting = None
 
        if not force_create:
 
            setting = session.get(model.Setting, name)
 
        if not setting:
 
            setting = model.Setting(name=name)
 
            session.add(setting)
 
        setting.value = value
 
        # upstream save
 
        super().save_setting(session, name, value,
 
                             force_create=force_create,
 
                             **kwargs)
 

	
 
        # invalidate beaker config cache for this setting, if applicable
 
        self.config.beaker_invalidate_setting(name)
 

	
 
    def delete_setting(self, session, name, **kwargs):
 
        """
 
        Delete the given setting from the DB.
 

	
 
        :param session: Current DB session.
 
    # TODO: remove this
 
    def delete_setting(self, session, name):
 
        """ """
 

	
 
        :param name: Name of the setting to delete.
 
        """
 
        model = self.model
 
        setting = session.get(model.Setting, name)
 
        if setting:
 
            session.delete(setting)
 
        # upstream delete
 
        super().delete_setting(session, name)
 

	
 
        # invalidate beaker config cache for this setting, if applicable
 
        self.config.beaker_invalidate_setting(name)
0 comments (0 inline, 0 general)