Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
authorPawel Solyga <Pawel.Solyga@gmail.com>
Mon, 10 Nov 2008 14:40:16 +0000
changeset 462 1f164cd0529b
parent 461 fca6a8b5ae34
child 463 b3a538f81a50
Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods. Patch by: Lennard de Rijk Review by: Pawel Solyga
app/soc/logic/path_link_name.py
app/soc/logic/validate.py
app/soc/views/models/docs.py
--- a/app/soc/logic/path_link_name.py	Mon Nov 10 00:42:41 2008 +0000
+++ b/app/soc/logic/path_link_name.py	Mon Nov 10 14:40:16 2008 +0000
@@ -19,6 +19,7 @@
 
 __authors__ = [
   '"Todd Larsen" <tlarsen@google.com>',
+  '"Lennard de Rijk" <ljvderijk@gmail.com>',
   ]
 
 
@@ -36,14 +37,23 @@
 LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
 
 # partial path is multiple link_name chunks,
+# each separated by a trailing /
+# (at least 1)
+PARTIAL_PATH_ARG_PATTERN = (r'(?P<partial_path>%(link_name)s'
+                             '(?:/%(link_name)s)*)' % {
+                               'link_name': LINKNAME_PATTERN_CORE})
+PARTIAL_PATH_PATTERN = r'^%s$' % PARTIAL_PATH_ARG_PATTERN
+PARTIAL_PATH_REGEX = re.compile(PARTIAL_PATH_PATTERN)
+
+# path is multiple link_name chunks,
 #   each separated by a trailing /
 #     (at least 1)
 # followed by a single link_name with no trailing /
 PATH_LINKNAME_ARGS_PATTERN = (
-    r'(?P<partial_path>%(link_name)s(?:/%(link_name)s)*)/'
+    r'%(partial_path)s/'
      '(?P<link_name>%(link_name)s)' % {
-        'link_name': LINKNAME_PATTERN_CORE})
-
+       'partial_path' : PARTIAL_PATH_ARG_PATTERN,
+       'link_name': LINKNAME_PATTERN_CORE})
 PATH_LINKNAME_PATTERN = r'^%s$' % PATH_LINKNAME_ARGS_PATTERN
 PATH_LINKNAME_REGEX = re.compile(PATH_LINKNAME_PATTERN)
 
--- a/app/soc/logic/validate.py	Mon Nov 10 00:42:41 2008 +0000
+++ b/app/soc/logic/validate.py	Mon Nov 10 14:40:16 2008 +0000
@@ -18,6 +18,7 @@
 """
 
 __authors__ = [
+  '"Lennard de Rijk" <ljvderijk@gmail.com>',
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
@@ -52,4 +53,18 @@
   """
   if path_link_name.LINKNAME_REGEX.match(link_name):
     return True
-  return False
\ No newline at end of file
+  return False
+
+
+def isPartialPathFormatValid(partial_path):
+  """Returns True if partial_path is in a valid format.
+  
+  Args:
+    partial_path: partial path prepended to link name
+      used for identification.
+  """
+   
+  if path_link_name.PARTIAL_PATH_REGEX.match(partial_path):
+    return True
+  
+  return False
--- a/app/soc/views/models/docs.py	Mon Nov 10 00:42:41 2008 +0000
+++ b/app/soc/views/models/docs.py	Mon Nov 10 14:40:16 2008 +0000
@@ -19,6 +19,7 @@
 
 __authors__ = [
     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+    '"Lennard de Rijk" <ljvderijk@gmail.com>',
     '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
@@ -56,11 +57,15 @@
   def clean_partial_path(self):
     partial_path = self.cleaned_data.get('partial_path')
     # TODO(tlarsen): combine path and link_name and check for uniqueness
+    if not validate.isPartialPathFormatValid(partial_path):
+      raise forms.ValidationError("This partial path is in wrong format.")
     return partial_path
 
   def clean_link_name(self):
     link_name = self.cleaned_data.get('link_name')
     # TODO(tlarsen): combine path and link_name and check for uniqueness
+    if not validate.isLinkNameFormatValid(link_name):
+      raise forms.ValidationError("This link name is in wrong format.")
     return link_name