app/soc/logic/cleaning.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 18 Sep 2009 21:50:54 +0200
changeset 2961 34c6737e77a0
parent 2929 04851beb824e
permissions -rw-r--r--
Convert cleaning module to use the new checker in core
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
#
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.
601
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
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
    17
"""Generic cleaning methods.
601
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__ = [
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
    21
    '"Madhusudan.C.S" <madhusudancs@gmail.com>',
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
    '"Todd Larsen" <tlarsen@google.com>',
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
    '"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
    24
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
2329
4e487ffd4102 Add comment to clean_html_content function and update __authors__.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2327
diff changeset
    25
    '"Pawel Solyga" <pawel.solyga@gmail.com>',
601
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
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
2327
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
    29
from htmlsanitizer import HtmlSanitizer
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
    30
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
    31
from google.appengine.api import users
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
    32
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
from django import forms
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
    34
from django.forms.util import ErrorList
1083
b8018d7a9f23 Moved clean_club_app_link_id to cleaning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1079
diff changeset
    35
from django.utils.translation import ugettext
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from soc.logic import validate
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    38
from soc.logic.models import document as document_logic
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
    39
from soc.logic.models.site import logic as site_logic
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
    40
from soc.logic.models.user import logic as user_logic
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    41
from soc.models import document as document_model
2961
34c6737e77a0 Convert cleaning module to use the new checker in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2929
diff changeset
    42
from soc.modules import callback
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
    45
DEF_LINK_ID_IN_USE_MSG = ugettext(
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
    46
    'This link ID is already in use, please specify another one')
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
    47
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    48
DEF_NO_RIGHTS_FOR_ACL_MSG = ugettext(
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
    49
    'You do not have the required rights for that ACL.')
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
    50
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
    51
DEF_ORGANZIATION_NOT_ACTIVE_MSG = ugettext(
1472
27c9ae3f8d8b Textual change in cleaing.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1446
diff changeset
    52
    "This organization is not active or doesn't exist.")
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    53
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    54
DEF_NO_SUCH_DOCUMENT_MSG = ugettext(
1768
3177f83c1e50 Typo fix in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1755
diff changeset
    55
    "There is no such document with that link ID under this entity.")
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    56
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
    57
DEF_MUST_BE_ABOVE_AGE_LIMIT_FMT = ugettext(
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
    58
    "To sign up as a student for this program, you "
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
    59
    "must be at least %d years of age, as of %s.")
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
    60
2029
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    61
DEF_MUST_BE_ABOVE_LIMIT_FMT = ugettext(
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    62
    "Must be at least %d characters, it has %d characters.")
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    63
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    64
DEF_MUST_BE_UNDER_LIMIT_FMT = ugettext(
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    65
    "Must be under %d characters, it has %d characters.")
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
    66
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    67
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    68
def check_field_is_empty(field_name):
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    69
  """Returns decorator that bypasses cleaning for empty fields.
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    70
  """
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    71
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    72
  def decorator(fun):
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    73
    """Decorator that checks if a field is empty if so doesn't do the cleaning.
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    74
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    75
    Note Django will capture errors concerning required fields that are empty.
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    76
    """
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    77
    from functools import wraps
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    78
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    79
    @wraps(fun)
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    80
    def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
    81
      """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
    82
      """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    83
      field_content = self.cleaned_data.get(field_name)
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    84
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    85
      if not field_content:
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    86
        # field has no content so bail out
1288
4ef7de6c2b7a Returning None instead of the empty to fix a bug on melange-demo instance.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1287
diff changeset
    87
        return None
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    88
      else:
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    89
        # field has contents
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    90
        return fun(self)
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    91
    return wrapper
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    92
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    93
  return decorator
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    94
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    95
1395
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    96
def clean_empty_field(field_name):
1935
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
    97
  """Incorporates the check_field_is_empty as regular cleaner.
1395
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    98
  """
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    99
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   100
  @check_field_is_empty(field_name)
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   101
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   102
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   103
    """
1395
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   104
    return self.cleaned_data.get(field_name)
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   105
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   106
  return wrapper
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   107
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
   108
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   109
def clean_link_id(field_name):
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   110
  """Checks if the field_name value is in a valid link ID format.
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   111
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   112
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   113
  @check_field_is_empty(field_name)
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   114
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   115
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   116
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   117
    # convert to lowercase for user comfort
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   118
    link_id = self.cleaned_data.get(field_name).lower()
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   119
    if not validate.isLinkIdFormatValid(link_id):
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   120
      raise forms.ValidationError("This link ID is in wrong format.")
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   121
    return link_id
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   122
  return wrapper
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   125
def clean_scope_path(field_name):
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   126
  """Checks if the field_name value is in a valid scope path format.
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   127
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   128
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   129
  @check_field_is_empty(field_name)
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   130
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   131
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   132
    """
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   133
    # convert to lowercase for user comfort
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   134
    scope_path = self.cleaned_data.get(field_name).lower()
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   135
    if not validate.isScopePathFormatValid(scope_path):
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   136
      raise forms.ValidationError("This scope path is in wrong format.")
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   137
    return scope_path
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   138
  return wrapper
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   139
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   140
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   141
def clean_agrees_to_tos(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   142
  """Checks if there is a ToS to see if it is allowed to leave
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   143
     the field_name field false.
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   144
  """
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   145
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   146
  @check_field_is_empty(field_name)
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   147
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   148
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   149
    """
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   150
    agrees_to_tos = self.cleaned_data.get(field_name)
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   151
1773
ad930f979ed9 Fix typo in soc.logic.cleaning module (site_logic.logic was called instead of just site_logic).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1768
diff changeset
   152
    if not site_logic.getToS(site_logic.getSingleton()):
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   153
      return agrees_to_tos
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   154
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   155
    # Site settings specify a site-wide ToS, so agreement is *required*
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   156
    if agrees_to_tos:
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   157
      return True
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   158
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   159
    # there was no agreement made so raise an error
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   160
    raise forms.ValidationError(
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   161
        'The site-wide Terms of Service must be accepted to participate'
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   162
        ' on this site.')
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   163
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   164
  return wrapper
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   165
1143
b07b7d5b3e27 Fix missing dots and blank lines to soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1088
diff changeset
   166
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   167
def clean_existing_user(field_name):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   168
  """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
   169
  """
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   170
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   171
  @check_field_is_empty(field_name)
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   172
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   173
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   174
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   175
    link_id = clean_link_id(field_name)(self)
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   176
1984
eb02fd5427c5 Use getFromKeyFields instead of getForFields where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1962
diff changeset
   177
    user_entity = user_logic.getFromKeyFields({'link_id': link_id})
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   178
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   179
    if not user_entity:
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   180
      # 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
   181
      raise forms.ValidationError("This user does not exist.")
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   182
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   183
    return user_entity
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   184
  return wrapped
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   185
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   186
1985
4ff03ca639fd Optionally return link_id in clean_user_is_current
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1984
diff changeset
   187
def clean_user_is_current(field_name, as_user=True):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   188
  """Check if the field_name value is a valid link_id and resembles the
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   189
     current user.
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   190
  """
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   191
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   192
  @check_field_is_empty(field_name)
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   193
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   194
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   195
    """
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   196
    link_id = clean_link_id(field_name)(self)
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   197
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   198
    user_entity = user_logic.getForCurrentAccount()
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2329
diff changeset
   199
    # pylint: disable-msg=E1103
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   200
    if not user_entity or user_entity.link_id != link_id:
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   201
      # this user is not the current user
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   202
      raise forms.ValidationError("This user is not you.")
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   203
1985
4ff03ca639fd Optionally return link_id in clean_user_is_current
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1984
diff changeset
   204
    return user_entity if as_user else link_id
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   205
  return wrapped
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   206
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   207
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   208
def clean_user_not_exist(field_name):
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   209
  """Check if the field_name value is a valid link_id and a user with the
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   210
     link id does not exist.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   211
  """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   212
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   213
  @check_field_is_empty(field_name)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   214
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   215
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   216
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   217
    link_id = clean_link_id(field_name)(self)
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   218
1984
eb02fd5427c5 Use getFromKeyFields instead of getForFields where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1962
diff changeset
   219
    user_entity = user_logic.getFromKeyFields({'link_id': link_id})
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   220
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   221
    if user_entity:
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   222
      # user exists already
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   223
      raise forms.ValidationError("There is already a user with this link id.")
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   224
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   225
    return link_id
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   226
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   227
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   228
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
   229
def clean_users_not_same(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   230
  """Check if the field_name field is a valid user and is not
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
   231
     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
   232
  """
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
   233
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   234
  @check_field_is_empty(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
   235
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   236
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   237
    """
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
   238
    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
   239
    user_entity = clean_user_field(self)
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   240
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   241
    current_user_entity = user_logic.getForCurrentAccount()
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2329
diff changeset
   242
    # pylint: disable-msg=E1103
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
   243
    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
   244
      # users are equal
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   245
      raise forms.ValidationError("You cannot enter yourself here.")
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   246
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
   247
    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
   248
  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
   249
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   250
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   251
def clean_user_account(field_name):
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   252
  """Returns the User with the given field_name value.
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   253
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   254
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   255
  @check_field_is_empty(field_name)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   256
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   257
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   258
    """
1522
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1472
diff changeset
   259
    email_adress = self.cleaned_data[field_name]
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1472
diff changeset
   260
    return users.User(email_adress)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   261
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   262
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   263
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   264
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   265
def clean_user_account_not_in_use(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   266
  """Check if the field_name value contains an email
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   267
     address that hasn't been used for an existing account.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   268
  """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   269
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   270
  @check_field_is_empty(field_name)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   271
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   272
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   273
    """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   274
    email_adress = self.cleaned_data.get(field_name).lower()
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   275
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   276
    # get the user account for this email and check if it's in use
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   277
    user_account = users.User(email_adress)
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   278
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   279
    fields = {'account': user_account}
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   280
    user_entity = user_logic.getForFields(fields, unique=True)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   281
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   282
    if user_entity or user_logic.isFormerAccount(user_account):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   283
      raise forms.ValidationError("There is already a user "
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   284
          "with this email adress.")
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   285
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   286
    return user_account
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   287
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   288
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   289
1722
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   290
def clean_ascii_only(field_name):
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   291
  """Clean method for cleaning a field that may only contain ASCII-characters.
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   292
  """
1962
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   293
1722
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   294
  @check_field_is_empty(field_name)
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   295
  def wrapper(self):
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   296
    """Decorator wrapper method.
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   297
    """
1962
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   298
1722
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   299
    value = self.cleaned_data.get(field_name)
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   300
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   301
    try:
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   302
      # encode to ASCII
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   303
      value = value.encode("ascii")
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   304
    except UnicodeEncodeError:
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   305
      # can not encode as ASCII
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   306
      raise forms.ValidationError("Only ASCII characters are allowed")
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   307
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   308
    return value
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   309
  return wrapper
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   310
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   311
2029
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   312
def clean_content_length(field_name, min_length=0, max_length=500):
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   313
  """Clean method for cleaning a field which must contain at least min and
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   314
     not more then max length characters.
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   315
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   316
  Args:
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   317
    field_name: the name of the field needed cleaning
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   318
    min_length: the minimum amount of allowed characters
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   319
    max_length: the maximum amount of allowed characters
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   320
  """
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   321
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   322
  @check_field_is_empty(field_name)
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   323
  def wrapper(self):
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   324
    """Decorator wrapper method.
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   325
    """
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   326
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   327
    value = self.cleaned_data[field_name]
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   328
    value_length = len(value)
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   329
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   330
    if value_length < min_length:
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   331
      raise forms.ValidationError(DEF_MUST_BE_ABOVE_LIMIT_FMT %(
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   332
          min_length, value_length))
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   333
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   334
    if value_length > max_length:
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   335
      raise forms.ValidationError(DEF_MUST_BE_UNDER_LIMIT_FMT %(
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   336
          max_length, value_length))
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   337
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   338
    return value
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   339
  return wrapper
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   340
1f276091b42a Added clean_content_length method to clean content of a specific size.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2014
diff changeset
   341
1990
fea8bdb43992 Automagically convert '+' to '00' in phone numbers
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1988
diff changeset
   342
def clean_phone_number(field_name):
1962
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   343
  """Clean method for cleaning a field that may only contain numerical values.
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   344
  """
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   345
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   346
  @check_field_is_empty(field_name)
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   347
  def wrapper(self):
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   348
    """Decorator wrapped method.
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   349
    """
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   350
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   351
    value = self.cleaned_data.get(field_name)
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   352
1990
fea8bdb43992 Automagically convert '+' to '00' in phone numbers
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1988
diff changeset
   353
    # allow for a '+' prefix which means '00'
fea8bdb43992 Automagically convert '+' to '00' in phone numbers
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1988
diff changeset
   354
    if value[0] == '+':
fea8bdb43992 Automagically convert '+' to '00' in phone numbers
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1988
diff changeset
   355
      value = '00' + value[1:]
fea8bdb43992 Automagically convert '+' to '00' in phone numbers
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1988
diff changeset
   356
1962
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   357
    if not value.isdigit():
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   358
      raise forms.ValidationError("Only numerical characters are allowed")
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   359
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   360
    return value
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   361
  return wrapper
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   362
7c9e517f2089 Added a numeric_only cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1935
diff changeset
   363
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   364
def clean_feed_url(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   365
  """Clean method for cleaning feed url.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   366
  """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   367
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   368
  feed_url = self.cleaned_data.get('feed_url')
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   369
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   370
  if feed_url == '':
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   371
    # 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
   372
    return None
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   373
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   374
  if not validate.isFeedURLValid(feed_url):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   375
    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
   376
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   377
  return feed_url
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   378
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   379
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   380
def clean_html_content(field_name):
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   381
  """Clean method for cleaning HTML content.
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   382
  """
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   383
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   384
  @check_field_is_empty(field_name)
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   385
  def wrapped(self):
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   386
    """Decorator wrapper method.
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   387
    """
2327
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   388
    from HTMLParser import HTMLParseError
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   389
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   390
    content = self.cleaned_data.get(field_name)
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
   391
2329
4e487ffd4102 Add comment to clean_html_content function and update __authors__.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2327
diff changeset
   392
    # clean_html_content is called when writing data into GAE rather than 
4e487ffd4102 Add comment to clean_html_content function and update __authors__.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2327
diff changeset
   393
    # when reading data from GAE. This short-circuiting of the sanitizer 
4e487ffd4102 Add comment to clean_html_content function and update __authors__.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2327
diff changeset
   394
    # only affects html authored by developers. The isDeveloper test for 
4e487ffd4102 Add comment to clean_html_content function and update __authors__.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2327
diff changeset
   395
    # example allows developers to add javascript.
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   396
    if user_logic.isDeveloper():
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   397
      return content
2327
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   398
    
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   399
    try:
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   400
      cleaner = HtmlSanitizer.Cleaner()
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   401
      cleaner.string = content
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   402
      cleaner.clean()
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   403
    except HTMLParseError, msg:
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   404
      raise forms.ValidationError(msg)
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   405
    
6c7d0fba105c Replace usage of feedparser for sanitizing html with new HtmlSanitizer module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2115
diff changeset
   406
    content = cleaner.string
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   407
    content = content.strip().replace('\r\n', '\n')
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   408
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   409
    return content
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   410
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   411
  return wrapped
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
   412
1722
7f285e96cb17 All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1699
diff changeset
   413
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   414
def clean_url(field_name):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   415
  """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
   416
  """
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   417
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   418
  @check_field_is_empty(field_name)
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   419
  def wrapped(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   420
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   421
    """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   422
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   423
    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
   424
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   425
    # call the Django URLField cleaning method to
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   426
    # properly clean/validate this field
967
be5f5533a07f Changed clean_url to work with dynaform.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 928
diff changeset
   427
    return forms.URLField.clean(self.fields[field_name], value)
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   428
  return wrapped
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   429
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   430
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   431
def clean_refs(params, fields):
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   432
  """Cleans all references to make sure they are valid.
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   433
  """
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   434
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   435
  logic = params['logic']
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   436
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   437
  def wrapped(self):
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   438
    """Decorator wrapper method.
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   439
    """
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   440
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   441
    scope_path = logic.getKeyNameFromFields(self.cleaned_data)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   442
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   443
    key_fields = {
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   444
        'scope_path': scope_path,
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   445
        'prefix': params['document_prefix'],
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   446
        }
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   447
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   448
    for field in fields:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   449
      link_id = self.cleaned_data.get(field)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   450
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   451
      if not link_id:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   452
        continue
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   453
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   454
      key_fields['link_id'] = link_id
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   455
      ref = document_logic.logic.getFromKeyFields(key_fields)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   456
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   457
      if not ref:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   458
        self._errors[field] = ErrorList([DEF_NO_SUCH_DOCUMENT_MSG])
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   459
        del self.cleaned_data[field]
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   460
      else:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   461
        self.cleaned_data['resolved_%s' % field] = ref
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   462
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   463
    return self.cleaned_data
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   464
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   465
  return wrapped
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   466
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   467
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   468
def validate_user_edit(link_id_field, account_field):
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   469
  """Clean method for cleaning user edit form.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   470
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   471
  Raises ValidationError if:
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   472
    -Another User has the given email address as account
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   473
    -Another User has the given email address in it's FormerAccounts list
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   474
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   475
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   476
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   477
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   478
    """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   479
    cleaned_data = self.cleaned_data
1242
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1217
diff changeset
   480
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   481
    link_id = cleaned_data.get(link_id_field)
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   482
    user_account = cleaned_data.get(account_field)
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   483
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   484
    # if both fields were valid do this check
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   485
    if link_id and user_account:
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   486
      # get the user from the link_id in the form
1984
eb02fd5427c5 Use getFromKeyFields instead of getForFields where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1962
diff changeset
   487
eb02fd5427c5 Use getFromKeyFields instead of getForFields where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1962
diff changeset
   488
      user_entity = user_logic.getFromKeyFields({'link_id': link_id})
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   489
1285
d54f2d80f46e Fixed comment and typo in cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1242
diff changeset
   490
      # if it's not the user's current account
1242
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1217
diff changeset
   491
      if user_entity.account != user_account:
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   492
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   493
        # get the user having the given account
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   494
        fields = {'account': user_account}
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   495
        user_from_account_entity = user_logic.getForFields(fields,
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   496
            unique=True)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   497
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   498
        # if there is a user with the given account or it's a former account
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   499
        if user_from_account_entity or \
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   500
            user_logic.isFormerAccount(user_account):
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   501
          # raise an error because this email address can't be used
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   502
          raise forms.ValidationError("There is already a user with "
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   503
              "this email address.")
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   504
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   505
    return cleaned_data
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   506
  return wrapper
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   507
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   508
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   509
def validate_new_group(link_id_field, scope_path_field,
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   510
                       group_logic, group_app_logic):
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   511
  """Clean method used to clean the group application or new group form.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   512
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   513
    Raises ValidationError if:
1217
aeabe6bed55b Reverted r1819 and making an application will now reserve the link_id indefinitely.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1205
diff changeset
   514
    -A application with this link id and scope path already exists
aeabe6bed55b Reverted r1819 and making an application will now reserve the link_id indefinitely.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1205
diff changeset
   515
    -A group with this link id and scope path already exists
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   516
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   517
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   518
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   519
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   520
    """
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   521
    cleaned_data = self.cleaned_data
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   522
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   523
    fields = {}
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   524
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   525
    link_id = cleaned_data.get(link_id_field)
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   526
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   527
    if link_id:
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   528
      fields['link_id'] = link_id
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   529
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   530
      scope_path = cleaned_data.get(scope_path_field)
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   531
      if scope_path:
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   532
        fields['scope_path'] = scope_path
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   533
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   534
      # get the application
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   535
      group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   536
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   537
      # get the current user
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   538
      user_entity = user_logic.getForCurrentAccount()
1217
aeabe6bed55b Reverted r1819 and making an application will now reserve the link_id indefinitely.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1205
diff changeset
   539
1755
972c546f0810 Fixed a cleaning bug which allowed a proposal to be recreated by the original proposer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1722
diff changeset
   540
      # if the proposal has not been accepted or it's not the applicant
972c546f0810 Fixed a cleaning bug which allowed a proposal to be recreated by the original proposer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1722
diff changeset
   541
      # creating the new group then show link ID in use message
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2329
diff changeset
   542
      # pylint: disable-msg=E1103
1755
972c546f0810 Fixed a cleaning bug which allowed a proposal to be recreated by the original proposer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1722
diff changeset
   543
      if group_app_entity and (group_app_entity.status != 'accepted' or (
972c546f0810 Fixed a cleaning bug which allowed a proposal to be recreated by the original proposer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1722
diff changeset
   544
          group_app_entity.applicant.key() != user_entity.key())):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   545
        # add the error message to the link id field
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   546
        self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   547
        del cleaned_data[link_id_field]
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   548
        # return the new cleaned_data
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   549
        return cleaned_data
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   550
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   551
      # check if there is already a group for the given fields
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   552
      group_entity = group_logic.logic.getForFields(fields, unique=True)
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   553
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   554
      if group_entity:
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   555
        # add the error message to the link id field
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   556
        self._errors[link_id_field] = ErrorList([DEF_LINK_ID_IN_USE_MSG])
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   557
        del cleaned_data[link_id_field]
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   558
        # return the new cleaned_data
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   559
        return cleaned_data
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   560
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   561
    return cleaned_data
1205
2e88261aba72 Added validate_new_group to cleaning and removed clean_new_club_link_id.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1143
diff changeset
   562
  return wrapper
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   563
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   564
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   565
def validate_student_proposal(org_field, scope_field,
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   566
                              student_logic, org_logic):
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   567
  """Validates the form of a student proposal.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   568
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   569
  Raises ValidationError if:
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   570
    -The organization link_id does not match an active organization
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   571
    -The hidden scope path is not a valid active student
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   572
  """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   573
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   574
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   575
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   576
    """
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   577
    cleaned_data = self.cleaned_data
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   578
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   579
    org_link_id = cleaned_data.get(org_field)
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   580
    scope_path = cleaned_data.get(scope_field)
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   581
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   582
    # only if both fields are valid
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   583
    if org_link_id and scope_path:
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   584
      filter = {'scope_path': scope_path,
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   585
                'status': 'active'}
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   586
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   587
      student_entity = student_logic.logic.getFromKeyName(scope_path)
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   588
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   589
      if not student_entity or student_entity.status != 'active':
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   590
        # raise validation error, access checks should have prevented this
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   591
        raise forms.ValidationError(
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   592
            ugettext("The given student is not valid."))
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   593
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   594
      filter = {'link_id': org_link_id,
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   595
                'scope': student_entity.scope,
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   596
                'status': 'active'}
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   597
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   598
      org_entity = org_logic.logic.getForFields(filter, unique=True)
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   599
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   600
      if not org_entity:
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   601
        #raise validation error, non valid organization entered
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   602
        self._errors['organization'] = ErrorList(
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   603
            [DEF_ORGANZIATION_NOT_ACTIVE_MSG])
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   604
        del cleaned_data['organization']
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   605
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   606
    return cleaned_data
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   607
  return wrapper
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   608
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   609
2115
fafd021def7e Major update to the views concerning StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2073
diff changeset
   610
def validate_student_project(org_field, mentor_field, student_field):
1935
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   611
  """Validates the form of a student proposal.
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   612
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   613
  Args:
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   614
    org_field: Field containing key_name for org
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   615
    mentor_field: Field containing the link_id of the mentor
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   616
    student_field: Field containing the student link_id
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   617
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   618
  Raises ValidationError if:
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   619
    -A valid Organization does not exist for the given keyname
2115
fafd021def7e Major update to the views concerning StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2073
diff changeset
   620
    -The mentor link_id does not match a mentor for the active organization
1935
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   621
    -The student link_id does not match a student in the org's Program
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   622
  """
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   623
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   624
  def wrapper(self):
2073
6eb9b4652c80 Style fixes and removal of unused imports in soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2029
diff changeset
   625
    """Decorator wrapper method.
6eb9b4652c80 Style fixes and removal of unused imports in soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2029
diff changeset
   626
    """
1935
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   627
    from soc.logic.models.mentor import logic as mentor_logic
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   628
    from soc.logic.models.organization import logic as org_logic
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   629
    from soc.logic.models.student import logic as student_logic
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   630
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   631
    cleaned_data = self.cleaned_data
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   632
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   633
    org_key_name = cleaned_data.get(org_field)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   634
    mentor_link_id = cleaned_data.get(mentor_field)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   635
    student_link_id = cleaned_data.get(student_field)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   636
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   637
    if not (org_key_name and mentor_link_id and student_link_id):
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   638
      # we can't do the check the other cleaners will pickup empty fields
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   639
      return cleaned_data
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   640
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   641
    org_entity = org_logic.getFromKeyName(org_key_name)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   642
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   643
    if not org_entity:
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   644
      # show error message
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   645
      raise forms.ValidationError(
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   646
          ugettext("The given Organization is not valid."))
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   647
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   648
    fields = {'link_id': mentor_link_id,
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   649
              'scope': org_entity,
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   650
              'status': 'active'}
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   651
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   652
    mentor_entity = mentor_logic.getForFields(fields, unique=True,)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   653
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   654
    if not mentor_entity:
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   655
      # show error message
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   656
      raise forms.ValidationError(
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   657
          ugettext("The given Mentor is not valid."))
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   658
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   659
    fields = {'link_id': student_link_id,
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   660
              'scope': org_entity.scope,
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   661
              'status': 'active'}
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   662
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   663
    student_entity = student_logic.getForFields(fields, unique=True)
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   664
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   665
    if not student_entity:
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   666
      #show error message
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   667
      raise forms.ValidationError(
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   668
          ugettext("The given Student is not valid."))
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   669
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   670
    # successfully validated
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   671
    return cleaned_data
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   672
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   673
  return wrapper
99ec4bbd70f5 Added new Student Project cleaning method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1773
diff changeset
   674
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   675
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   676
def validate_student(birth_date_field, school_type_field, major_field,
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   677
                     degree_field, grade_field, scope_field, program_logic):
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   678
  """Checks if the student is eligible to sign up, determined 
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   679
  by his birth_date given in field_name.
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   680
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   681
  Also checks if the school information has been correctly filled in.
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   682
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   683
  Args:
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   684
    birth_date_field: Field containing birth_date of student
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   685
    school_type_field: The field containing the type of student
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   686
    major_field: The field containing the major of a University student
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   687
    degree_field: The field containing the type of degree for a University
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   688
                  student
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   689
    grade_field: The field containing the grade of a High School student
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   690
    scope_field: Field containing scope_path of the student entity
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   691
    program_logic: Logic instance of the program
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   692
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   693
  Raises ValidationError if:
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   694
    -The student's age is less than the minimum age required by the program
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   695
  """
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   696
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   697
  def wrapper(self):
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   698
    """Wrapper method.
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   699
    """
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   700
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   701
    cleaned_data = self.cleaned_data
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   702
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   703
    birth_date = cleaned_data.get(birth_date_field)
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   704
    program_key_name = cleaned_data.get(scope_field)
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   705
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   706
    if not birth_date or not program_key_name:
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   707
      # nothing to check, field validator will find these errors
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   708
      return cleaned_data
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   709
2929
04851beb824e Moved the Student cleaner to the create form such that it is always used.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2927
diff changeset
   710
    # get the current program entity
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   711
    entity = program_logic.getFromKeyName(program_key_name)
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   712
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   713
    if not entity:
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   714
      raise forms.ValidationError(
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   715
          ugettext("No valid program found"))
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   716
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   717
    school_type = cleaned_data.get(school_type_field)
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   718
    major = cleaned_data.get(major_field)
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   719
    degree = cleaned_data.get(degree_field)
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   720
2929
04851beb824e Moved the Student cleaner to the create form such that it is always used.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2927
diff changeset
   721
    # TODO: when school_type is required this can be removed
04851beb824e Moved the Student cleaner to the create form such that it is always used.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2927
diff changeset
   722
    if not school_type:
04851beb824e Moved the Student cleaner to the create form such that it is always used.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2927
diff changeset
   723
      raise forms.ValidationError("School type cannot be left blank.")
04851beb824e Moved the Student cleaner to the create form such that it is always used.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2927
diff changeset
   724
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   725
    # if school_type is University, check for major
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   726
    if school_type == 'University' and not major:
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   727
      raise forms.ValidationError("Major cannot be left blank.")
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   728
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   729
    # if school_type is University, check for degree
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   730
    if school_type == 'University' and not degree: 
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   731
      raise forms.ValidationError("Degree must be selected from "
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   732
                                  "the given options.")
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   733
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   734
    grade = cleaned_data.get(grade_field)
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   735
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   736
    # if school_type is High School check for grade
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   737
    if school_type == 'High School' and not grade: 
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   738
      raise forms.ValidationError("Grade cannot be left blank.")
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   739
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   740
    if entity.student_min_age and entity.student_min_age_as_of:
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   741
      # only check if both the min_age and min_age_as_of are defined
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   742
      min_year = entity.student_min_age_as_of.year - entity.student_min_age
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   743
      min_date = entity.student_min_age_as_of.replace(year=min_year)
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   744
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   745
      if birth_date > min_date:
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   746
        # this Student is not old enough
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   747
        self._errors[birth_date_field] = ErrorList(
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   748
            [DEF_MUST_BE_ABOVE_AGE_LIMIT_FMT %(
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   749
            entity.student_min_age,
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   750
            entity.student_min_age_as_of.strftime('%A, %B %d, %Y'))])
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   751
        del cleaned_data[birth_date_field]
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   752
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   753
    return cleaned_data
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   754
  return wrapper
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   755
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   756
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   757
def validate_document_acl(view, creating=False):
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   758
  """Validates that the document ACL settings are correct.
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   759
  """
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   760
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   761
  def wrapper(self):
1621
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   762
    """Decorator wrapper method.
106a5a7834d0 Add missing docstrings, fix indentions and too long lines in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1522
diff changeset
   763
    """
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   764
    cleaned_data = self.cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   765
    read_access = cleaned_data.get('read_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   766
    write_access = cleaned_data.get('write_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   767
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   768
    if not (read_access and write_access and ('prefix' in cleaned_data)):
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   769
      return cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   770
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   771
    if read_access != 'public':
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   772
      ordening = document_model.Document.DOCUMENT_ACCESS
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   773
      if ordening.index(read_access) < ordening.index(write_access):
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   774
        raise forms.ValidationError(
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   775
            "Read access should be less strict than write access.")
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   776
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   777
    params = view.getParams()
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   778
    rights = params['rights']
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   779
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   780
    user = user_logic.getForCurrentAccount()
2927
ac4f93519855 Changed the Student Model cleaner to deal with the new Student properties.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2859
diff changeset
   781
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2329
diff changeset
   782
    # pylint: disable-msg=E1103
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   783
    rights.setCurrentUser(user.account, user)
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   784
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   785
    prefix = self.cleaned_data['prefix']
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   786
    scope_path = self.cleaned_data['scope_path']
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   787
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   788
    validate_access(self, view, rights, prefix, scope_path, 'read_access')
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   789
    validate_access(self, view, rights, prefix, scope_path, 'write_access')
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   790
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   791
    if creating and not has_access(rights, 'restricted', scope_path, prefix):
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   792
      raise forms.ValidationError(
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   793
          "You do not have the required access to create this document.")
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   794
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   795
    return cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   796
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   797
  return wrapper
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   798
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   799
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   800
def has_access(rights, access_level, scope_path, prefix):
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   801
  """Checks whether the current user has the required access.
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   802
  """
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   803
2961
34c6737e77a0 Convert cleaning module to use the new checker in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2929
diff changeset
   804
  checker = callback.getCore().getRightsChecker(prefix)
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   805
  roles = checker.getMembership(access_level)
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   806
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   807
  django_args = {
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   808
      'scope_path': scope_path,
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   809
      'prefix': prefix,
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   810
      }
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   811
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   812
  return rights.hasMembership(roles, django_args)
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   813
2716
9921ac952f13 Added minimum age property to Program model.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2365
diff changeset
   814
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   815
def validate_access(self, view, rights, prefix, scope_path, field):
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   816
  """Validates that the user has access to the ACL for the specified fields.
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   817
  """
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   818
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   819
  access_level = self.cleaned_data[field]
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   820
1988
d826f7aed8f2 Make sure the user has restricted rights to create a new document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1985
diff changeset
   821
  if not has_access(rights, access_level, scope_path, prefix):
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   822
    self._errors[field] = ErrorList([DEF_NO_RIGHTS_FOR_ACL_MSG])
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   823
    del self.cleaned_data[field]
2859
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   824
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   825
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   826
def str2set(string_field):
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   827
  """Clean method for cleaning comma separated strings.
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   828
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   829
  Obtains the comma separated string from the form and returns it as
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   830
  a set of strings.
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   831
  """
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   832
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   833
  def wrapper(self):
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   834
    """Decorator wrapper method.
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   835
    """
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   836
    cleaned_data = self.cleaned_data
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   837
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   838
    string_data = cleaned_data.get(string_field)
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   839
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   840
    list_data = []
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   841
    for string in string_data.split(','):
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   842
      string_strip = string.strip()
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   843
      if string_strip and string_strip not in list_data:
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   844
        list_data.append(string_strip)
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   845
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   846
    return list_data
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   847
391766c2aacf Added cleaning method to convert a list of comma separated strings into a set.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2787
diff changeset
   848
  return wrapper