app/soc/tasks/updates/student_school_type.py
changeset 2930 74d4875b4922
child 2932 666d31bd43bd
equal deleted inserted replaced
2929:04851beb824e 2930:74d4875b4922
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     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
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    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.
       
    16 
       
    17 """Appengine Task to convert the Student datastore model to contain
       
    18 school_type.
       
    19 """
       
    20 
       
    21 __authors__ = [
       
    22     '"Madhusudan.C.S" <madhusudancs@gmail.com>',
       
    23     '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    24   ]
       
    25 
       
    26 
       
    27 from google.appengine.api.labs import taskqueue
       
    28 from google.appengine.ext import db
       
    29 
       
    30 from django.http import HttpResponse
       
    31 
       
    32 from soc.views.helper import responses
       
    33 
       
    34 
       
    35 # batch size to use when going through Student entities.
       
    36 DEF_BATCH_SIZE = 25
       
    37 
       
    38 
       
    39 def startSchoolTypeUpdate(request, task_url):
       
    40   """Spawns a task for initiating the Student model conversion to include
       
    41   school type.
       
    42 
       
    43   Args:
       
    44     request: Django Request object
       
    45     task_url: The URL used to run this update task
       
    46 
       
    47   Returns:
       
    48     True iff the new task is successfully added to the Task Queue API
       
    49   """
       
    50 
       
    51   new_task = taskqueue.Task(url=task_url)
       
    52   new_task.add()
       
    53 
       
    54   return True
       
    55 
       
    56 
       
    57 def runSchoolTypeUpdate(request, *args, **kwargs):
       
    58   """Appengine Task that adds school_type as University for existing
       
    59   Student entities in batches.
       
    60 
       
    61   Addition of required school_type property to Student model 
       
    62   requires addition of corresponding value to all the existing 
       
    63   Student entities in the datastore. Since this property is introduced
       
    64   during GSoC 2009 all students should be University students.
       
    65   This task sets the school_type value to "University" to
       
    66   all the existing entities.
       
    67 
       
    68   Args:
       
    69     request: Django Request object
       
    70   """
       
    71 
       
    72   from soc.logic.models.student import logic as student_logic
       
    73   from soc.models.student import Student
       
    74 
       
    75   fields = {}
       
    76 
       
    77   post_dict = request.POST
       
    78 
       
    79   start_key = post_dict.get('start_key')
       
    80 
       
    81   if start_key:
       
    82     # retrieve the last student entity that was converted
       
    83     start = student_logic.getFromKeyName(start_key)
       
    84 
       
    85     if not start:
       
    86       # invalid starting student key specified, log and return OK
       
    87       return error_handler.logErrorAndReturnOK(
       
    88           'Invalid Student Key specified: %s' %(start_key))
       
    89 
       
    90     fields['__key__ >'] = start.key()
       
    91 
       
    92   # get the first batch_size number of StudentProjects
       
    93   entities = student_logic.getForFields(fields, limit=DEF_BATCH_SIZE)
       
    94 
       
    95   for entity in entities:
       
    96     entity.school_type = 'University'
       
    97 
       
    98   db.put(entities)
       
    99 
       
   100   if len(entities) == DEF_BATCH_SIZE:
       
   101     # spawn new task starting from the last
       
   102     new_start = entities[DEF_BATCH_SIZE-1].key().id_or_name()
       
   103 
       
   104     # pass along these params as POST to the new task
       
   105     task_params = {'start_key': new_start}
       
   106 
       
   107     new_task = taskqueue.Task(params=task_params, url=request['PATH_INFO'])
       
   108     new_task.add()
       
   109 
       
   110   # task completed, return OK
       
   111   return HttpResponse('OK')