[ckan-changes] [okfn/ckan] f9e151: [2302] Adding validation and POST handling to the ...

GitHub noreply at github.com
Fri Apr 20 11:13:28 UTC 2012


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

  Changed paths:
    M ckan/controllers/settings.py
    M ckan/logic/schema.py
    M ckan/templates/settings/index.html

  Log Message:
  -----------
  [2302] Adding validation and POST handling to the controller


diff --git a/ckan/controllers/settings.py b/ckan/controllers/settings.py
index aecabc6..b84fc7b 100644
--- a/ckan/controllers/settings.py
+++ b/ckan/controllers/settings.py
@@ -1,15 +1,26 @@
-import random
-
 from pylons.i18n import set_lang
 import sqlalchemy.exc
 
 import ckan.authz as authz
-
+import ckan.lib.navl.dictization_functions as dictfunc
 import ckan.logic as logic
+import ckan.logic.action as action
+
 from ckan.lib.base import *
 from ckan.lib.helpers import url_for
 
 
+def _ensure_hex(key, data, errors, context):
+    value = data.get(key)
+
+    def is_hex(value):
+        return True
+
+    if not value or not is_hex(value):
+        data.pop(key, None)
+        errors[key] = _("Value must be a hexadecimal number")
+        raise dictfunc.StopOnError
+
 class SettingsController(BaseController):
     repo = model.repo
 
@@ -18,6 +29,30 @@ def __before__(self, action, **env):
         if not authz.Authorizer.is_sysadmin(c.user):
             abort(401, _('Not authorized to see this page'))
 
-    def index(self):
-        return render('settings/index.html', cache_force=True)
 
+    def index(self):
+        data, errors, error_summary = {}, {}, {}
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author }
+
+        if request.method == 'POST':
+            td = logic.tuplize_dict( logic.parse_params(request.params) )
+            unflattened = dictfunc.unflatten( td )
+            data = logic.clean_dict( unflattened )
+
+            schema = logic.schema.default_settings_schema(_ensure_hex)
+            data, errors = dictfunc.validate(data, schema)
+            if errors:
+                e = logic.ValidationError(errors, action.error_summary(errors))
+                errors = e.error_dict
+                error_summary = e.error_summary
+            else:
+                # Persist the data
+                pass
+
+        extras = {'data':data, 'error_summary': error_summary, "errors": errors}
+        self._setup_template_variables()
+        return render('settings/index.html',extra_vars=extras)
+
+    def _setup_template_variables(self):
+        pass
\ No newline at end of file
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
index b07d2bb..f88a2c7 100644
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -279,6 +279,15 @@ def default_update_relationship_schema():
     return schema
 
 
+def default_settings_schema(hex_func):
+    return {
+        "image_url": [ignore, unicode],
+        "name"     : [not_empty, unicode],
+        "tagline"  : [not_empty, unicode],
+        "css_header"    : [ignore_missing, unicode, hex_func],
+        "css_background": [ignore_missing, unicode, hex_func],
+        "css_footer"    : [ignore_missing, unicode, hex_func],
+    }
 
 
 def default_user_schema():
diff --git a/ckan/templates/settings/index.html b/ckan/templates/settings/index.html
index 3981535..f96923b 100644
--- a/ckan/templates/settings/index.html
+++ b/ckan/templates/settings/index.html
@@ -20,62 +20,71 @@
   </py:match>
 
   <div py:match="content">
-      <form class="form-horizontal well">
+
+    <div class="alert alert-error" py:if="error_summary">
+        The form contains invalid entries please correct them and submit again
+    </div>
+
+      <form class="form-horizontal well" method="POST">
           <fieldset>
               <legend>Settings</legend>
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('image_url', '') else '' }">
                   <label class="control-label" for="input01">Logo url</label>
                   <div class="controls">
-                      <div class="input-prepend">
-                      <span class='add-on'>http://</span><input type="text" class="input-xlarge" id="input01"/>
-                      </div>
+                      <input type="text" class="input-xlarge" id="image_url" name="image_url" value="${data.get('image_url', g.site_url + '/img/logo.png' )}"/>
                       <p class="help-block">The image loaded from this URL should be 145x120 px</p>
+                      <span class="help-inline" py:if="errors.get('image_url')">${errors['image_url']}</span>
                   </div>
               </div>
 
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('name', '') else '' }">
                   <label class="control-label" for="input01">Site name</label>
                   <div class="controls">
-                      <input type="text" class="input-xlarge" id="input01"/>
+                      <input type="text" class="input-xlarge" id="name" name="name"  value="${data.get('name','')}"/>
+                      <span class="help-inline" py:if="errors.get('name')">${errors['name']}</span>
                   </div>
               </div>
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('tagline', '') else '' }">
                   <label class="control-label" for="input01">Tag line</label>
                   <div class="controls">
-                      <input type="text" class="input-xlarge" id="input01"/>
+                      <input type="text" class="input-xlarge" id="tagline" name="tagline"  value="${data.get('tagline','')}"/>
+                      <span class="help-inline" py:if="errors.get('tagline')">${errors['tagline']}</span>
                   </div>
               </div>
 
           </fieldset>
           <fieldset>
               <legend>CSS</legend>
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('css_header', '') else '' }">
                   <label class="control-label" for="input01">Header background</label>
                   <div class="controls">
                       <div class="input-prepend">
-                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="input01"/>
+                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="css_header" name="css_header"  value="${data.get('css_header','')}"/>
                       </div>
-                      <p class="help-block">The CSS hex value for the header</p>
+                      <span class="help-inline" py:if="not errors.get('css_header')">Hexadecimal value for the header</span>
+                      <span class="help-inline" py:if="errors.get('css_header')">${errors['css_header']}</span>
                   </div>
               </div>
 
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('css_background', '') else '' }">
                   <label class="control-label" for="input01">Background</label>
                   <div class="controls">
                       <div class="input-prepend">
-                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="input01"/>
+                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="css_background" name="css_background"  value="${data.get('css_background','')}"/>
                       </div>
-                      <p class="help-block">The CSS hex value for the main background</p>
+                      <span class="help-inline" py:if="not errors.get('css_background')">Hexadecimal value for the main background</span>
+                      <span class="help-inline" py:if="errors.get('css_background')">${errors['css_background']}</span>
                   </div>
               </div>
 
-              <div class="control-group">
+              <div class="control-group ${ 'error' if errors.get('css_footer', '') else '' }">
                   <label class="control-label" for="input01">Footer background</label>
                   <div class="controls">
                       <div class="input-prepend">
-                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="input01"/>
+                      <span class='add-on'>#</span><input type="text" class="input-xlarge" id="css_footer" name="css_footer"  value="${data.get('css_footer','')}"/>
                       </div>
-                      <p class="help-block">The CSS hex value for the footer</p>
+                      <span class="help-inline" py:if="not errors.get('css_footer')">Hexadecimal value for the footer</span>
+                      <span class="help-inline" py:if="errors.get('css_footer')">${errors['css_footer']}</span>
                   </div>
               </div>
           </fieldset>


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



More information about the ckan-changes mailing list