app/soc/tasks/updates/module_conversion.py
changeset 2973 ba3f2522e8df
child 2996 9a62e3cad4a8
equal deleted inserted replaced
2972:1e1b8f22e153 2973:ba3f2522e8df
       
     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 """The module conversion updates are defined in this module.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api.labs import taskqueue
       
    26 from google.appengine.ext import db
       
    27 
       
    28 from django.http import HttpResponse
       
    29 
       
    30 from soc.tasks.helper import error_handler
       
    31 
       
    32 
       
    33 # batch size to use when going through the entities
       
    34 DEF_BATCH_SIZE = 10
       
    35 
       
    36 
       
    37 def startUpdateWithUrl(request, task_url):
       
    38   """Spawns an update task for the given task URL.
       
    39 
       
    40   Args:
       
    41     request: Django Request object
       
    42     task_url: The URL used to run this update task
       
    43 
       
    44   Returns:
       
    45     True iff the new task is successfully added to the Task Queue API
       
    46   """
       
    47 
       
    48   new_task = taskqueue.Task(url=task_url)
       
    49   new_task.add()
       
    50 
       
    51   return True
       
    52 
       
    53 
       
    54 def runProgramConversionUpdate(request, *args, **kwargs):
       
    55   """Appengine Task that converts Programs into GSoCPrograms.
       
    56 
       
    57   The POST dict can contain the key where to start the conversion.
       
    58 
       
    59   Args:
       
    60     request: Django Request object
       
    61   """
       
    62 
       
    63   from soc.logic.models.program import logic as program_logic
       
    64 
       
    65   from soc.modules.gsoc.models.program import GSoCProgram
       
    66 
       
    67   fields = {}
       
    68 
       
    69   post_dict = request.POST
       
    70 
       
    71   start_key = post_dict.get('start_key')
       
    72 
       
    73   if start_key:
       
    74     # retrieve the last Program entity that was converted
       
    75     start = program_logic.getFromKeyName(start_key)
       
    76 
       
    77     if not start:
       
    78       # invalid starting key specified, log and return OK
       
    79       return error_handler.logErrorAndReturnOK(
       
    80           'Invalid Program Key specified: %s' %(start_key))
       
    81 
       
    82     fields['__key__ >'] = start.key()
       
    83 
       
    84   # get batch_size number of Programs
       
    85   entities = program_logic.getForFields(fields, limit=DEF_BATCH_SIZE)
       
    86 
       
    87   # get all the properties that are part of each Programs
       
    88   program_model = program_logic.getModel()
       
    89   program_properties = program_model.properties().keys()
       
    90 
       
    91   # use this to store all the new GSoCPrograms
       
    92   gsoc_programs = []
       
    93 
       
    94   for entity in entities:
       
    95     gsoc_properties = {}
       
    96 
       
    97     for program_property in program_properties:
       
    98       # copy over all the information from the program entity
       
    99       gsoc_properties[program_property] = getattr(entity, program_property)
       
   100 
       
   101     # create the new GSoCProgram entity and prepare it to be stored
       
   102     gsoc_program_entity = GSoCProgram(key_name=entity.key().name(), **gsoc_properties)
       
   103     gsoc_programs.append(gsoc_program_entity)
       
   104 
       
   105     # store all the new GSoCPrograms
       
   106     db.put(gsoc_programs)
       
   107 
       
   108   if len(entities) == DEF_BATCH_SIZE:
       
   109     # spawn new task starting from the last
       
   110     new_start = entities[DEF_BATCH_SIZE-1].key().id_or_name()
       
   111 
       
   112     # pass along these params as POST to the new task
       
   113     task_params = {'start_key': new_start}
       
   114 
       
   115     new_task = taskqueue.Task(params=task_params,
       
   116                               url=request.META['PATH_INFO'])
       
   117     new_task.add()
       
   118 
       
   119   # task completed, return OK
       
   120   return HttpResponse('OK')