Browse Source

Updating the changed values works now.

sh4nks 11 years ago
parent
commit
960c5bfbaf
2 changed files with 34 additions and 15 deletions
  1. 11 2
      flaskbb/admin/models.py
  2. 23 13
      flaskbb/admin/views.py

+ 11 - 2
flaskbb/admin/models.py

@@ -217,14 +217,23 @@ class Setting(db.Model):
             app.config.update(updated_settings)
 
     @classmethod
-    def as_dict(cls, upper=False):
+    def as_dict(cls, from_group=None, upper=False):
         """Returns the settings key and value as a dict.
 
+        :param from_group: Returns only the settings from the group as a dict.
         :param upper: If upper is ``True``, the key will use upper-case
                       letters. Defaults to ``False``.
         """
         settings = {}
-        for setting in cls.query.all():
+        result = None
+        if from_group is not None:
+            result = SettingsGroup.query.filter_by(key=from_group).\
+                first_or_404()
+            result = result.settings
+        else:
+            result = cls.query.all()
+
+        for setting in result:
             if upper:
                 settings[setting.key.upper()] = setting.value
             else:

+ 23 - 13
flaskbb/admin/views.py

@@ -55,23 +55,33 @@ def settings(slug=None):
     # Get all settinggroups so that we can build the settings navigation
     settingsgroup = SettingsGroup.query.all()
 
-    if slug is not None:
-        SettingsForm = Setting.get_form(slug)
-    else:
-        # or should we display an index with all available settingsgroups?
-        SettingsForm = Setting.get_form("general")
+    slug = slug if slug else "general"
+
+    SettingsForm = Setting.get_form(slug)
 
-    # TODO: Only get those settings from the group
-    #old_settings = Setting.as_dict()
-    #new_settings = {}
+    old_settings = Setting.as_dict(from_group=slug)
+    new_settings = {}
 
     form = SettingsForm()
 
-    if request.method == "POST":
-        if form.validate():
-            print form.data
-        else:
-            print form.errors
+    if form.validate_on_submit():
+        for key, value in old_settings.iteritems():
+            try:
+                # check if the value has changed
+                if value == form.__dict__[key].data:
+                    continue
+                else:
+                    new_settings[key] = form.__dict__[key].data
+            except (KeyError, ValueError):
+                pass
+
+        Setting.update(settings=new_settings, app=current_app)
+    else:
+        for key, value in old_settings.iteritems():
+            try:
+                form.__dict__[key].data = value
+            except (KeyError, ValueError):
+                pass
 
     return render_template("admin/settings.html", form=form,
                            settingsgroup=settingsgroup)