[ckan-changes] [okfn/ckan] eeb1cc: [2347] Add a url validator to the related schema

GitHub noreply at github.com
Fri May 4 09:51:28 UTC 2012


  Branch: refs/heads/feature-2347-related-dashboard
  Home:   https://github.com/okfn/ckan
  Commit: eeb1cc954e0cc7604edbdcc6fb3a95d3b91c5f73
      https://github.com/okfn/ckan/commit/eeb1cc954e0cc7604edbdcc6fb3a95d3b91c5f73
  Author: Ross Jones <rossdjones at gmail.com>
  Date:   2012-05-04 (Fri, 04 May 2012)

  Changed paths:
    M ckan/logic/schema.py
    M ckan/logic/validators.py

  Log Message:
  -----------
  [2347] Add a url validator to the related schema


diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
index 4c174e7..8bcbdfd 100644
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -37,7 +37,8 @@
                                    user_id_exists,
                                    object_id_validator,
                                    activity_type_exists,
-                                   tag_not_in_vocabulary)
+                                   tag_not_in_vocabulary,
+                                   url_validator)
 from formencode.validators import OneOf
 import ckan.model
 
@@ -241,8 +242,8 @@ def default_related_schema():
         'title': [not_empty, unicode],
         'description': [ignore_missing, unicode],
         'type': [not_empty, unicode],
-        'image_url': [ignore_missing, unicode],
-        'url': [ignore_missing, unicode],
+        'image_url': [ignore_missing, unicode, url_validator],
+        'url': [ignore_missing, unicode, url_validator],
         'owner_id': [not_empty, unicode],
         'created': [ignore],
         'featured': [ignore_missing, unicode],
diff --git a/ckan/logic/validators.py b/ckan/logic/validators.py
index 3f58a78..9451d4d 100644
--- a/ckan/logic/validators.py
+++ b/ckan/logic/validators.py
@@ -492,3 +492,23 @@ def tag_not_in_vocabulary(key, tag_dict, errors, context):
                 (tag_name, vocabulary_id))
     else:
         return
+
+def url_validator(key, data, errors, context):
+    """ Checks that the provided value (if it is present) is a valid URL """
+    import urlparse
+    import string
+
+    model = context['model']
+    session = context['session']
+
+    url = data.get(key, None)
+    if not url:
+        return
+
+    pieces = urlparse.urlparse(url)
+    if all([pieces.scheme, pieces.netloc]) and \
+       set(pieces.netloc) <= set(string.letters + string.digits + '-.') and \
+       pieces.scheme in ['http', 'https']:
+       return
+
+    errors[key].append(_('Please provide a valid URL'))


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



More information about the ckan-changes mailing list