def post(self, request, *args, **kwargs): handle = request.FILES['bulk_upload_file'] extension = os.path.splitext(handle.name)[1][1:].strip().lower() if extension not in valid_extensions: messages.error(request, _("Please choose a file with one of the following extensions: " "{}").format(", ".join(valid_extensions))) return self.get(request, *args, **kwargs) meta = transient_file_store.write_file(handle, handle.name, self.domain) file_id = meta.identifier f = transient_file_store.get_tempfile_ref_for_contents(file_id) try: open_spreadsheet_download_ref(f) except SpreadsheetFileExtError: messages.error(request, _("File does not appear to be an Excel file. Please choose another file.")) return self.get(request, *args, **kwargs) from corehq.apps.app_manager.views.media_utils import interpolate_media_path from corehq.apps.hqmedia.view_helpers import validate_multimedia_paths_rows, update_multimedia_paths # Get rows, filtering out header, no-ops, and any extra "Usages" columns rows = [] with get_spreadsheet(f) as spreadsheet: for row in list(spreadsheet.iter_rows())[1:]: if row[1]: rows.append(row[:2]) (errors, warnings) = validate_multimedia_paths_rows(self.app, rows) if len(errors): for msg in errors: messages.error(request, msg, extra_tags='html') return self.get(request, *args, **kwargs) paths = { row[0]: interpolate_media_path(row[1]) for row in rows if row[1] } successes = update_multimedia_paths(self.app, paths) self.app.save() # Force all_media to reset self.app.all_media.reset_cache(self.app) self.app.all_media_paths.reset_cache(self.app) # Warn if any old paths remain in app (because they're used in a place this function doesn't know about) warnings = [] self.app.remove_unused_mappings() app_paths = {m.path: True for m in self.app.all_media()} for old_path, new_path in paths.items(): if old_path in app_paths: warnings.append(_("Could not completely update path <code>{}</code>, " "please check app for remaining references.").format(old_path)) for msg in successes: messages.success(request, msg, extra_tags='html') for msg in warnings: messages.warning(request, msg, extra_tags='html') return self.get(request, *args, **kwargs)
def validate_multimedia_paths_rows(app, rows): old_paths_last_seen = {i.path: None for i in app.all_media()} new_paths_last_seen = defaultdict(lambda: None) errors = [] warnings = [] for i, row in enumerate(rows): (old_path, new_path) = row if old_path not in old_paths_last_seen: errors.append( _("Path in row {} could not be found in application: " "<code>{}</code>").format(i, old_path)) elif old_path == new_path: errors.append( _("In row {}, old and new paths are both <code>{}</code>. Please provide " "an updated path or remove this row").format(i, old_path)) elif old_paths_last_seen[old_path] is not None: # Duplicate old paths is an error: can't rename to two different new values errors.append( _("Path in row {} was already renamed in row {}: " "<code>{}</code>").format(i, old_paths_last_seen[old_path], old_path)) old_paths_last_seen[old_path] = i interpolated_new_path = interpolate_media_path( new_path) # checks for jr:// if interpolated_new_path != new_path: warnings.append( _("Path <code>{}</code> in row {} was replaced with " "<code>{}</code>").format(new_path, i, interpolated_new_path)) else: # It's usually a bad idea to change file extensions, since the file itself isn't changing old_extension = old_path.split(".")[-1].lower() new_extension = new_path.split(".")[-1].lower() if old_extension != new_extension: warnings.append( _("File extension in row {} changed " "from {} to {}".format(i, old_extension, new_extension))) # Duplicate new paths is a warning: will combine what were previously different items if new_path in new_paths_last_seen: warnings.append( _("New path in row {} was already used to rename row {}: " "<code>{}</code>").format(i, new_paths_last_seen[new_path], new_path)) new_paths_last_seen[new_path] = i return errors, warnings
def validate_multimedia_paths_rows(app, rows): old_paths_last_seen = {i.path: None for i in app.all_media()} new_paths_last_seen = defaultdict(lambda: None) errors = [] warnings = [] for i, row in enumerate(rows): (old_path, new_path) = row if old_path not in old_paths_last_seen: errors.append(_("Path in row {} could not be found in application: " "<code>{}</code>").format(i, old_path)) elif old_path == new_path: errors.append(_("In row {}, old and new paths are both <code>{}</code>. Please provide " "an updated path or remove this row").format(i, old_path)) elif old_paths_last_seen[old_path] is not None: # Duplicate old paths is an error: can't rename to two different new values errors.append(_("Path in row {} was already renamed in row {}: " "<code>{}</code>").format(i, old_paths_last_seen[old_path], old_path)) old_paths_last_seen[old_path] = i interpolated_new_path = interpolate_media_path(new_path) # checks for jr:// if interpolated_new_path != new_path: warnings.append(_("Path <code>{}</code> in row {} was replaced with " "<code>{}</code>").format(new_path, i, interpolated_new_path)) else: # It's usually a bad idea to change file extensions, since the file itself isn't changing old_extension = old_path.split(".")[-1].lower() new_extension = new_path.split(".")[-1].lower() if old_extension != new_extension: warnings.append(_("File extension in row {} changed " "from {} to {}".format(i, old_extension, new_extension))) # Duplicate new paths is a warning: will combine what were previously different items if new_path in new_paths_last_seen: warnings.append(_("New path in row {} was already used to rename row {}: " "<code>{}</code>").format(i, new_paths_last_seen[new_path], new_path)) new_paths_last_seen[new_path] = i return errors, warnings
def update_multimedia_paths(request, domain, app_id): if not request.FILES: return json_response( {'error': _("Please choose an Excel file to import.")}) handle = request.FILES['file'] extension = os.path.splitext(handle.name)[1][1:].strip().lower() if extension not in ALLOWED_EXTENSIONS: return json_response({ 'error': _("Please choose a file with one of the following extensions: " "{}").format(", ".join(ALLOWED_EXTENSIONS)) }) meta = transient_file_store.write_file(handle, handle.name, domain) file_id = meta.identifier f = transient_file_store.get_tempfile_ref_for_contents(file_id) try: open_spreadsheet_download_ref(f) except SpreadsheetFileExtError: return json_response({ 'error': _("File does not appear to be an Excel file. Please choose another file." ) }) app = get_app(domain, app_id) from corehq.apps.app_manager.views.media_utils import interpolate_media_path from corehq.apps.hqmedia.view_helpers import validate_multimedia_paths_rows, update_multimedia_paths # Get rows, filtering out header, no-ops, and any extra "Usages" columns rows = [] with get_spreadsheet(f) as spreadsheet: for row in list(spreadsheet.iter_rows())[1:]: if row[1]: rows.append(row[:2]) (errors, warnings) = validate_multimedia_paths_rows(app, rows) if len(errors): return json_response({ 'complete': 1, 'errors': errors, }) paths = {row[0]: interpolate_media_path(row[1]) for row in rows if row[1]} successes = update_multimedia_paths(app, paths) app.save() # Force all_media to reset app.all_media.reset_cache(app) app.all_media_paths.reset_cache(app) # Warn if any old paths remain in app (because they're used in a place this function doesn't know about) warnings = [] app.remove_unused_mappings() app_paths = {m.path: True for m in app.all_media()} for old_path, new_path in paths.items(): if old_path in app_paths: warnings.append( _("Could not completely update path <code>{}</code>, " "please check app for remaining references.").format( old_path)) return json_response({ 'complete': 1, 'successes': successes, 'warnings': warnings, })
def update_multimedia_paths(request, domain, app_id): if not request.FILES: return json_response({ 'error': _("Please choose an Excel file to import.") }) handle = request.FILES['file'] extension = os.path.splitext(handle.name)[1][1:].strip().lower() if extension not in ALLOWED_EXTENSIONS: return json_response({ 'error': _("Please choose a file with one of the following extensions: " "{}").format(", ".join(ALLOWED_EXTENSIONS)) }) meta = transient_file_store.write_file(handle, handle.name, domain) file_id = meta.identifier f = transient_file_store.get_tempfile_ref_for_contents(file_id) try: open_spreadsheet_download_ref(f) except SpreadsheetFileExtError: return json_response({ 'error': _("File does not appear to be an Excel file. Please choose another file.") }) app = get_app(domain, app_id) from corehq.apps.app_manager.views.media_utils import interpolate_media_path from corehq.apps.hqmedia.view_helpers import validate_multimedia_paths_rows, update_multimedia_paths # Get rows, filtering out header, no-ops, and any extra "Usages" columns rows = [] with get_spreadsheet(f) as spreadsheet: for row in list(spreadsheet.iter_rows())[1:]: if row[1]: rows.append(row[:2]) (errors, warnings) = validate_multimedia_paths_rows(app, rows) if len(errors): return json_response({ 'complete': 1, 'errors': errors, }) paths = { row[0]: interpolate_media_path(row[1]) for row in rows if row[1] } successes = update_multimedia_paths(app, paths) app.save() # Force all_media to reset app.all_media.reset_cache(app) app.all_media_paths.reset_cache(app) # Warn if any old paths remain in app (because they're used in a place this function doesn't know about) warnings = [] app.remove_unused_mappings() app_paths = {m.path: True for m in app.all_media()} for old_path, new_path in six.iteritems(paths): if old_path in app_paths: warnings.append(_("Could not completely update path <code>{}</code>, " "please check app for remaining references.").format(old_path)) return json_response({ 'complete': 1, 'successes': successes, 'warnings': warnings, })