Improve 'settings' API functions so they don't require a session.
Seems way handier that way, sometimes.
This commit is contained in:
parent
e402352dd6
commit
6ffd7235ff
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2014 Lance Edgar
|
||||
# Copyright © 2010-2015 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -26,27 +26,37 @@ Settings API
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .. import model
|
||||
|
||||
|
||||
__all__ = ['get_setting', 'save_setting']
|
||||
from rattail.db import Session, model
|
||||
|
||||
|
||||
def get_setting(session, name):
|
||||
"""
|
||||
Returns a setting value from the database.
|
||||
"""
|
||||
local_session = False
|
||||
if session is None:
|
||||
session = Session()
|
||||
local_session = True
|
||||
setting = session.query(model.Setting).get(name)
|
||||
if setting:
|
||||
return setting.value
|
||||
value = None if setting is None else setting.value
|
||||
if local_session:
|
||||
session.close()
|
||||
return value
|
||||
|
||||
|
||||
def save_setting(session, name, value):
|
||||
"""
|
||||
Saves a setting to the database.
|
||||
"""
|
||||
local_session = False
|
||||
if session is None:
|
||||
session = Session()
|
||||
local_session = True
|
||||
setting = session.query(model.Setting).get(name)
|
||||
if not setting:
|
||||
setting = model.Setting(name=name)
|
||||
session.add(setting)
|
||||
setting.value = value
|
||||
if local_session:
|
||||
session.commit()
|
||||
session.close()
|
||||
|
|
Loading…
Reference in a new issue