app/soc/tasks/updates/module_conversion.py
changeset 2997 a7cb53d213d7
parent 2996 9a62e3cad4a8
child 2998 9c34768346da
equal deleted inserted replaced
2996:9a62e3cad4a8 2997:a7cb53d213d7
    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
    30 from soc.logic.models.program import logic as program_logic
       
    31 from soc.logic.models.organization import logic as org_logic
    31 from soc.tasks.helper import decorators
    32 from soc.tasks.helper import decorators
    32 from soc.tasks.helper import error_handler
    33 from soc.tasks.helper import error_handler
    33 
    34 
    34 
    35 
    35 # batch size to use when going through the entities
    36 # batch size to use when going through the entities
    79     for program_property in program_properties:
    80     for program_property in program_properties:
    80       # copy over all the information from the program entity
    81       # copy over all the information from the program entity
    81       gsoc_properties[program_property] = getattr(entity, program_property)
    82       gsoc_properties[program_property] = getattr(entity, program_property)
    82 
    83 
    83     # create the new GSoCProgram entity and prepare it to be stored
    84     # create the new GSoCProgram entity and prepare it to be stored
    84     gsoc_program_entity = GSoCProgram(key_name=entity.key().name(), **gsoc_properties)
    85     gsoc_program_entity = GSoCProgram(key_name=entity.key().name(),
       
    86                                       **gsoc_properties)
    85     gsoc_programs.append(gsoc_program_entity)
    87     gsoc_programs.append(gsoc_program_entity)
    86 
    88 
    87     # store all the new GSoCPrograms
    89     # store all the new GSoCPrograms
    88     db.put(gsoc_programs)
    90     db.put(gsoc_programs)
    89 
    91 
    90   # task completed, return
    92   # task completed, return
    91   return
    93   return
       
    94 
       
    95 
       
    96 @decorators.iterative_task(org_logic)
       
    97 def runOrgConversionUpdate(request, entities, context, *args, **kwargs):
       
    98   """Appengine Task that converts Organizations into GSoCOrganizations.
       
    99 
       
   100   Args:
       
   101     request: Django Request object
       
   102     entities: list of Organization entities to convert
       
   103     context: the context of this task
       
   104   """
       
   105 
       
   106   from soc.modules.gsoc.logic.models.program import logic as gsoc_program_logic
       
   107   from soc.modules.gsoc.models.organization import GSoCOrganization
       
   108 
       
   109   # get all the properties that are part of each Organization
       
   110   org_model = org_logic.getModel()
       
   111   org_properties = org_model.properties().keys()
       
   112 
       
   113   # use this to store all the new GSoCOrganization
       
   114   gsoc_orgs = []
       
   115 
       
   116   for entity in entities:
       
   117     gsoc_properties = {}
       
   118 
       
   119     for org_property in org_properties:
       
   120       # copy over all the information from the Organization entity
       
   121       gsoc_properties[org_property] = getattr(entity, org_property)
       
   122 
       
   123       # get the Program key belonging to the old Organization
       
   124       program_key = entity.scope.key().id_or_name()
       
   125       # get the new GSoCProgram and set it as scope for the GSoCOrganzation
       
   126       gsoc_program = gsoc_program_logic.getFromKeyName(program_key)
       
   127       gsoc_properties['scope'] = gsoc_program
       
   128 
       
   129     # create the new GSoCOrganization entity and prepare it to be stored
       
   130     gsoc_org_entity = GSoCOrganization(key_name=entity.key().name(),
       
   131                                        **gsoc_properties)
       
   132     gsoc_orgs.append(gsoc_org_entity)
       
   133 
       
   134     # store all the new GSoCOrganizations
       
   135     db.put(gsoc_orgs)
       
   136 
       
   137   # task completed, return
       
   138   return