app/soc/tasks/updates/student_school_type.py
author Mario Ferraro <fadinlight@gmail.com>
Sun, 15 Nov 2009 22:12:20 +0100
changeset 3093 d1be59b6b627
parent 2969 d0914c76ecc3
permissions -rw-r--r--
GMaps related JS changed to use new google namespace. Google is going to change permanently in the future the way to load its services, so better stay safe. Also this commit shows uses of the new melange.js module. Fixes Issue 634.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2930
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     2
#
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     4
#
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     8
#
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    10
#
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    15
# limitations under the License.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    16
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    17
"""Appengine Task to convert the Student datastore model to contain
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    18
school_type.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    19
"""
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    20
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    21
__authors__ = [
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    22
    '"Madhusudan.C.S" <madhusudancs@gmail.com>',
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    23
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    24
  ]
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    25
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    26
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    27
from google.appengine.api.labs import taskqueue
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    28
from google.appengine.ext import db
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    29
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    30
from django.http import HttpResponse
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    31
2969
d0914c76ecc3 Corrected an import in the student_school_type update module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2932
diff changeset
    32
from soc.tasks.helper import error_handler
2930
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    33
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    34
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    35
# batch size to use when going through Student entities.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    36
DEF_BATCH_SIZE = 25
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    37
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    38
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    39
def startSchoolTypeUpdate(request, task_url):
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    40
  """Spawns a task for initiating the Student model conversion to include
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    41
  school type.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    42
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    43
  Args:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    44
    request: Django Request object
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    45
    task_url: The URL used to run this update task
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    46
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    47
  Returns:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    48
    True iff the new task is successfully added to the Task Queue API
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    49
  """
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    50
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    51
  new_task = taskqueue.Task(url=task_url)
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    52
  new_task.add()
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    53
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    54
  return True
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    55
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    56
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    57
def runSchoolTypeUpdate(request, *args, **kwargs):
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    58
  """Appengine Task that adds school_type as University for existing
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    59
  Student entities in batches.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    60
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    61
  Addition of required school_type property to Student model 
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    62
  requires addition of corresponding value to all the existing 
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    63
  Student entities in the datastore. Since this property is introduced
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    64
  during GSoC 2009 all students should be University students.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    65
  This task sets the school_type value to "University" to
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    66
  all the existing entities.
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    67
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    68
  Args:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    69
    request: Django Request object
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    70
  """
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    71
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    72
  from soc.logic.models.student import logic as student_logic
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    73
  from soc.models.student import Student
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    74
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    75
  fields = {}
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    76
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    77
  post_dict = request.POST
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    78
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    79
  start_key = post_dict.get('start_key')
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    80
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    81
  if start_key:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    82
    # retrieve the last student entity that was converted
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    83
    start = student_logic.getFromKeyName(start_key)
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    84
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    85
    if not start:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    86
      # invalid starting student key specified, log and return OK
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    87
      return error_handler.logErrorAndReturnOK(
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    88
          'Invalid Student Key specified: %s' %(start_key))
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    89
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    90
    fields['__key__ >'] = start.key()
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    91
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    92
  # get the first batch_size number of StudentProjects
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    93
  entities = student_logic.getForFields(fields, limit=DEF_BATCH_SIZE)
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    94
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    95
  for entity in entities:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    96
    entity.school_type = 'University'
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    97
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    98
  db.put(entities)
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
    99
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   100
  if len(entities) == DEF_BATCH_SIZE:
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   101
    # spawn new task starting from the last
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   102
    new_start = entities[DEF_BATCH_SIZE-1].key().id_or_name()
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   103
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   104
    # pass along these params as POST to the new task
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   105
    task_params = {'start_key': new_start}
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   106
2932
666d31bd43bd Fixed the Student update to properly find its own URL.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2930
diff changeset
   107
    new_task = taskqueue.Task(params=task_params,
666d31bd43bd Fixed the Student update to properly find its own URL.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2930
diff changeset
   108
                              url=request.META['PATH_INFO'])
2930
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   109
    new_task.add()
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   110
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   111
  # task completed, return OK
74d4875b4922 Added updater to update the existing Student models with the school_type entry.
Madhusudan.C.S <madhusudancs@gmai.com>
parents:
diff changeset
   112
  return HttpResponse('OK')