def _inner(request, link_domain, data, master_domain=domain): clear_app_cache(request, link_domain) if data['toggles']: for slug in data['toggles'].split(","): set_toggle(slug, link_domain, True, namespace=toggles.NAMESPACE_DOMAIN) linked = data.get('linked') if linked: for module in app.modules: if isinstance(module, ReportModule): messages.error(request, _('This linked application uses mobile UCRs which ' 'are currently not supported. For this application to ' 'function correctly, you will need to remove those modules.')) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) master_version = get_latest_released_app_version(app.domain, app_id) if not master_version: messages.error(request, _("Creating linked app failed." " Unable to get latest released version of your app." " Make sure you have at least one released build.")) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) linked_app = create_linked_app(master_domain, app_id, link_domain, data['name']) try: update_linked_app(linked_app, request.couch_user.get_id) except AppLinkError as e: messages.error(request, str(e)) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) messages.success(request, _('Application successfully copied and linked.')) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[link_domain, linked_app.get_id])) else: extra_properties = {'name': data['name']} app_copy = import_app_util(app_id_or_source, link_domain, extra_properties) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
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 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 app_exchange(request, domain): template = "app_manager/app_exchange.html" records = [] for obj in ExchangeApplication.objects.all(): app = get_app(obj.domain, obj.app_id) records.append({ "id": app.get_id, "domain": app.domain, "name": app.name, "comment": app.comment, "last_released": obj.last_released.date() if obj.last_released else None, "help_link": obj.help_link, }) context = { "domain": domain, "records": records, } if request.method == "POST": clear_app_cache(request, domain) from_domain = request.POST.get('from_domain') from_app_id = request.POST.get('from_app_id') doc = get_latest_released_app_doc(from_domain, from_app_id) if not doc: messages.error(request, _("Could not find latest released version of app.")) return render(request, template, context) app_copy = import_app_util(doc, domain) return back_to_main(request, domain, app_id=app_copy._id) return render(request, template, context)
def import_app(request, domain): template = "app_manager/import_app.html" if request.method == "POST": clear_app_cache(request, domain) name = request.POST.get('name') compressed = request.POST.get('compressed') valid_request = True if not name: messages.error( request, _("You must submit a name for the application you are importing." )) valid_request = False if not compressed: messages.error(request, _("You must submit the source data.")) valid_request = False if not valid_request: return render(request, template, {'domain': domain}) source = decompress([ six.unichr(int(x)) if int(x) < 256 else int(x) for x in compressed.split(',') ]) source = json.loads(source) assert (source is not None) app = import_app_util(source, domain, {'name': name}) return back_to_main(request, domain, app_id=app._id) else: app_id = request.GET.get('app') redirect_domain = request.GET.get('domain') or None if redirect_domain is not None: redirect_domain = redirect_domain.lower() if Domain.get_by_name(redirect_domain): return HttpResponseRedirect( reverse('import_app', args=[redirect_domain]) + "?app={app_id}".format(app_id=app_id)) else: if redirect_domain: messages.error( request, "We can't find a project called \"%s\"." % redirect_domain) else: messages.error(request, "You left the project name blank.") return HttpResponseRedirect( request.META.get('HTTP_REFERER', request.path)) if app_id: app = get_app(None, app_id) assert (app.get_doc_type() in ('Application', 'RemoteApp')) assert (request.couch_user.is_member_of(app.domain)) else: app = None return render(request, template, { 'domain': domain, 'app': app, })
def _inner(request, domain, data): clear_app_cache(request, domain) if data["toggles"]: for slug in data["toggles"].split(","): set_toggle(slug, domain, True, namespace=toggles.NAMESPACE_DOMAIN) app_copy = import_app_util(app_id_or_source, domain, {"name": data["name"]}) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _inner(request, link_domain, data, master_domain=domain): clear_app_cache(request, link_domain) if data['toggles']: for slug in data['toggles'].split(","): set_toggle(slug, link_domain, True, namespace=toggles.NAMESPACE_DOMAIN) linked = data.get('linked') if linked: master_version = get_latest_released_app_version(app.domain, app_id) if not master_version: messages.error(request, _("Creating linked app failed." " Unable to get latest released version of your app." " Make sure you have at least one released build.")) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) linked_app = create_linked_app(master_domain, app_id, link_domain, data['name']) try: update_linked_app(linked_app, request.couch_user.get_id) except AppLinkError as e: linked_app.delete() messages.error(request, str(e)) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) messages.success(request, _('Application successfully copied and linked.')) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[link_domain, linked_app.get_id])) else: extra_properties = {'name': data['name']} try: app_copy = import_app_util(app_id_or_source, link_domain, extra_properties) except ReportConfigurationNotFoundError: messages.error(request, _("Copying the application failed because " "your application contains a Report Module " "that references a static UCR configuration.")) return HttpResponseRedirect(reverse_util('app_settings', params={}, args=[domain, app_id])) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
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 _inner(request, domain, data): clear_app_cache(request, domain) if data['toggles']: for slug in data['toggles'].split(","): set_toggle(slug, domain, True, namespace=toggles.NAMESPACE_DOMAIN) extra_properties = {'name': data['name']} if data.get('linked'): extra_properties['master'] = app_id extra_properties['doc_type'] = 'LinkedApplication' app = get_app(None, app_id) if domain not in app.linked_whitelist: app.linked_whitelist.append(domain) app.save() app_copy = import_app_util(app_id_or_source, domain, extra_properties) mobile_ucrs = False for module in app_copy.modules: if isinstance(module, ReportModule): mobile_ucrs = True break if mobile_ucrs: messages.error( request, _('This linked application uses mobile UCRs ' 'which are currently not supported. For this application ' 'to function correctly, you will need to remove those modules.' )) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _inner(request, link_domain, data, master_domain=domain): clear_app_cache(request, link_domain) if data['toggles']: for slug in data['toggles'].split(","): set_toggle(slug, link_domain, True, namespace=toggles.NAMESPACE_DOMAIN) linked = data.get('linked') if linked: master_version = get_latest_released_app_version( app.domain, app_id) if not master_version: messages.error( request, _("Creating linked app failed." " Unable to get latest released version of your app." " Make sure you have at least one released build.")) return HttpResponseRedirect( reverse_util('app_settings', params={}, args=[domain, app_id])) linked_app = create_linked_app(master_domain, app_id, link_domain, data['name']) try: update_linked_app(linked_app, request.couch_user.get_id) except AppLinkError as e: linked_app.delete() messages.error(request, str(e)) return HttpResponseRedirect( reverse_util('app_settings', params={}, args=[domain, app_id])) messages.success( request, _('Application successfully copied and linked.')) return HttpResponseRedirect( reverse_util('app_settings', params={}, args=[link_domain, linked_app.get_id])) else: extra_properties = {'name': data['name']} try: app_copy = import_app_util(app_id_or_source, link_domain, extra_properties) except ReportConfigurationNotFoundError: messages.error( request, _("Copying the application failed because " "your application contains a Report Module " "that references a static UCR configuration.")) return HttpResponseRedirect( reverse_util('app_settings', params={}, args=[domain, app_id])) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _copy_app_helper(request, from_app_id, to_domain, to_app_name): extra_properties = {'name': to_app_name} app_copy = import_app_util(from_app_id, to_domain, extra_properties, request) if is_linked_app(app_copy): app_copy = app_copy.convert_to_application() app_copy.save() return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _inner(request, domain, data): clear_app_cache(request, domain) if data['toggles']: for slug in data['toggles'].split(","): set_toggle(slug, domain, True, namespace=toggles.NAMESPACE_DOMAIN) app_copy = import_app_util(app_id_or_source, domain, {'name': data['name']}) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def import_app(request, domain, template="app_manager/import_app.html"): if request.method == "POST": clear_app_cache(request, domain) name = request.POST.get('name') compressed = request.POST.get('compressed') valid_request = True if not name: messages.error(request, _("You must submit a name for the application you are importing.")) valid_request = False if not compressed: messages.error(request, _("You must submit the source data.")) valid_request = False if not valid_request: return render(request, template, {'domain': domain}) source = decompress([chr(int(x)) if int(x) < 256 else int(x) for x in compressed.split(',')]) source = json.loads(source) assert(source is not None) app = import_app_util(source, domain, {'name': name}) return back_to_main(request, domain, app_id=app._id) else: app_id = request.GET.get('app') redirect_domain = request.GET.get('domain') or None if redirect_domain is not None: redirect_domain = redirect_domain.lower() if Domain.get_by_name(redirect_domain): return HttpResponseRedirect( reverse('import_app', args=[redirect_domain]) + "?app={app_id}".format(app_id=app_id) ) else: if redirect_domain: messages.error(request, "We can't find a project called %s." % redirect_domain) else: messages.error(request, "You left the project name blank.") return HttpResponseRedirect(request.META.get('HTTP_REFERER', request.path)) if app_id: app = get_app(None, app_id) assert(app.get_doc_type() in ('Application', 'RemoteApp')) assert(request.couch_user.is_member_of(app.domain)) else: app = None return render(request, template, { 'domain': domain, 'app': app, 'is_superuser': request.couch_user.is_superuser })
def app_exchange(request, domain): template = "app_manager/app_exchange.html" records = [] for obj in ExchangeApplication.objects.all(): results = get_all_built_app_results(obj.domain, app_id=obj.app_id) results = [r['value'] for r in results if r['value']['is_released']] if not results: continue results.reverse() first = results[0] def _version_text(result): if result['_id'] == first['_id']: return _("Latest Version") built_on = iso_string_to_datetime( result['built_on']).strftime("%B %d, %Y") return _("{} version").format(built_on) records.append({ "id": first['_id'], "name": first['name'], "help_link": obj.help_link, "changelog_link": obj.changelog_link, "last_released": iso_string_to_datetime(first['built_on']).date(), "versions": [{ "id": r['_id'], "text": _version_text(r), } for r in results], }) context = { "domain": domain, "records": records, } if request.method == "POST": clear_app_cache(request, domain) from_app_id = request.POST.get('from_app_id') app_copy = import_app_util(from_app_id, domain, { 'created_from_template': from_app_id, }) return back_to_main(request, domain, app_id=app_copy._id) return render(request, template, context)
def app_from_template(request, domain, slug): meta = get_meta(request) track_app_from_template_on_hubspot.delay(request.couch_user, request.COOKIES, meta) clear_app_cache(request, domain) template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) module_id = 0 form_id = 0 try: app.get_module(module_id).get_form(form_id) except (ModuleNotFoundException, FormNotFoundException): return HttpResponseRedirect(reverse('view_app', args=[domain, app._id])) return HttpResponseRedirect(reverse('form_source', args=[domain, app._id, module_id, form_id]))
def app_from_template(request, domain, slug): meta = get_meta(request) track_app_from_template_on_hubspot.delay(request.couch_user, request.COOKIES, meta) clear_app_cache(request, domain) template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) module_id = 0 form_id = 0 try: app.get_module(module_id).get_form(form_id) except (ModuleNotFoundException, FormNotFoundException): return HttpResponseRedirect(reverse('view_app', args=[domain, app._id])) return HttpResponseRedirect(reverse('view_form_legacy', args=[domain, app._id, module_id, form_id]))
def app_from_template(request, domain, slug): meta = get_meta(request) track_app_from_template_on_hubspot.delay(request.couch_user, request.COOKIES, meta) if tours.NEW_APP.is_enabled(request.user): identify.delay(request.couch_user.username, {"First Template App Chosen": "%s" % slug}) clear_app_cache(request, domain) template = load_app_template(slug) app = import_app_util(template, domain, {"created_from_template": "%s" % slug}) module_id = 0 form_id = 0 try: app.get_module(module_id).get_form(form_id) except (ModuleNotFoundException, FormNotFoundException): return HttpResponseRedirect(reverse("view_app", args=[domain, app._id])) return HttpResponseRedirect(reverse("view_form", args=[domain, app._id, module_id, form_id]))
def _copy_app_helper(request, master_domain, master_app_id_or_source, copy_to_domain, copy_to_app_name, app_id): extra_properties = {'name': copy_to_app_name} try: app_copy = import_app_util(master_app_id_or_source, copy_to_domain, extra_properties) except ReportConfigurationNotFoundError: messages.error( request, _("Copying the application failed because " "your application contains a Report Module " "that references a static UCR configuration.")) return HttpResponseRedirect( reverse_util('app_settings', params={}, args=[master_domain, app_id])) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def app_from_template(request, domain, slug): meta = get_meta(request) track_app_from_template_on_hubspot.delay(request.couch_user, request.COOKIES, meta) if tours.NEW_APP.is_enabled(request.user): identify.delay(request.couch_user.username, {'First Template App Chosen': '%s' % slug}) clear_app_cache(request, domain) template = load_app_template(slug) app = import_app_util(template, domain, { 'created_from_template': '%s' % slug, }) module_id = 0 form_id = 0 try: app.get_module(module_id).get_form(form_id) except (ModuleNotFoundException, FormNotFoundException): return HttpResponseRedirect(reverse('view_app', args=[domain, app._id])) return HttpResponseRedirect(reverse('view_form', args=[domain, app._id, module_id, form_id]))
def import_app(request, domain): template = "app_manager/import_app.html" if request.method == "POST": clear_app_cache(request, domain) name = request.POST.get('name') file = request.FILES.get('source_file') valid_request = True if not name: messages.error( request, _("You must submit a name for the application you are importing." )) valid_request = False if not file: messages.error(request, _("You must upload the app source file.")) valid_request = False try: if valid_request: source = json.load(file) except json.decoder.JSONDecodeError: messages.error(request, _("The file uploaded is an invalid JSON file")) valid_request = False if not valid_request: return render(request, template, {'domain': domain}) assert (source is not None) app = import_app_util(source, domain, {'name': name}, request=request) return back_to_main(request, domain, app_id=app._id) else: app_id = request.GET.get('app') redirect_domain = request.GET.get('domain') or None if redirect_domain is not None: redirect_domain = redirect_domain.lower() if Domain.get_by_name(redirect_domain): return HttpResponseRedirect( reverse('import_app', args=[redirect_domain]) + "?app={app_id}".format(app_id=app_id)) else: if redirect_domain: messages.error( request, "We can't find a project called \"%s\"." % redirect_domain) else: messages.error(request, "You left the project name blank.") return HttpResponseRedirect( request.META.get('HTTP_REFERER', request.path)) if app_id: app = get_app(None, app_id) assert (app.get_doc_type() in ('Application', 'RemoteApp')) assert (request.couch_user.is_member_of(app.domain)) else: app = None return render(request, template, { 'domain': domain, 'app': app, })
def copy_app_check_domain(request, domain, name, app_id_or_source): app_copy = import_app_util(app_id_or_source, domain, {'name': name}) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _copy_app_helper(request, master_domain, master_app_id_or_source, copy_to_domain, copy_to_app_name, app_id): extra_properties = {'name': copy_to_app_name} app_copy = import_app_util(master_app_id_or_source, copy_to_domain, extra_properties, request) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def copy_app_check_domain(request, domain, name, app_id): app_copy = import_app_util(app_id, domain, {'name': name}) return back_to_main(request, app_copy.domain, app_id=app_copy._id)
def _copy_app_helper(request, from_app_id, to_domain, to_app_name): extra_properties = {'name': to_app_name} app_copy = import_app_util(from_app_id, to_domain, extra_properties, request) return back_to_main(request, app_copy.domain, app_id=app_copy._id)