app/soc/logic/cleaning.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 23 Jan 2009 09:08:26 +0000
changeset 913 db38e7680d1c
parent 905 70eb2493a346
child 928 df051fc9d7a1
permissions -rw-r--r--
Added state property to role model. This can be used when for instance a member has been removed from a club or a when a program has been marked inactive. Certain roles would then be shown on the upcoming roles page marked as previous roles. This would give us the archiving capability that was shown in the mockup. Patch by: Lennard de Rijk Reviewd 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>',
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_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
    33
  # 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
    34
  link_id = self.cleaned_data.get('link_id').lower()
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
  if not validate.isLinkIdFormatValid(link_id):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    raise forms.ValidationError("This link ID is in wrong format.")
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  return link_id
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
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    40
def clean_existing_user(field_name):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    41
  """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
    42
  """
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
  def wrapped(self):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    45
    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
    46
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    47
    if not validate.isLinkIdFormatValid(link_id):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    48
      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
    49
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    50
    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
    51
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    52
    if not user_entity:
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    53
      # 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
    54
      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
    55
  
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    56
    return user_entity
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    57
  return wrapped
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    58
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
    59
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
    60
  """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
    61
     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
    62
  """
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
  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
    65
    
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
    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
    67
    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
    68
    
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
    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
    70
    
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
    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
    72
      # 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
    73
      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
    74
    
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
    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
    76
  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
    77
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
    78
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
def clean_feed_url(self):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
  feed_url = self.cleaned_data.get('feed_url')
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
  if feed_url == '':
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
    # 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
    84
    return None
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
  
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
  if not validate.isFeedURLValid(feed_url):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    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
    88
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
  return feed_url
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    90
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    91
def clean_url(field_name):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    92
  """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
    93
  """
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
  def wrapped(self):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    96
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    97
    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
    98
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
    99
    # 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
   100
    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
   101
      return None
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   102
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   103
    # 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
   104
    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
   105
  return wrapped
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   106