app/soc/tasks/updates/module_conversion.py
changeset 2996 9a62e3cad4a8
parent 2973 ba3f2522e8df
child 2997 a7cb53d213d7
equal deleted inserted replaced
2995:5931e6d6056f 2996:9a62e3cad4a8
    25 from google.appengine.api.labs import taskqueue
    25 from google.appengine.api.labs import taskqueue
    26 from google.appengine.ext import db
    26 from google.appengine.ext import db
    27 
    27 
    28 from django.http import HttpResponse
    28 from django.http import HttpResponse
    29 
    29 
       
    30 from soc.logic.models.program import logic as program_logic
       
    31 from soc.tasks.helper import decorators
    30 from soc.tasks.helper import error_handler
    32 from soc.tasks.helper import error_handler
    31 
    33 
    32 
    34 
    33 # batch size to use when going through the entities
    35 # batch size to use when going through the entities
    34 DEF_BATCH_SIZE = 10
    36 DEF_BATCH_SIZE = 10
    49   new_task.add()
    51   new_task.add()
    50 
    52 
    51   return True
    53   return True
    52 
    54 
    53 
    55 
    54 def runProgramConversionUpdate(request, *args, **kwargs):
    56 
       
    57 @decorators.iterative_task(program_logic)
       
    58 def runProgramConversionUpdate(request, entities, context, *args, **kwargs):
    55   """Appengine Task that converts Programs into GSoCPrograms.
    59   """Appengine Task that converts Programs into GSoCPrograms.
    56 
       
    57   The POST dict can contain the key where to start the conversion.
       
    58 
    60 
    59   Args:
    61   Args:
    60     request: Django Request object
    62     request: Django Request object
       
    63     entities: list of Program entities to convert
       
    64     context: the context of this task
    61   """
    65   """
    62 
    66 
    63   from soc.logic.models.program import logic as program_logic
       
    64 
       
    65   from soc.modules.gsoc.models.program import GSoCProgram
    67   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 
    68 
    87   # get all the properties that are part of each Programs
    69   # get all the properties that are part of each Programs
    88   program_model = program_logic.getModel()
    70   program_model = program_logic.getModel()
    89   program_properties = program_model.properties().keys()
    71   program_properties = program_model.properties().keys()
    90 
    72 
   103     gsoc_programs.append(gsoc_program_entity)
    85     gsoc_programs.append(gsoc_program_entity)
   104 
    86 
   105     # store all the new GSoCPrograms
    87     # store all the new GSoCPrograms
   106     db.put(gsoc_programs)
    88     db.put(gsoc_programs)
   107 
    89 
   108   if len(entities) == DEF_BATCH_SIZE:
    90   # task completed, return
   109     # spawn new task starting from the last
    91   return
   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')