(processing_id, domain, app_id, username=None, share_media=False,
                            license_name=None, author=None, attribution_notes=None)
Responsible for processing the uploaded zip from Bulk Upload.
Responsible for processing the uploaded zip from Bulk Upload.
Responsible for processing the uploaded zip from Bulk Upload .
def process_bulk_upload_zip(processing_id, domain, app_id, username=None, share_media=False,
                            license_name=None, author=None, attribution_notes=None):
    """
        Responsible for processing the uploaded zip from Bulk Upload.
    """

    status = BulkMultimediaStatusCache.get(processing_id)

    if not status:
        # no download data available, abort
        return

    app = get_app(domain, app_id)

    status.in_celery = True
    status.save()

    uploaded_zip = status.get_upload_zip()
    if not uploaded_zip:
        return

    zipped_files = uploaded_zip.namelist()
    status.total_files = len(zipped_files)
    checked_paths = []

    try:
        save_app = False
        for index, path in enumerate(zipped_files):
            status.update_progress(len(checked_paths))
            checked_paths.append(path)
            file_name = os.path.basename(path)
            try:
                data = uploaded_zip.read(path)
            except Exception as e:
                status.add_unmatched_path(path, _("Error reading file: %s" % e))
                continue

            media_class = CommCareMultimedia.get_class_by_data(data, filename=path)
            if not media_class:
                status.add_skipped_path(path, CommCareMultimedia.get_mime_type(data))
                continue

            app_paths = list(app.get_all_paths_of_type(media_class.__name__))
            app_paths_lower = [p.lower() for p in app_paths]
            form_path = media_class.get_form_path(path, lowercase=True)

            if not form_path in app_paths_lower:
                status.add_unmatched_path(path,
                                          _("Did not match any %s paths in application." % media_class.get_nice_name()))
                continue

            index_of_path = app_paths_lower.index(form_path)
            form_path = app_paths[index_of_path]  # this is the correct capitalization as specified in the form

            multimedia = media_class.get_by_data(data)
            if not multimedia:
                status.add_unmatched_path(path,
                                          _("Matching path found, but could not save the data to couch."))
                continue

            is_new = form_path not in app.multimedia_map
            is_updated = multimedia.attach_data(data,
                                                original_filename=file_name,
                                                username=username)

            if not is_updated and not getattr(multimedia, '_id'):
                status.add_unmatched_path(form_path,
                                          _("Matching path found, but didn't save new multimedia correctly."))
                continue

            if is_updated or is_new:
                multimedia.add_domain(domain, owner=True)
                if share_media:
                    multimedia.update_or_add_license(domain, type=license_name, author=author,
                                                     attribution_notes=attribution_notes)
                save_app = True
                app.create_mapping(multimedia, form_path, save=False)

            media_info = multimedia.get_media_info(form_path, is_updated=is_updated, original_path=path)
            status.add_matched_path(media_class, media_info)

        if save_app:
            app.save()
        status.update_progress(len(checked_paths))
    except Exception as e:
        status.mark_with_error(_("Error while processing zip: %s" % e))
    uploaded_zip.close()

    status.complete = True
    status.save()
def process_bulk_upload_zip ( processing_id , domain , app_id , username = None , share_media = False , license_name = None , author = None , attribution_notes = None ) : status = BulkMultimediaStatusCache . get ( processing_id ) if not status : # no download data available, abort return app = get_app ( domain , app_id ) status . in_celery = True status . save ( ) uploaded_zip = status . get_upload_zip ( ) if not uploaded_zip : return zipped_files = uploaded_zip . namelist ( ) status . total_files = len ( zipped_files ) checked_paths = [ ] try : save_app = False for index , path in enumerate ( zipped_files ) : status . update_progress ( len ( checked_paths ) ) checked_paths . append ( path ) file_name = os . path . basename ( path ) try : data = uploaded_zip . read ( path ) except Exception as e : status . add_unmatched_path ( path , _ ( "Error reading file: %s" % e ) ) continue media_class = CommCareMultimedia . get_class_by_data ( data , filename = path ) if not media_class : status . add_skipped_path ( path , CommCareMultimedia . get_mime_type ( data ) ) continue app_paths = list ( app . get_all_paths_of_type ( media_class . __name__ ) ) app_paths_lower = [ p . lower ( ) for p in app_paths ] form_path = media_class . get_form_path ( path , lowercase = True ) if not form_path in app_paths_lower : status . add_unmatched_path ( path , _ ( "Did not match any %s paths in application." % media_class . get_nice_name ( ) ) ) continue index_of_path = app_paths_lower . index ( form_path ) form_path = app_paths [ index_of_path ] # this is the correct capitalization as specified in the form multimedia = media_class . get_by_data ( data ) if not multimedia : status . add_unmatched_path ( path , _ ( "Matching path found, but could not save the data to couch." ) ) continue is_new = form_path not in app . multimedia_map is_updated = multimedia . attach_data ( data , original_filename = file_name , username = username ) if not is_updated and not getattr ( multimedia , '_id' ) : status . add_unmatched_path ( form_path , _ ( "Matching path found, but didn't save new multimedia correctly." ) ) continue if is_updated or is_new : multimedia . add_domain ( domain , owner = True ) if share_media : multimedia . update_or_add_license ( domain , type = license_name , author = author , attribution_notes = attribution_notes ) save_app = True app . create_mapping ( multimedia , form_path , save = False ) media_info = multimedia . get_media_info ( form_path , is_updated = is_updated , original_path = path ) status . add_matched_path ( media_class , media_info ) if save_app : app . save ( ) status . update_progress ( len ( checked_paths ) ) except Exception as e : status . mark_with_error ( _ ( "Error while processing zip: %s" % e ) ) uploaded_zip . close ( ) status . complete = True status . save ( )