changeset 6227:555c8d26988f

db: always add to session in Setting.create_or_update There's no use case for not adding the newly created Setting to the SQLAlchemy session (and thus, once we commit, the database). With this change, all the various "create" model methods add their result to the session.
author Søren Løvborg <sorenl@unity3d.com>
date Tue, 13 Sep 2016 18:40:49 +0200
parents 590d5b7a2b26
children f35ddb654668
files kallithea/controllers/admin/auth_settings.py kallithea/controllers/admin/defaults.py kallithea/controllers/admin/settings.py kallithea/model/db.py kallithea/tests/conftest.py kallithea/tests/models/test_settings.py
diffstat 6 files changed, 12 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/auth_settings.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/controllers/admin/auth_settings.py	Tue Sep 13 18:40:49 2016 +0200
@@ -131,7 +131,6 @@
                     v = ','.join(v)
                 log.debug("%s = %s", k, str(v))
                 setting = Setting.create_or_update(k, v)
-                Session().add(setting)
             Session().commit()
             h.flash(_('Auth settings updated successfully'),
                        category='success')
--- a/kallithea/controllers/admin/defaults.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/controllers/admin/defaults.py	Tue Sep 13 18:40:49 2016 +0200
@@ -71,7 +71,6 @@
             form_result = _form.to_python(dict(request.POST))
             for k, v in form_result.iteritems():
                 setting = Setting.create_or_update(k, v)
-                Session().add(setting)
             Session().commit()
             h.flash(_('Default settings updated successfully'),
                     category='success')
--- a/kallithea/controllers/admin/settings.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/controllers/admin/settings.py	Tue Sep 13 18:40:49 2016 +0200
@@ -208,25 +208,14 @@
                     force_defaults=False)
 
             try:
-                sett1 = Setting.create_or_update('title',
-                                            form_result['title'])
-                Session().add(sett1)
-
-                sett2 = Setting.create_or_update('realm',
-                                            form_result['realm'])
-                Session().add(sett2)
-
-                sett3 = Setting.create_or_update('ga_code',
-                                            form_result['ga_code'])
-                Session().add(sett3)
-
-                sett4 = Setting.create_or_update('captcha_public_key',
-                                    form_result['captcha_public_key'])
-                Session().add(sett4)
-
-                sett5 = Setting.create_or_update('captcha_private_key',
-                                    form_result['captcha_private_key'])
-                Session().add(sett5)
+                for setting in (
+                    'title',
+                    'realm',
+                    'ga_code',
+                    'captcha_public_key',
+                    'captcha_private_key',
+                ):
+                    Setting.create_or_update(setting, form_result[setting])
 
                 Session().commit()
                 set_app_settings(config)
@@ -279,9 +268,7 @@
                     ('clone_uri_tmpl', 'clone_uri_tmpl', 'unicode'),
                 ]
                 for setting, form_key, type_ in settings:
-                    sett = Setting.create_or_update(setting,
-                                        form_result[form_key], type_)
-                    Session().add(sett)
+                    Setting.create_or_update(setting, form_result[form_key], type_)
 
                 Session().commit()
                 set_app_settings(config)
--- a/kallithea/model/db.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/model/db.py	Tue Sep 13 18:40:49 2016 +0200
@@ -271,6 +271,7 @@
             val = Optional.extract(val)
             type = Optional.extract(type)
             res = cls(key, val, type)
+            Session().add(res)
         else:
             res.app_settings_name = key
             if not isinstance(val, Optional):
--- a/kallithea/tests/conftest.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/tests/conftest.py	Tue Sep 13 18:40:49 2016 +0200
@@ -60,8 +60,7 @@
         k = kvt[0]
         v = kvt[1]
         t = kvt[2] if len(kvt) == 3 else 'unicode'
-        setting = Setting.create_or_update(k, v, t)
-        session.add(setting)
+        Setting.create_or_update(k, v, t)
     session.commit()
 
 
@@ -82,8 +81,7 @@
     for k, v, t in settings_snapshot:
         if t == 'list' and hasattr(v, '__iter__'):
             v = ','.join(v) # Quirk: must format list value manually.
-        s = Setting.create_or_update(k, v, t)
-        session.add(s)
+        Setting.create_or_update(k, v, t)
     session.commit()
 
 @pytest.yield_fixture
--- a/kallithea/tests/models/test_settings.py	Thu Sep 15 15:13:27 2016 +0200
+++ b/kallithea/tests/models/test_settings.py	Tue Sep 13 18:40:49 2016 +0200
@@ -7,7 +7,6 @@
 def test_passing_list_setting_value_results_in_string_valued_setting():
     assert Setting.get_by_name(name) is None
     setting = Setting.create_or_update(name, ['spam', 'eggs'])
-    Session().add(setting)
     Session().flush()
     try:
         assert Setting.get_by_name(name) is not None
@@ -23,7 +22,6 @@
     assert Setting.get_by_name(name) is None
     # Quirk: need manual formatting of list setting value.
     setting = Setting.create_or_update(name, 'spam,eggs', type='list')
-    Session().add(setting)
     Session().flush()
     try:
         assert setting.app_settings_value == ['spam', 'eggs']