# HG changeset patch # User Madhusudan.C.S # Date 1253100953 -7200 # Node ID 74d4875b49227ca439e3b3ea111c3b6f1dae49f9 # Parent 04851beb824e036746cf0f1a2577c9125f690a99 Added updater to update the existing Student models with the school_type entry. Reviewed by: Lennard de Rijk diff -r 04851beb824e -r 74d4875b4922 app/soc/tasks/updates/student_school_type.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/soc/tasks/updates/student_school_type.py Wed Sep 16 13:35:53 2009 +0200 @@ -0,0 +1,111 @@ +#!/usr/bin/python2.5 +# +# Copyright 2009 the Melange authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Appengine Task to convert the Student datastore model to contain +school_type. +""" + +__authors__ = [ + '"Madhusudan.C.S" ', + '"Lennard de Rijk" ', + ] + + +from google.appengine.api.labs import taskqueue +from google.appengine.ext import db + +from django.http import HttpResponse + +from soc.views.helper import responses + + +# batch size to use when going through Student entities. +DEF_BATCH_SIZE = 25 + + +def startSchoolTypeUpdate(request, task_url): + """Spawns a task for initiating the Student model conversion to include + school type. + + Args: + request: Django Request object + task_url: The URL used to run this update task + + Returns: + True iff the new task is successfully added to the Task Queue API + """ + + new_task = taskqueue.Task(url=task_url) + new_task.add() + + return True + + +def runSchoolTypeUpdate(request, *args, **kwargs): + """Appengine Task that adds school_type as University for existing + Student entities in batches. + + Addition of required school_type property to Student model + requires addition of corresponding value to all the existing + Student entities in the datastore. Since this property is introduced + during GSoC 2009 all students should be University students. + This task sets the school_type value to "University" to + all the existing entities. + + Args: + request: Django Request object + """ + + from soc.logic.models.student import logic as student_logic + from soc.models.student import Student + + fields = {} + + post_dict = request.POST + + start_key = post_dict.get('start_key') + + if start_key: + # retrieve the last student entity that was converted + start = student_logic.getFromKeyName(start_key) + + if not start: + # invalid starting student key specified, log and return OK + return error_handler.logErrorAndReturnOK( + 'Invalid Student Key specified: %s' %(start_key)) + + fields['__key__ >'] = start.key() + + # get the first batch_size number of StudentProjects + entities = student_logic.getForFields(fields, limit=DEF_BATCH_SIZE) + + for entity in entities: + entity.school_type = 'University' + + db.put(entities) + + if len(entities) == DEF_BATCH_SIZE: + # spawn new task starting from the last + new_start = entities[DEF_BATCH_SIZE-1].key().id_or_name() + + # pass along these params as POST to the new task + task_params = {'start_key': new_start} + + new_task = taskqueue.Task(params=task_params, url=request['PATH_INFO']) + new_task.add() + + # task completed, return OK + return HttpResponse('OK')