app/soc/logic/cleaning.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 10 Jan 2009 18:17:44 +0000
changeset 801 06a84103ed8b
parent 788 892877b7db07
child 837 bc1c951bf3a0
permissions -rw-r--r--
Invite (backup-)admin when creating a club Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Generic cleaning methods
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Todd Larsen" <tlarsen@google.com>',
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
    ]
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
from django import forms
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from soc.logic import validate
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    29
from soc.logic.models import user as user_logic
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
def clean_new_link_id(logic):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  """Clean method for new link_id's
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
  Returns a clean method that checks if the specified link_id is
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  in the proper format, and verifies that the link_id is not already in
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  in use by another entity of the same type.
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  """
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
  def wrapped(self):
650
33b6dcae5615 Changed clean_link_id to convert the input to lower characters for user comfort.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 601
diff changeset
    41
    # convert to lowercase for user comfort
33b6dcae5615 Changed clean_link_id to convert the input to lower characters for user comfort.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 601
diff changeset
    42
    link_id = self.cleaned_data.get('link_id').lower()
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
    if not validate.isLinkIdFormatValid(link_id):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
      raise forms.ValidationError("This link ID is in wrong format.")
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    if logic.getFromFields(link_id=link_id):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
      raise forms.ValidationError("This link ID is already in use.")
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
    return link_id
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  return wrapped
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
def clean_link_id(self):
650
33b6dcae5615 Changed clean_link_id to convert the input to lower characters for user comfort.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 601
diff changeset
    53
  # convert to lowercase for user comfort
33b6dcae5615 Changed clean_link_id to convert the input to lower characters for user comfort.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 601
diff changeset
    54
  link_id = self.cleaned_data.get('link_id').lower()
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
  if not validate.isLinkIdFormatValid(link_id):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
    raise forms.ValidationError("This link ID is in wrong format.")
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  return link_id
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    60
def clean_existing_user(field_name):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    61
  """Check if the field_name field is a valid user.
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    62
  """
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    63
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    64
  def wrapped(self):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    65
    link_id = self.cleaned_data.get(field_name).lower()
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    66
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    67
    if not validate.isLinkIdFormatValid(link_id):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    68
      raise forms.ValidationError("This link ID is in wrong format.")
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    69
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    70
    user_entity = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    71
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    72
    if not user_entity:
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    73
      # user does not exist
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    74
      raise forms.ValidationError("This user does not exist")
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    75
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    76
    return user_entity
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    77
  return wrapped
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    78
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    79
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
def clean_feed_url(self):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  feed_url = self.cleaned_data.get('feed_url')
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
  if feed_url == '':
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
    # feed url not supplied (which is OK), so do not try to validate it
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    return None
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
  
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
  if not validate.isFeedURLValid(feed_url):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
  return feed_url