app/soc/logic/cleaning.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sat, 24 Jan 2009 10:57:53 +0000
changeset 946 db4134853d64
parent 928 df051fc9d7a1
child 967 be5f5533a07f
permissions -rw-r--r--
Add missing dots in soc.views.helper.redirects module function docstrings. Patch by: Pawel Solyga Review by: to-be-reviewed
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>',
928
df051fc9d7a1 Replaced the boolean properties in soc/models/request.py with a state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 905
diff changeset
    23
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
601
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
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from django import forms
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
from soc.logic import validate
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    30
from soc.logic.models import user as user_logic
601
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
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
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
    34
  # 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
    35
  link_id = self.cleaned_data.get('link_id').lower()
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  if not validate.isLinkIdFormatValid(link_id):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
    raise forms.ValidationError("This link ID is in wrong format.")
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  return link_id
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
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    41
def clean_existing_user(field_name):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    42
  """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
    43
  """
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    44
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    45
  def wrapped(self):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    46
    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
    47
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    48
    if not validate.isLinkIdFormatValid(link_id):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    49
      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
    50
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    51
    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
    52
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    53
    if not user_entity:
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    54
      # user does not exist
837
bc1c951bf3a0 Add missing blank line in soc.views.helper.params module. Fix docstring typo in soc.views.models.role module. Add missing dots at the end of sentences in soc.logic.cleaning and soc.view.models.user_self modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 788
diff changeset
    55
      raise forms.ValidationError("This user does not exist.")
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    56
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    57
    return user_entity
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    58
  return wrapped
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    59
905
70eb2493a346 Rename clean_existing_user_not_equal_to_current function to clean_users_not_same in soc.logic.cleaning module and fix affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 902
diff changeset
    60
def clean_users_not_same(field_name):
886
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    61
  """Check if the field_name field is a valid user and is not 
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    62
     equal to the current user.
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    63
  """
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    64
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    65
  def wrapped(self):
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    66
    
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    67
    clean_user_field = clean_existing_user(field_name)
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    68
    user_entity = clean_user_field(self)
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    69
    
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    70
    current_user_entity = user_logic.logic.getForCurrentAccount()
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    71
    
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    72
    if user_entity.key() == current_user_entity.key():
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    73
      # users are equal
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    74
      raise forms.ValidationError("You cannot enter yourself here")
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    75
    
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    76
    return user_entity
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    77
  return wrapped
f5847f24c56e Added clean_existing_user_not_equal_to_current to clean the backup Administrator field in an application.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 867
diff changeset
    78
788
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
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    91
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    92
def clean_url(field_name):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    93
  """Clean method for cleaning a field belonging to a LinkProperty.
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    94
  """
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    95
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    96
  def wrapped(self):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    97
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    98
    value = self.cleaned_data.get(field_name)
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    99
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   100
    # LinkProperty does not accept the empty string so we must return None
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   101
    if not value or value == u'':
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   102
      return None
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   103
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   104
    # call the Django URLField cleaning method to properly clean/validate this field
902
0cc7a76f0eb3 Fixed one style error and one typo.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 898
diff changeset
   105
    return forms.URLField.clean(self, value)
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   106
  return wrapped
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   107