コード例 #1
0
ファイル: apps.py プロジェクト: homck007/commcare-hq
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)
コード例 #2
0
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)
コード例 #3
0
ファイル: apps.py プロジェクト: dimagi/commcare-hq
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
コード例 #4
0
ファイル: apps.py プロジェクト: dimagi/commcare-hq
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]))
コード例 #5
0
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]))
コード例 #6
0
ファイル: apps.py プロジェクト: nnestle/commcare-hq
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]))
コード例 #7
0
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]))
コード例 #8
0
    def migrate_app(self, app_doc):
        should_save = False

        template_slug = app_doc['created_from_template']
        template = load_app_template(template_slug)

        if _get_first_form_id(app_doc) == _get_first_form_id(template):
            should_save = True
            app = Application.wrap(app_doc)

            _attachments = {}
            for name in app_doc.get('_attachments', {}):
                if re.match(ATTACHMENT_REGEX, name):
                    _attachments[name] = app.fetch_attachment(name)
            app_doc['_attachments'] = _attachments

            app_doc = update_unique_ids(app_doc)

        return Application.wrap(app_doc) if should_save else None
コード例 #9
0
 def setUp(self):
     self.app = Application.wrap(
         load_app_template("wash_before_cond_case_update"))