app/soc/logic/validate.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 25 Aug 2009 11:43:14 +0200
changeset 2795 776aae4d0499
parent 1656 9ffee5d1fe17
permissions -rw-r--r--
Set new Melange version number to 0-5-20090825 in app.yaml.template.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    17
"""Common validation helper functions.
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
462
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
  ]
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
322
6641e941ef1e Fixed imports sorting based on comments for r751.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    26
import feedparser
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    28
from google.appengine.api import urlfetch
1532
ea80be783971 Added a missing import
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1501
diff changeset
    29
from google.appengine.api import urlfetch_errors
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    30
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 513
diff changeset
    31
from soc.models import linkable
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    32
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    33
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    34
def isFeedURLValid(feed_url=None):
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    35
  """Returns True if provided url is valid ATOM or RSS.
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    36
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    37
  Args:
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    38
    feed_url: ATOM or RSS feed url
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    39
  """
1501
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    40
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    41
  # a missing or empty feed url is never valid
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    42
  if not feed_url:
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    43
    return False
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    44
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    45
  try:
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    46
    result = urlfetch.fetch(feed_url)
1656
9ffee5d1fe17 Remove unused variable in soc.logic.validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1532
diff changeset
    47
  except urlfetch_errors.Error:
1501
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    48
    return False
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    49
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    50
  # 200 is the status code for 'all ok'
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    51
  if result.status_code != 200:
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    52
    return False
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    53
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    54
  parsed_feed = feedparser.parse(result.content)
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    55
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    56
  # version is always present if the feed is valid
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    57
  if not parsed_feed.version:
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    58
    return False
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    59
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    60
  # TODO: isn't this check redunant?
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    61
  if parsed_feed.version == '':
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    62
    return False
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    63
3601ddfe785a Make validate.isURLValid more robust
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    64
  return True
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    65
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    67
def isLinkIdFormatValid(link_id):
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    68
  """Returns True if link_id is in a valid format.
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
  Args:
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    71
    link_id: link ID used in URLs for identification
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
  """
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 513
diff changeset
    73
  if linkable.LINK_ID_REGEX.match(link_id):
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
    return True
462
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    75
  return False
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    76
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    77
513
3c1e16637ad7 Rename partial_path to scope_path, ignoring case, etc.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    78
def isScopePathFormatValid(scope_path):
3c1e16637ad7 Rename partial_path to scope_path, ignoring case, etc.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    79
  """Returns True if scope_path is in a valid format.
462
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    80
  
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    81
  Args:
513
3c1e16637ad7 Rename partial_path to scope_path, ignoring case, etc.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    82
    scope_path: scope path prepended to link ID
462
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    83
      used for identification.
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    84
  """
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    85
   
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 513
diff changeset
    86
  if linkable.SCOPE_PATH_REGEX.match(scope_path):
462
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    87
    return True
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    88
  
1f164cd0529b 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    89
  return False