app/soc/logic/models/student_proposal.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sun, 08 Mar 2009 13:11:49 +0000
changeset 1740 fbc2ad561c4b
parent 1624 1fc2089f4bc9
child 1753 564f3adf4acf
permissions -rw-r--r--
Added a new method in the review logic to return all reviews for the given entity as scope. 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
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    27
from soc.logic.models.ranker_root import logic as ranker_root_logic
1611
83e7b09ce959 Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1610
diff changeset
    28
from soc.models import student_proposal
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
import soc.models.linkable
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
import soc.models.student_proposal
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
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
class Logic(base.Logic):
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """Logic methods for the Student Proposal model.
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
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
  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
    39
               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
    40
               scope_logic=student_logic):
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
    """Defines the name, key_name and model for this entity.
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
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    super(Logic, self).__init__(model=model, base_model=base_model,
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
                                scope_logic=scope_logic)
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
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
    47
  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
    48
    """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
    49
    """
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
    50
1611
83e7b09ce959 Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1610
diff changeset
    51
    fields = {'link_id': student_proposal.DEF_RANKER_NAME,
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
    52
              'scope': entity.org}
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
    53
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    54
    ranker_root = ranker_root_logic.getForFields(fields, unique=True)
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    55
    ranker = ranker_root_logic.getRootFromEntity(ranker_root) 
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
    56
    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
    57
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
    58
    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
    59
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
    60
  def _updateField(self, entity, entity_properties, name):
1612
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    61
    """Update the ranker if the score changes and keep the score within bounds.
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
    62
    """
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
    63
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
    64
    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
    65
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
    66
    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
    67
      # 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
    68
      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
    69
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    70
      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
    71
      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
    72
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    73
      # update the ranker
1611
83e7b09ce959 Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1610
diff changeset
    74
      fields = {'link_id': student_proposal.DEF_RANKER_NAME,
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
    75
                'scope': entity.org}
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
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    77
      ranker_root = ranker_root_logic.getForFields(fields, unique=True)
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1612
diff changeset
    78
      ranker = ranker_root_logic.getRootFromEntity(ranker_root)
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
    79
      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
    80
1612
09bccdf6bdec Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1611
diff changeset
    81
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
    82
    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
    83
1422
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
052218c51035 Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
logic = Logic()