app/soc/modules/ghop/logic/cleaning.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Mon, 02 Nov 2009 23:38:43 +0100
changeset 3074 ebda36efbd61
parent 2943 8086f6562353
permissions -rw-r--r--
HtmlSanitizer becomes Python 2.6 compatible. The Cleaner class must not have any arguments when calling __init__ function for the object class, because in this case Python 2.6 raises TypeError (while previous versions just ignored them).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2820
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
#
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
#
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
#
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
#
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
"""GHOP module cleaning methods.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
"""
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
__authors__ = [
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
    '"Madhusudan.C.S" <madhusudancs@gmail.com>',
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
    ]
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
from django import forms
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
from django.utils.translation import ugettext
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    28
from soc.logic import cleaning
2893
1adc6a815c71 Several style and import fixes regarding the GHOP module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2884
diff changeset
    29
from soc.logic import validate
1adc6a815c71 Several style and import fixes regarding the GHOP module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2884
diff changeset
    30
2820
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
def cleanTaskComment(comment_field, action_field, ws_field):
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
  """Cleans the comment form and checks to see if there is either
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
  action or comment content.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
  Raises ValidationError if:
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
    -There is no action taking place and no comment present
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
    -The action is needs_review and there is no comment or work submission
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
     present
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
  """
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
  def wrapper(self):
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
    """Decorator wrapper method.
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
    """
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
    cleaned_data = self.cleaned_data
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    content = cleaned_data.get(comment_field)
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
    action = cleaned_data.get(action_field)
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
    work_submission = cleaned_data.get(ws_field)
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
    if action == 'noaction' and not content:
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
      raise forms.ValidationError(
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
          ugettext('You cannot have comment field empty with no action.'))
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
    if action == 'needs_review' and not content and not work_submission:
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
      raise forms.ValidationError(
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
          ugettext('You cannot have both comment field and work '
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
                   'submission fields empty.'))
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
    return cleaned_data
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
eb57ebee8b91 Added GHOP Form cleaning module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
  return wrapper
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    62
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    63
2884
2be8f6b8379e Renamed method in GHOP Cleaning to match style guide.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2861
diff changeset
    64
def cleanMentorsList(field_name):
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    65
  """Clean method to check and validate list of mentor's link_ids.
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    66
  """
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    67
2893
1adc6a815c71 Several style and import fixes regarding the GHOP module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2884
diff changeset
    68
  @cleaning.check_field_is_empty(field_name)
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    69
  def wrapper(self):
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    70
    """Decorator wrapped method.
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    71
    """
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    72
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    73
    from soc.modules.ghop.logic.models.mentor import logic as ghop_mentor_logic
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    74
2943
8086f6562353 Fixed error in mentor field cleaning, the list of mentors was never resolved.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2893
diff changeset
    75
    mentors_list_str = cleaning.str2set(field_name)(self)
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    76
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    77
    filter = {
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    78
        'scope_path': self.cleaned_data.get('scope_path'),
2861
17d5cfe85faf Fixed GHOP Cleaning clean_mentors_list method from using query on list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2860
diff changeset
    79
        'status': 'active'
2860
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    80
        }
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    81
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    82
    mentors_list = []
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    83
    for link_id in mentors_list_str:
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    84
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    85
      if not validate.isLinkIdFormatValid(link_id):
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    86
        raise forms.ValidationError(
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    87
            "%s is not a valid link ID." % link_id)
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    88
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    89
      filter['link_id'] = link_id
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    90
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    91
      if not ghop_mentor_logic.getFromKeyFields(filter):
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    92
        raise forms.ValidationError(
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    93
            'link_id "%s" is not a valid Mentor.' % link_id)
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    94
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    95
      mentors_list.append(link_id)
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    96
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    97
    return mentors_list
ea9909161840 Added cleaning method to validate a list of mentor's link_ids.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2820
diff changeset
    98
  return wrapper