app/soc/logic/models/student_project.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 02 Sep 2009 10:24:10 +0200
changeset 2854 003a84e774e2
parent 2643 222be0eb4aab
permissions -rw-r--r--
Add support for prefetching fields This makes it possible to specify the fields that should be prefetched as part of a getForFields query. Doing a prefetch on a ReferenceProperty when it is known that the property will be referenced in (almost all) entities is far more efficient than doing the invididual fetches sequentially.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1933
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Student Project (Model) query functions.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
2643
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    25
from google.appengine.ext import db
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    26
1933
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from soc.logic.models import base
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from soc.logic.models import organization as org_logic
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
import soc.models.linkable
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
import soc.models.student_project
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
class Logic(base.Logic):
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """Logic methods for the Student Project model.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
  """
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
  def __init__(self, model=soc.models.student_project.StudentProject,
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
               base_model=soc.models.linkable.Linkable, 
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
               scope_logic=org_logic):
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
    """Defines the name, key_name and model for this entity.
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
    """
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    super(Logic, self).__init__(model=model, base_model=base_model,
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
                                scope_logic=scope_logic)
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
2643
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    47
  def updateProjectsForGradingRecords(self, record_entities):
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    48
    """Updates StudentProjects using a list of GradingRecord entities.
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    49
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    50
    Args:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    51
      record_entities: List of GradingRecord entities to process.
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    52
    """
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    53
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    54
    projects_to_update = []
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    55
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    56
    for record_entity in record_entities:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    57
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    58
      project_entity = record_entity.project
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    59
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    60
      if project_entity.status in ['withdrawn', 'invalid']:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    61
        # skip this project
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    62
        continue
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    63
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    64
      # get the key from the GradingRecord entity since that gets stored
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    65
      record_key = record_entity.key()
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    66
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    67
      passed_evals = project_entity.passed_evaluations
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    68
      failed_evals = project_entity.failed_evaluations
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    69
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    70
      # try to remove this GradingRecord from the existing list of evals
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    71
      if record_key in passed_evals:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    72
        passed_evals.remove(record_key)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    73
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    74
      if record_key in failed_evals:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    75
        failed_evals.remove(record_key)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    76
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    77
      # get the grade_decision from the GradingRecord
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    78
      grade_decision = record_entity.grade_decision
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    79
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    80
      # update GradingRecord lists with respect to the grading_decision
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    81
      if grade_decision == 'pass':
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    82
        passed_evals.append(record_key)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    83
      elif grade_decision == 'fail':
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    84
        failed_evals.append(record_key)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    85
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    86
      if project_entity.status != 'completed':
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    87
        # Only when the project has not been completed should the status be
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    88
        # updated to reflect the new setting of the evaluations.
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    89
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    90
        if len(failed_evals) == 0:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    91
          # no failed evaluations present
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    92
          new_status = 'accepted'
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    93
        else:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    94
          new_status = 'failed'
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    95
      else:
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    96
          new_status = project_entity.status
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    97
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    98
      # update the necessary fields and store it before updating
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
    99
      project_entity.passed_evaluations = passed_evals
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   100
      project_entity.failed_evaluations = failed_evals
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   101
      project_entity.status = new_status
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   102
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   103
      projects_to_update.append(project_entity)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   104
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   105
    # batch put the StudentProjects that need to be updated
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   106
    db.put(projects_to_update)
222be0eb4aab Added updateProjectsForGradingRecords to StudentProject Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1933
diff changeset
   107
1933
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
49aef36e0022 Added StudentProject logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
logic = Logic()