(type_stubs, excel_importer=None)
return all_objs_by_code
Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects

    :param type_stubs: (list) list of LocationType objects with meta-data attributes and
          `needs_save`, 'is_new', 'db_object' set correctly
    :param excel_importer: Used for providing progress feedback. Disabled on None

    :returns: (dict) a dict of {object.code: object for all type objects}
Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects
Given a list of LocationTypeStub objects saves them to SQL as LocationType objects
def save_types(type_stubs, excel_importer=None):
    """
    Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects

    :param type_stubs: (list) list of LocationType objects with meta-data attributes and
          `needs_save`, 'is_new', 'db_object' set correctly
    :param excel_importer: Used for providing progress feedback. Disabled on None

    :returns: (dict) a dict of {object.code: object for all type objects}
    """

    # This proceeds in 3 steps
    # 1. Lookup all to be deleted types and 'bulk_delete' them
    # 2. Lookup all new types and 'bulk_create' the SQL objects, but don't set ForeignKey attrs like
    #    'parent' yet
    # 3. Lookup all to be updated types. Set foreign key attrs on these and new objects, and
    #    'bulk_update' the objects

    # step 1
    to_be_deleted_types = [lt.db_object for lt in type_stubs if lt.do_delete]
    LocationType.bulk_delete(to_be_deleted_types)
    if excel_importer:
        excel_importer.add_progress(len(to_be_deleted_types))
    # step 2
    new_type_objects = LocationType.bulk_create([lt.db_object for lt in type_stubs if lt.is_new])
    if excel_importer:
        excel_importer.add_progress(len(new_type_objects))
    # step 3
    type_objects_by_code = {lt.code: lt for lt in new_type_objects}
    type_objects_by_code.update({ROOT_LOCATION_TYPE: None})
    type_objects_by_code.update({
        lt.code: lt.db_object
        for lt in type_stubs
        if not lt.is_new and not lt.do_delete
    })
    to_bulk_update = []
    for lt in type_stubs:
        if (lt.needs_save or lt.is_new) and not lt.do_delete:
            # lookup foreign key attributes from stub and set them on objects
            type_object = type_objects_by_code[lt.code]
            type_object.parent_type = type_objects_by_code[lt.parent_code]
            to_bulk_update.append(type_object)

    LocationType.bulk_update(to_bulk_update)
    if excel_importer:
        excel_importer.add_progress(len(to_bulk_update))
    all_objs_by_code = {lt.code: lt for lt in to_bulk_update}
    all_objs_by_code.update({
        lt.code: lt.db_object
        for lt in type_stubs
        if not lt.needs_save
    })
    return all_objs_by_code
def save_types ( type_stubs , excel_importer = None ) : # This proceeds in 3 steps # 1. Lookup all to be deleted types and 'bulk_delete' them # 2. Lookup all new types and 'bulk_create' the SQL objects, but don't set ForeignKey attrs like #    'parent' yet # 3. Lookup all to be updated types. Set foreign key attrs on these and new objects, and #    'bulk_update' the objects # step 1 to_be_deleted_types = [ lt . db_object for lt in type_stubs if lt . do_delete ] LocationType . bulk_delete ( to_be_deleted_types ) if excel_importer : excel_importer . add_progress ( len ( to_be_deleted_types ) ) # step 2 new_type_objects = LocationType . bulk_create ( [ lt . db_object for lt in type_stubs if lt . is_new ] ) if excel_importer : excel_importer . add_progress ( len ( new_type_objects ) ) # step 3 type_objects_by_code = { lt . code : lt for lt in new_type_objects } type_objects_by_code . update ( { ROOT_LOCATION_TYPE : None } ) type_objects_by_code . update ( { lt . code : lt . db_object for lt in type_stubs if not lt . is_new and not lt . do_delete } ) to_bulk_update = [ ] for lt in type_stubs : if ( lt . needs_save or lt . is_new ) and not lt . do_delete : # lookup foreign key attributes from stub and set them on objects type_object = type_objects_by_code [ lt . code ] type_object . parent_type = type_objects_by_code [ lt . parent_code ] to_bulk_update . append ( type_object ) LocationType . bulk_update ( to_bulk_update ) if excel_importer : excel_importer . add_progress ( len ( to_bulk_update ) ) all_objs_by_code = { lt . code : lt for lt in to_bulk_update } all_objs_by_code . update ( { lt . code : lt . db_object for lt in type_stubs if not lt . needs_save } ) return all_objs_by_code