Style fixes and removal of unused imports in soc.logic.models modules.
[Melange.git] / app / soc / logic / models / mentor.py
blob4741dfe8ed8765b25dc42bf811a77f88efeead56
1 #!/usr/bin/python2.5
3 # Copyright 2009 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Mentor (Model) query functions.
18 """
20 __authors__ = [
21 '"Lennard de Rijk" <ljvderijk@gmail.com>',
25 from google.appengine.ext import db
27 from soc.logic.models import role
28 from soc.logic.models import organization as org_logic
30 import soc.models.mentor
31 import soc.models.role
34 DEF_ALREADY_MENTORING_RPOJECT_MSG = "This Mentor is mentoring a Student "\
35 "Project and can therefore not be resigned. Please assign another Mentor."
37 DEF_ALREADY_MENTORING_PROPOSAL_MSG = "This Mentor is mentoring a Student "\
38 "Proposal and can therefore not be resigned. Please assign another Mentor."
41 class Logic(role.Logic):
42 """Logic methods for the Mentor model.
43 """
45 def __init__(self, model=soc.models.mentor.Mentor,
46 base_model=soc.models.role.Role, scope_logic=org_logic,
47 disallow_last_resign=False):
48 """Defines the name, key_name and model for this entity.
49 """
51 super(Logic, self).__init__(model=model, base_model=base_model,
52 scope_logic=scope_logic,
53 disallow_last_resign=disallow_last_resign)
55 def canResign(self, entity):
56 """Checks if the Mentor is able to resign.
58 Checks if there are no Student Proposals or Student Projects that
59 have this mentor assigned to it.
61 Args:
62 entity: a Mentor entity
64 """
66 from soc.logic.models.student_project import logic as \
67 student_project_logic
68 from soc.logic.models.student_proposal import logic as \
69 student_proposal_logic
71 fields = {'mentor': entity}
73 student_project_entity = student_project_logic.getForFields(fields,
74 unique=True)
75 if student_project_entity:
76 return DEF_ALREADY_MENTORING_RPOJECT_MSG
78 student_proposal_entity = student_proposal_logic.getForFields(fields,
79 unique=True)
81 if student_proposal_entity:
82 return DEF_ALREADY_MENTORING_PROPOSAL_MSG
84 return super(Logic, self).canResign(entity)
86 def _updateField(self, entity, entity_properties, name):
87 """Called when the fields of the mentor are updated
89 When status is changed to invalid, removes the Mentor from all Student
90 Proposals possible mentor lists.
91 """
93 from soc.logic.models.student_proposal import logic \
94 as student_proposal_logic
96 value = entity_properties[name]
98 if name == 'status' and value != entity.status and value == 'invalid':
99 fields = {'org': entity.scope}
101 # TODO make this work for more then 1000 entities
102 proposals_query = student_proposal_logic.getQueryForFields(fields)
104 # store all updated proposals
105 changed = []
107 for proposal in proposals_query:
109 if proposal.possible_mentors.count(entity.key()):
110 # remove from list and add to changed
111 proposal.possible_mentors.remove(entity.key())
112 changed.append(proposal)
114 # store all changed proposals
115 db.put(changed)
117 return super(Logic, self)._updateField(entity, entity_properties, name)
120 logic = Logic()