[ckan-changes] [okfn/ckan] 143cec: [2302] Hooked up the forms to the model for writin...

GitHub noreply at github.com
Mon Apr 23 10:19:02 UTC 2012


  Branch: refs/heads/feature-2302-simple-theming
  Home:   https://github.com/okfn/ckan
  Commit: 143cec299be84f3418d503de1d3aff309866b948
      https://github.com/okfn/ckan/commit/143cec299be84f3418d503de1d3aff309866b948
  Author: Ross Jones <rossdjones at gmail.com>
  Date:   2012-04-23 (Mon, 23 Apr 2012)

  Changed paths:
    M ckan/controllers/settings.py
    M ckan/logic/schema.py
    M ckan/migration/versions/054_add_setting_table.py
    M ckan/model/setting.py
    M ckan/templates/settings/index.html

  Log Message:
  -----------
  [2302] Hooked up the forms to the model for writing/reading settings in the kv store in the db


diff --git a/ckan/controllers/settings.py b/ckan/controllers/settings.py
index 1f99cf6..09c8c7d 100644
--- a/ckan/controllers/settings.py
+++ b/ckan/controllers/settings.py
@@ -40,7 +40,11 @@ def index(self):
         data, errors, error_summary = {}, {}, {}
         context = {'model': model, 'session': model.Session,
                    'user': c.user or c.author }
+        fields = ['name', 'image_url',
+                  'tagline', 'css_header',
+                  'css_footer', 'css_background']
 
+        success = False
         if request.method == 'POST':
             td = logic.tuplize_dict( logic.parse_params(request.params) )
             unflattened = dictfunc.unflatten( td )
@@ -53,10 +57,14 @@ def index(self):
                 errors = e.error_dict
                 error_summary = e.error_summary
             else:
-                # Persist the data
-                pass
+                for f in fields:
+                    model.Setting.set_value( f, data.get(f, ""), c.user )
+                    success = True
+        else:
+            data = dict(model.Setting.get_values(fields))
 
-        extras = {'data':data, 'error_summary': error_summary, "errors": errors}
+        extras = { "data" : data, "error_summary" : error_summary,
+                   "errors" : errors, "success" : success}
         self._setup_template_variables()
         return render('settings/index.html',extra_vars=extras)
 
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
index f88a2c7..5b3c7d3 100644
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -281,7 +281,7 @@ def default_update_relationship_schema():
 
 def default_settings_schema(hex_func):
     return {
-        "image_url": [ignore, unicode],
+        "image_url": [ignore_missing, unicode],
         "name"     : [not_empty, unicode],
         "tagline"  : [not_empty, unicode],
         "css_header"    : [ignore_missing, unicode, hex_func],
diff --git a/ckan/migration/versions/054_add_setting_table.py b/ckan/migration/versions/054_add_setting_table.py
index d8e3cf8..df58c80 100644
--- a/ckan/migration/versions/054_add_setting_table.py
+++ b/ckan/migration/versions/054_add_setting_table.py
@@ -8,7 +8,9 @@ def upgrade(migrate_engine):
         CREATE TABLE setting (
         	id text NOT NULL,
         	key text,
-        	value text
+        	value text,
+        	owner text,
+        	updated timestamp without time zone
         );
 
         ALTER TABLE setting
diff --git a/ckan/model/setting.py b/ckan/model/setting.py
index ab4fd27..0c0241c 100644
--- a/ckan/model/setting.py
+++ b/ckan/model/setting.py
@@ -1,11 +1,11 @@
 import datetime
 
-
 import core
 import meta
 import types
 import sqlalchemy as sa
 
+
 __all__ = ['Setting']
 
 setting_table = meta.Table('setting', meta.metadata,
@@ -14,15 +14,31 @@
                                   default=types.make_uuid),
                      meta.Column('key', sa.types.UnicodeText),
                      meta.Column('value', sa.types.UnicodeText),
-                     )
+                     meta.Column('owner', sa.types.UnicodeText),
+                     meta.Column('updated', sa.types.DateTime, default=datetime.datetime.now)
+                )
 
 class Setting(core.DomainObject):
 
     @classmethod
     def get_values(cls, keys):
-        query = Session.query(cls)
-        for k in keys:
-            query = query.filter(cls.key == k)
+        query = meta.Session.query(Setting).\
+                filter(Setting.key.in_(keys))
         return [ (s.key, s.value,) for s in query.all() ]
 
+    @classmethod
+    def set_value(cls, key, value, user):
+        setting = meta.Session.query(cls).\
+                    filter(cls.key == key).first()
+        if setting:
+            setting.value = value
+            setting.owner = user
+            setting.updated = datetime.datetime.now()
+        else:
+            setting = Setting( key=key, value=value,
+                                updated=datetime.datetime.now(),
+                                owner=user  )
+        meta.Session.add( setting )
+        meta.Session.commit()
+
 meta.mapper(Setting, setting_table)
diff --git a/ckan/templates/settings/index.html b/ckan/templates/settings/index.html
index 8480e27..bc83e7c 100644
--- a/ckan/templates/settings/index.html
+++ b/ckan/templates/settings/index.html
@@ -20,8 +20,14 @@
   </py:match>
 
   <div py:match="content">
+    <div class="alert alert-success" py:if="success">
+        The settings have been saved
+        <a class="close" onclick="$(this).parent().hide();">×</a>
+    </div>
+
     <div class="alert alert-error" py:if="error_summary">
         The form contains invalid entries please correct them and submit again
+        <a class="close" onclick="$(this).parent().hide();">×</a>
     </div>
 
       <form class="form-horizontal well" method="POST">


================================================================



More information about the ckan-changes mailing list