'Common private method that saves/updates asset metadata items in the internal modulestore
structure used to store asset metadata items.'

def _save_assets_by_type(self, course_key, asset_metadata_list, course_assets, user_id, import_only):

    assets_by_type = defaultdict((lambda : SortedAssetList(iterable=course_assets.get(asset_type, []))))
    for asset_md in asset_metadata_list:
        if (asset_md.asset_id.course_key != course_key):
            log.warning("Asset's    course    {}    does    not    match    other    assets    for    course    {}    -    not    saved.".format(asset_md.asset_id.course_key, course_key))
            continue
        if (not import_only):
            asset_md.update({'edited_by': user_id, 'edited_on': datetime.datetime.now(UTC)})
        asset_type = asset_md.asset_id.asset_type
        all_assets = assets_by_type[asset_type]
        all_assets.insert_or_update(asset_md)
    return assets_by_type
