def load_app_from_slug(domain, username, slug): # Import app itself template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) # Fetch multimedia, which is hosted elsewhere multimedia_filename = os.path.join(app_template_dir(slug), 'multimedia.json') if (os.path.exists(multimedia_filename)): with open(multimedia_filename) as f: path_url_map = json.load(f) http = urllib3.PoolManager() for path, url in path_url_map.items(): try: req = http.request('GET', url) except Exception: # If anything goes wrong, just bail. It's not a big deal if a template app is missing a file. continue if req.status == 200: data = req.data media_class = CommCareMultimedia.get_class_by_data(data) if media_class: multimedia = media_class.get_by_data(data) multimedia.attach_data(data, original_filename=os.path.basename(path), username=username) multimedia.add_domain(domain, owner=True) app.create_mapping(multimedia, MULTIMEDIA_PREFIX + path) return _build_sample_app(app)
def app_from_template(request, domain, slug): send_hubspot_form(HUBSPOT_APP_TEMPLATE_FORM_ID, request) clear_app_cache(request, domain) template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) for path in get_template_app_multimedia_paths(slug): media_class = None with open(os.path.join(app_template_dir(slug), path), "rb") as f: f.seek(0) data = f.read() media_class = CommCareMultimedia.get_class_by_data(data) if media_class: multimedia = media_class.get_by_data(data) multimedia.attach_data( data, original_filename=os.path.basename(path), username=request.user.username) multimedia.add_domain(domain, owner=True) app.create_mapping(multimedia, MULTIMEDIA_PREFIX + path) comment = _("A sample application you can try out in Web Apps") build = make_async_build(app, request.user.username, release=True, comment=comment) cloudcare_state = '{{"appId":"{}"}}'.format(build._id) return HttpResponseRedirect( reverse(FormplayerMain.urlname, args=[domain]) + '#' + cloudcare_state)
def load_app_from_slug(domain, username, slug): # Import app itself template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) # Fetch multimedia, which is hosted elsewhere multimedia_filename = os.path.join(app_template_dir(slug), 'multimedia.json') if (os.path.exists(multimedia_filename)): with open(multimedia_filename) as f: path_url_map = json.load(f) http = urllib3.PoolManager() for path, url in path_url_map.items(): try: req = http.request('GET', url) except Exception: # If anything goes wrong, just bail. It's not a big deal if a template app is missing a file. continue if req.status == 200: data = req.data media_class = CommCareMultimedia.get_class_by_data(data) if media_class: multimedia = media_class.get_by_data(data) multimedia.attach_data(data, original_filename=os.path.basename(path), username=username) multimedia.add_domain(domain, owner=True) app.create_mapping(multimedia, MULTIMEDIA_PREFIX + path) comment = _("A sample application you can try out in Web Apps") build = make_async_build(app, username, allow_prune=False, comment=comment) build.is_released = True build.save(increment_version=False) return build
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: 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 list(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) app.create_mapping(multimedia, form_path) media_info = multimedia.get_media_info(form_path, is_updated=is_updated, original_path=path) status.add_matched_path(media_class, media_info) 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): """ 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: 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 list(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) app.create_mapping(multimedia, form_path) media_info = multimedia.get_media_info(form_path, is_updated=is_updated, original_path=path) status.add_matched_path(media_class, media_info) 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, replace_existing=False): """ 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() try: saved_file = StringIO.StringIO() saved_ref = DownloadBase.get(processing_id) data = saved_ref.get_content() saved_file.write(data) except Exception as e: status.mark_with_error(_("Could not fetch cached bulk upload file. Error: %s." % e)) return try: saved_file.seek(0) uploaded_zip = zipfile.ZipFile(saved_file) except Exception as e: status.mark_with_error(_("Error opening file as zip file: %s" % e)) return if uploaded_zip.testzip(): status.mark_with_error(_("Error encountered processing Zip File. File doesn't look valid.")) return zipped_files = uploaded_zip.namelist() status.total_files = len(zipped_files) checked_paths = [] try: 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) if not media_class: # skip these... continue app_paths = app.get_all_paths_of_type(media_class.__name__) form_path = media_class.get_form_path(path) if not form_path in app_paths: status.add_unmatched_path(path, _("Did not match any %s paths in application." % media_class.get_nice_name())) continue 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_updated = multimedia.attach_data(data, original_filename=file_name, username=username, replace_attachment=replace_existing) 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: multimedia.add_domain(domain, owner=True) if share_media: multimedia.update_or_add_license(domain, type=license_name, author=author, attribution_notes=attribution_notes) app.create_mapping(multimedia, form_path) media_info = multimedia.get_media_info(form_path, is_updated=is_updated, original_path=path) status.add_matched_path(media_class, media_info) 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()