app/soc/logic/models/mentor.py
author Lennard de Rijk <ljvderijk@gmail.com>
Thu, 23 Jul 2009 14:36:45 +0200
changeset 2664 06d4f4d06f64
parent 2072 eb4565a8e5b4
child 2672 7bc9d3b962af
permissions -rw-r--r--
Added registerRole to Role Logic where all roles are registered. This is used to make the use of the role property in the Request Model more easily consistent.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1121
diff changeset
     3
# Copyright 2009 the Melange authors.
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Mentor (Model) query functions.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
1982
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    25
from google.appengine.ext import db
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    26
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from soc.logic.models import role
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from soc.logic.models import organization as org_logic
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
import soc.models.mentor
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
import soc.models.role
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
1972
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    34
DEF_ALREADY_MENTORING_RPOJECT_MSG = "This Mentor is mentoring a Student "\
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    35
    "Project and can therefore not be resigned. Please assign another Mentor."
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    36
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    37
DEF_ALREADY_MENTORING_PROPOSAL_MSG = "This Mentor is mentoring a Student "\
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    38
    "Proposal and can therefore not be resigned. Please assign another Mentor."
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    39
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    40
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
class Logic(role.Logic):
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
  """Logic methods for the Mentor model.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
  """
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
  def __init__(self, model=soc.models.mentor.Mentor,
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1307
diff changeset
    46
               base_model=soc.models.role.Role, scope_logic=org_logic,
2664
06d4f4d06f64 Added registerRole to Role Logic where all roles are registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2072
diff changeset
    47
               role_name='mentor', disallow_last_resign=False):
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
    """Defines the name, key_name and model for this entity.
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
    """
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
2664
06d4f4d06f64 Added registerRole to Role Logic where all roles are registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2072
diff changeset
    51
    super(Logic, self).__init__(role_name=role_name, model=model,
06d4f4d06f64 Added registerRole to Role Logic where all roles are registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2072
diff changeset
    52
                                base_model=base_model, scope_logic=scope_logic,
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1307
diff changeset
    53
                                disallow_last_resign=disallow_last_resign)
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
1972
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    55
  def canResign(self, entity):
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    56
    """Checks if the Mentor is able to resign.
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    57
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    58
    Checks if there are no Student Proposals or Student Projects that
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    59
    have this mentor assigned to it.
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    60
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    61
    Args:
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    62
      entity: a Mentor entity
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    63
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    64
    """
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    65
2072
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    66
    from soc.logic.models.student_project import logic as \
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    67
        student_project_logic
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    68
    from soc.logic.models.student_proposal import logic as \
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    69
        student_proposal_logic
1972
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    70
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    71
    fields = {'mentor': entity}
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    72
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    73
    student_project_entity = student_project_logic.getForFields(fields,
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    74
                                                                unique=True)
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    75
    if student_project_entity:
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    76
      return DEF_ALREADY_MENTORING_RPOJECT_MSG
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    77
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    78
    student_proposal_entity = student_proposal_logic.getForFields(fields,
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    79
                                                                  unique=True)
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    80
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    81
    if student_proposal_entity:
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    82
      return DEF_ALREADY_MENTORING_PROPOSAL_MSG
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    83
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    84
    return super(Logic, self).canResign(entity)
5f1e3b5262cb Mentors cant resign when mentoring a project/proposal.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    85
1982
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    86
  def _updateField(self, entity, entity_properties, name):
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    87
    """Called when the fields of the mentor are updated
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    88
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    89
      When status is changed to invalid, removes the Mentor from all Student
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    90
      Proposals possible mentor lists.
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    91
    """
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    92
2072
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    93
    from soc.logic.models.student_proposal import logic \
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1982
diff changeset
    94
        as student_proposal_logic
1982
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    95
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    96
    value = entity_properties[name]
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    97
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    98
    if name == 'status' and value != entity.status and value == 'invalid':
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
    99
      fields = {'org': entity.scope}
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   100
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   101
      # TODO make this work for more then 1000 entities
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   102
      proposals_query = student_proposal_logic.getQueryForFields(fields)
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   103
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   104
      # store all updated proposals
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   105
      changed = []
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   106
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   107
      for proposal in proposals_query:
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   108
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   109
        if proposal.possible_mentors.count(entity.key()):
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   110
          # remove from list and add to changed
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   111
          proposal.possible_mentors.remove(entity.key())
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   112
          changed.append(proposal)
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   113
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   114
      # store all changed proposals
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   115
      db.put(changed)
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   116
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   117
    return super(Logic, self)._updateField(entity, entity_properties, name)
d3bebec883f1 Removes the Mentor from all Student Proposals possible mentor lists when resigning.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1972
diff changeset
   118
1121
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   119
77c602de9248 Added mentor and org_admin logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   120
logic = Logic()