app/soc/logic/cleaning.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 12 Mar 2009 22:30:04 +0000
changeset 1815 7a9b69f36111
parent 1773 ad930f979ed9
child 1935 99ec4bbd70f5
permissions -rw-r--r--
Always convert newlines to linebreaks Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
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__ = [
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Todd Larsen" <tlarsen@google.com>',
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
928
df051fc9d7a1 Replaced the boolean properties in soc/models/request.py with a state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 905
diff changeset
    23
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
    ]
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
    27
import feedparser
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
    28
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
    29
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
    30
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
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
    32
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
    33
from django.utils.translation import ugettext
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
1354
aba2beea6dfa Fix import sorting in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1333
diff changeset
    35
from soc.logic import rights as rights_logic
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
from soc.logic import validate
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    37
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
    38
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
    39
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
    40
from soc.models import document as document_model
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
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
    43
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
    44
    '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
    45
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    46
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
    47
    '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
    48
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
    49
DEF_ORGANZIATION_NOT_ACTIVE_MSG = ugettext(
1472
27c9ae3f8d8b Textual change in cleaing.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1446
diff changeset
    50
    "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
    51
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    52
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
    53
    "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
    54
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
    55
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    56
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
    57
  """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
    58
  """
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    59
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    60
  def decorator(fun):
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    61
    """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
    62
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    63
    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
    64
    """
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    65
    from functools import wraps
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    66
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    67
    @wraps(fun)
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    68
    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
    69
      """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
    70
      """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    71
      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
    72
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    73
      if not field_content:
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    74
        # 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
    75
        return None
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    76
      else:
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    77
        # field has contents
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    78
        return fun(self)
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    79
    return wrapper
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    80
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    81
  return decorator
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    82
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
    83
1395
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    84
def clean_empty_field(field_name):
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    85
  """Incorperates the check_field_is_empty as regular cleaner.
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    86
  """
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    87
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    88
  @check_field_is_empty(field_name)
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    89
  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
    90
    """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
    91
    """
1395
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    92
    return self.cleaned_data.get(field_name)
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    93
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    94
  return wrapper
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    95
08dbb5aa1dc9 Add a regular 'empty field cleaner'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1354
diff changeset
    96
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
    97
def clean_link_id(field_name):
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
    98
  """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
    99
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   100
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   101
  @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
   102
  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
   103
    """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
   104
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   105
    # 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
   106
    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
   107
    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
   108
      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
   109
    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
   110
  return wrapper
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   113
def clean_scope_path(field_name):
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   114
  """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
   115
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   116
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   117
  @check_field_is_empty(field_name)
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   118
  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
   119
    """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
   120
    """
1088
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   121
    # convert to lowercase for user comfort
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   122
    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
   123
    if not validate.isScopePathFormatValid(scope_path):
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   124
      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
   125
    return scope_path
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   126
  return wrapper
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   127
7ad48d59da3d Added a clean_scope_path to cleaning
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
   128
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   129
def clean_agrees_to_tos(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   130
  """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
   131
     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
   132
  """
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   133
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   134
  @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
   135
  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
   136
    """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
   137
    """
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   138
    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
   139
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
   140
    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
   141
      return agrees_to_tos
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   142
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   143
    # 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
   144
    if agrees_to_tos:
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   145
      return True
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   146
1039
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   147
    # 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
   148
    raise forms.ValidationError(
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   149
        '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
   150
        ' on this site.')
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   151
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   152
  return wrapper
d53b963b1454 Added new clean_agrees_to_tos to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1028
diff changeset
   153
1143
b07b7d5b3e27 Fix missing dots and blank lines to soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1088
diff changeset
   154
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   155
def clean_existing_user(field_name):
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   156
  """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
   157
  """
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   158
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   159
  @check_field_is_empty(field_name)
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   160
  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
   161
    """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
   162
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   163
    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
   164
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   165
    user_entity = user_logic.getForFields({'link_id': link_id},
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   166
        unique=True)
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   167
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   168
    if not user_entity:
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   169
      # 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
   170
      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
   171
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   172
    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
   173
  return wrapped
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   174
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   175
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   176
def clean_user_is_current(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   177
  """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
   178
     current user.
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   179
  """
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   180
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   181
  @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
   182
  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
   183
    """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
   184
    """
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   185
    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
   186
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   187
    user_entity = user_logic.getForCurrentAccount()
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   188
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   189
    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
   190
      # 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
   191
      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
   192
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   193
    return user_entity
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   194
  return wrapped
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   195
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   196
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   197
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
   198
  """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
   199
     link id does not exist.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   200
  """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   201
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   202
  @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
   203
  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
   204
    """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
   205
    """
1079
be1aacb33b0f Changed clean_link_id to be in a wrapper form as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1039
diff changeset
   206
    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
   207
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   208
    user_entity = user_logic.getForFields({'link_id': link_id},
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   209
        unique=True)
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   210
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   211
    if user_entity:
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   212
      # user exists already
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   213
      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
   214
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   215
    return link_id
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   216
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   217
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   218
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
   219
def clean_users_not_same(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   220
  """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
   221
     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
   222
  """
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
   223
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   224
  @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
   225
  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
   226
    """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
   227
    """
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
   228
    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
   229
    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
   230
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   231
    current_user_entity = user_logic.getForCurrentAccount()
1333
c0ff6fc3192e Added clean_user_is_current to cleaning and fixed some indentation.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1320
diff changeset
   232
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
   233
    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
   234
      # users are equal
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   235
      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
   236
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
   237
    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
   238
  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
   239
788
892877b7db07 Factored out the clean_existing_user method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 650
diff changeset
   240
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   241
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
   242
  """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
   243
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   244
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   245
  @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
   246
  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
   247
    """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
   248
    """
1522
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1472
diff changeset
   249
    email_adress = self.cleaned_data[field_name]
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1472
diff changeset
   250
    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
   251
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   252
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   253
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   254
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   255
def clean_user_account_not_in_use(field_name):
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   256
  """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
   257
     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
   258
  """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   259
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   260
  @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
   261
  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
   262
    """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
   263
    """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   264
    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
   265
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   266
    # 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
   267
    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
   268
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   269
    fields = {'account': user_account}
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   270
    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
   271
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   272
    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
   273
      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
   274
          "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
   275
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   276
    return user_account
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   277
  return wrapped
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   278
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   279
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
   280
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
   281
  """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
   282
  """
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
   283
  @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
   284
  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
   285
    """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
   286
    """
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
   287
    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
   288
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
   289
    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
   290
      # 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
   291
      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
   292
    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
   293
      # 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
   294
      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
   295
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
    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
   297
  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
   298
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
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   300
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
   301
  """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
   302
  """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   303
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   304
  feed_url = self.cleaned_data.get('feed_url')
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   305
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   306
  if feed_url == '':
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   307
    # 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
   308
    return None
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   309
601
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   310
  if not validate.isFeedURLValid(feed_url):
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   311
    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
   312
988f8a8cd6de Added a cleaning module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   313
  return feed_url
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   314
1028
43fdf6739e8d Style fixes in soc.logic.cleaning module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1013
diff changeset
   315
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   316
def clean_html_content(field_name):
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   317
  """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
   318
  """
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   319
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   320
  @check_field_is_empty(field_name)
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   321
  def wrapped(self):
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   322
    """Decorator wrapper method.
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   323
    """
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   324
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   325
    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
   326
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   327
    if user_logic.isDeveloper():
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   328
      return content
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
   329
1682
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   330
    sanitizer = feedparser._HTMLSanitizer('utf-8')
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   331
    sanitizer.feed(content)
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   332
    content = sanitizer.output()
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   333
    content = content.strip().replace('\r\n', '\n')
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   334
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   335
    return content
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   336
242ca517c340 Make clean_document_content generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1681
diff changeset
   337
  return wrapped
1446
bcbbcb72429d Use feedparser to sanitize HTML content for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1440
diff changeset
   338
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
   339
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   340
def clean_url(field_name):
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   341
  """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
   342
  """
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   343
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   344
  @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
   345
  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
   346
    """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
   347
    """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   348
898
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   349
    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
   350
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   351
    # 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
   352
    # properly clean/validate this field
967
be5f5533a07f Changed clean_url to work with dynaform.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 928
diff changeset
   353
    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
   354
  return wrapped
adf5b7e98dcf Added a cleaning method for a LinkProperty Field.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 886
diff changeset
   355
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   356
1699
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   357
def clean_refs(params, fields):
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   358
  """Cleans all references to make sure they are valid.
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   359
  """
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   360
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   361
  logic = params['logic']
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   362
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   363
  def wrapped(self):
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   364
    """Decorator wrapper method.
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   365
    """
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   366
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   367
    scope_path = logic.getKeyNameFromFields(self.cleaned_data)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   368
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   369
    key_fields = {
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   370
        'scope_path': scope_path,
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   371
        'prefix': params['document_prefix'],
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   372
        }
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   373
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   374
    for field in fields:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   375
      link_id = self.cleaned_data.get(field)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   376
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   377
      if not link_id:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   378
        continue
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   379
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   380
      key_fields['link_id'] = link_id
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   381
      ref = document_logic.logic.getFromKeyFields(key_fields)
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   382
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   383
      if not ref:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   384
        self._errors[field] = ErrorList([DEF_NO_SUCH_DOCUMENT_MSG])
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   385
        del self.cleaned_data[field]
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   386
      else:
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   387
        self.cleaned_data['resolved_%s' % field] = ref
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   388
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   389
    return self.cleaned_data
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   390
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   391
  return wrapped
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   392
699f65fbf08a Add a document ref cleaner
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1682
diff changeset
   393
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   394
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
   395
  """Clean method for cleaning user edit form.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   396
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   397
  Raises ValidationError if:
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   398
    -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
   399
    -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
   400
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   401
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   402
  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
   403
    """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
   404
    """
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   405
    cleaned_data = self.cleaned_data
1242
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1217
diff changeset
   406
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   407
    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
   408
    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
   409
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   410
    # 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
   411
    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
   412
      # get the user from the link_id in the form
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   413
      fields = {'link_id': link_id}
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   414
      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
   415
1285
d54f2d80f46e Fixed comment and typo in cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1242
diff changeset
   416
      # if it's not the user's current account
1242
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1217
diff changeset
   417
      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
   418
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   419
        # 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
   420
        fields = {'account': user_account}
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   421
        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
   422
            unique=True)
1013
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   423
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   424
        # 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
   425
        if user_from_account_entity or \
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   426
            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
   427
          # 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
   428
          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
   429
              "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
   430
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   431
    return cleaned_data
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   432
  return wrapper
68c52d6b3fb4 Added new clean methods in preparation for new User View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 967
diff changeset
   433
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
   434
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   435
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
   436
                       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
   437
  """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
   438
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
   439
    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
   440
    -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
   441
    -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
   442
  """
1287
9b18d612510a Added check_field_is_empty decorator to cleaning.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1285
diff changeset
   443
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
   444
  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
   445
    """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
   446
    """
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
   447
    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
   448
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
   449
    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
   450
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
   451
    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
   452
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
   453
    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
   454
      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
   455
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
   456
      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
   457
      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
   458
        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
   459
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
   460
      # 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
   461
      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
   462
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
   463
      # get the current user
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   464
      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
   465
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
   466
      # 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
   467
      # creating the new group then show link ID in use message
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
   468
      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
   469
          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
   470
        # 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
   471
        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
   472
        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
   473
        # 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
   474
        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
   475
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
   476
      # 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
   477
      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
   478
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
   479
      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
   480
        # 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
   481
        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
   482
        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
   483
        # 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
   484
        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
   485
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
   486
    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
   487
  return wrapper
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   488
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   489
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
   490
                              student_logic, org_logic):
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   491
  """Validates the form of a student proposal.
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   492
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   493
  Raises ValidationError if:
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   494
    -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
   495
    -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
   496
  """
1681
b0e10552f5c2 Fix some whitespace damadge
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1680
diff changeset
   497
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   498
  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
   499
    """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
   500
    """
1425
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   501
    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
   502
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   503
    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
   504
    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
   505
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   506
    # 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
   507
    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
   508
      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
   509
                'status': 'active'}
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   510
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   511
      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
   512
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   513
      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
   514
        # 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
   515
        raise forms.ValidationError(
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   516
            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
   517
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   518
      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
   519
                'scope': student_entity.scope,
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   520
                'status': 'active'}
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   521
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   522
      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
   523
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   524
      if not org_entity:
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   525
        #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
   526
        self._errors['organization'] = ErrorList(
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   527
            [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
   528
        del cleaned_data['organization']
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   529
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   530
    return cleaned_data
49d385edb6b4 Added new cleaning method to validate a student proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1395
diff changeset
   531
  return wrapper
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   532
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   533
def validate_document_acl(view):
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   534
  """Validates that the document ACL settings are correct.
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   535
  """
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   536
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   537
  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
   538
    """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
   539
    """
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   540
    cleaned_data = self.cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   541
    read_access = cleaned_data.get('read_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   542
    write_access = cleaned_data.get('write_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   543
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   544
    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
   545
      return cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   546
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   547
    if read_access != 'public':
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   548
      ordening = document_model.Document.DOCUMENT_ACCESS
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   549
      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
   550
        raise forms.ValidationError(
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   551
            "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
   552
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   553
    validate_access(self, view, 'read_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   554
    validate_access(self, view, 'write_access')
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   555
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   556
    return cleaned_data
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   557
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   558
  return wrapper
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   559
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   560
def validate_access(self, view, field):
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   561
  """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
   562
  """
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   563
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   564
  access_level = self.cleaned_data[field]
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   565
  prefix = self.cleaned_data['prefix']
1320
a5913d46e97e Include scope_path as django_args
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1311
diff changeset
   566
  scope_path = self.cleaned_data['scope_path']
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   567
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   568
  params = view.getParams()
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   569
  rights = params['rights']
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   570
1680
fbf8101f6eff Import user and site logic globals directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1621
diff changeset
   571
  user = user_logic.getForCurrentAccount()
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   572
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   573
  rights.setCurrentUser(user.account, user)
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   574
  checker = rights_logic.Checker(prefix)
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   575
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   576
  roles = checker.getMembership(access_level)
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   577
1440
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   578
  django_args = {
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   579
      'scope_path': scope_path,
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   580
      'prefix': prefix
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   581
      }
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   582
58ac0b3f59ac Also pass the prefix in the document cleaning method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1425
diff changeset
   583
  if not rights.hasMembership(roles, django_args):
1311
9836cfc0bb31 Add cleaning methods for document ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   584
    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
   585
    del self.cleaned_data[field]