app/soc/logic/validate.py
changeset 249 325fb70c61a9
parent 245 b14c2c4d3484
child 251 8f23804302d0
equal deleted inserted replaced
248:95e0b84e71c5 249:325fb70c61a9
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Common validation helper functions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 import re
       
    26 
       
    27 from google.appengine.api import urlfetch
       
    28 
       
    29 import feedparser
       
    30 
       
    31 
       
    32 def isFeedURLValid(feed_url=None):
       
    33   """Returns True if provided url is valid ATOM or RSS.
       
    34 
       
    35   Args:
       
    36     feed_url: ATOM or RSS feed url
       
    37   """
       
    38   if feed_url:
       
    39     result = urlfetch.fetch(feed_url)
       
    40     if result.status_code == 200:
       
    41       parsed_feed = feedparser.parse(result.content)
       
    42       if parsed_feed.version and (parsed_feed.version != ''):
       
    43         return True
       
    44   return False
       
    45 
       
    46 
       
    47 LINKNAME_PATTERN = r'''(?x)
       
    48     ^
       
    49     [0-9a-z]   # start with ASCII digit or lowercase
       
    50     (
       
    51      [0-9a-z]  # additional ASCII digit or lowercase
       
    52      |         # -OR-
       
    53      _[0-9a-z] # underscore and ASCII digit or lowercase
       
    54     )*         # zero or more of OR group
       
    55     $
       
    56 '''
       
    57 
       
    58 LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
       
    59 
       
    60 def isLinkNameFormatValid(link_name):
       
    61   """Returns True if link_name is in a valid format.
       
    62 
       
    63   Args:
       
    64     link_name: link name used in URLs for identification
       
    65   """
       
    66   if LINKNAME_REGEX.match(link_name):
       
    67     return True
       
    68   return False