app/soc/logic/models/student_proposal.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 10 Mar 2009 19:06:16 +0000
changeset 1786 d9694a1f1a56
parent 1753 564f3adf4acf
child 1884 90b372ece1a5
permissions -rw-r--r--
Added getRankerFor method to student_proposal logic. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Student Proposal (Model) query functions.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
from soc.logic.models import base
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
from soc.logic.models import student as student_logic
1611
83e7b09ce959 Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1610
diff changeset
    27
from soc.models import student_proposal
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
import soc.models.linkable
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
import soc.models.student_proposal
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
class Logic(base.Logic):
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
  """Logic methods for the Student Proposal model.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
  def __init__(self, model=soc.models.student_proposal.StudentProposal,
1624
1fc2089f4bc9 Fix too long lines in soc.logic.models.student_proposal module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1623
diff changeset
    38
               base_model=soc.models.linkable.Linkable, 
1fc2089f4bc9 Fix too long lines in soc.logic.models.student_proposal module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1623
diff changeset
    39
               scope_logic=student_logic):
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
    """Defines the name, key_name and model for this entity.
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
    """
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
    super(Logic, self).__init__(model=model, base_model=base_model,
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
                                scope_logic=scope_logic)
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
1786
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    46
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    47
  def getRankerFor(self, entity):
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    48
    """Returns the ranker for the given Student Proposal.
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    49
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    50
    Args:
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    51
      entity: Student Proposal entity for which the ranker should be returned
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    52
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    53
    Returns:
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    54
      Ranker object which is used to rank the given entity
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    55
    """
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    56
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    57
    from soc.logic.models.ranker_root import logic as ranker_root_logic
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    58
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    59
    fields = {'link_id': student_proposal.DEF_RANKER_NAME,
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    60
        'scope': entity.org}
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    61
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    62
    ranker_root = ranker_root_logic.getForFields(fields, unique=True)
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    63
    ranker = ranker_root_logic.getRootFromEntity(ranker_root)
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    64
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    65
    return ranker
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    66
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    67
  def _onCreate(self, entity):
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    68
    """Adds this proposal to the organization ranker entity.
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    69
    """
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    70
1786
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    71
    ranker = self.getRankerFor(entity)
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    72
    ranker.SetScore(entity.key().name(), [entity.score])
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    73
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    74
    super(Logic, self)._onCreate(entity)
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    75
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    76
  def _updateField(self, entity, entity_properties, name):
1753
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    77
    """Called when the fields of the student_proposal are updated.
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    78
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    79
      - Update the ranker if the score changes and keep the score within bounds
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    80
      - Remove the entity from the ranker when the status changes to invalid or rejected
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    81
    """
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    82
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    83
    value = entity_properties[name]
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    84
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    85
    if name == 'score':
1612
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    86
      # keep the score within bounds
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    87
      min_score, max_score = student_proposal.DEF_SCORE
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    88
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    89
      value = max(min_score, min(value, max_score-1))
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    90
      entity_properties[name] = value
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    91
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    92
      # update the ranker
1786
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
    93
      ranker = self.getRankerFor(entity)
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    94
      ranker.SetScore(entity.key().name(), [value])
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
    95
1753
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    96
    if name == 'status':
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    97
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    98
      if value in ['invalid', 'rejected'] and entity.status != value:
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
    99
        # the proposal is going into invalid or rejected state
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
   100
        # remove the score from the ranker
1786
d9694a1f1a56 Added getRankerFor method to student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1753
diff changeset
   101
        ranker = self.getRankerFor(entity)
1753
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
   102
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
   103
        # entries in the ranker can be removed by setting the score to None
564f3adf4acf Students who withdraw their proposal will now be removed from the ranker.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1624
diff changeset
   104
        ranker.SetScore(entity.key().name(), None)
1612
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
   105
1610
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
   106
    return super(Logic, self)._updateField(entity, entity_properties, name)
6f675b690f92 Update the ranker belonging to the org of a student_proposal when the score changes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1422
diff changeset
   107
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
logic = Logic()