author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Wed, 04 Mar 2009 16:46:54 +0000 | |
changeset 1618 | 1a9820619d5f |
parent 1612 | 09bccdf6bdec |
child 1623 | 8b70d6bb3f8f |
permissions | -rw-r--r-- |
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 |
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
|
26 |
from soc.logic.models import rankerroot as ranker_logic |
1422
052218c51035
Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff
changeset
|
27 |
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
|
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, |
052218c51035
Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff
changeset
|
39 |
base_model=soc.models.linkable.Linkable, scope_logic=student_logic): |
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 |
|
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
|
46 |
def _onCreate(self, 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
|
47 |
"""Adds this proposal to the organization ranker 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
|
48 |
""" |
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 |
|
1611
83e7b09ce959
Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1610
diff
changeset
|
50 |
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
|
51 |
'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
|
52 |
|
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 |
ranker_root = ranker_logic.logic.getForFields(fields, unique=True) |
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
|
54 |
ranker = ranker_logic.logic.getRootFromEntity(ranker_root) |
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
|
55 |
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
|
56 |
|
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 |
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
|
58 |
|
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 |
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
|
60 |
"""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
|
61 |
""" |
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 |
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
|
64 |
|
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 |
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
|
66 |
# 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
|
67 |
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
|
68 |
|
09bccdf6bdec
Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1611
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
|
09bccdf6bdec
Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1611
diff
changeset
|
72 |
# update the ranker |
1611
83e7b09ce959
Brown paper bag fix in student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1610
diff
changeset
|
73 |
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
|
74 |
'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
|
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 |
ranker_root = ranker_logic.logic.getForFields(fields, unique=True) |
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
|
77 |
ranker = ranker_logic.logic.getRootFromEntity(ranker_root) |
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
|
78 |
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
|
79 |
|
1612
09bccdf6bdec
Keep the score within the allowed bounds via student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1611
diff
changeset
|
80 |
|
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 |
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
|
82 |
|
1422
052218c51035
Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff
changeset
|
83 |
|
052218c51035
Added student_proposal logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff
changeset
|
84 |
logic = Logic() |