Example #1
0
def get_current_app_doc(domain, app_id):
    from .models import Application
    app = Application.get_db().get(app_id)
    if app.get('domain', None) != domain:
        raise ResourceNotFound()
    return app
Example #2
0
def direct_ccz(request, domain):
    """
    You must specify an app_id, and you may specify either 'version' or 'latest'
    latest can be one of:
        release: Latest starred version
        build: Latest version regardless of star
        save: Latest saved version of the application (even without a build)
    If 'version' and 'latest' aren't specified it will default to latest save
    You may also set 'include_multimedia=true' if you need multimedia.
    """
    def error(msg, code=400):
        return json_response({
            'status': 'error',
            'message': msg
        },
                             status_code=code)

    def get_app(app_id, version, latest):
        if version:
            return get_build_doc_by_version(domain, app_id, version)
        elif latest == 'build':
            return get_latest_build_doc(domain, app_id)
        elif latest == 'release':
            return get_latest_released_app_doc(domain, app_id)
        else:
            # either latest=='save' or they didn't specify
            return get_current_app(domain, app_id)

    app_id = request.GET.get('app_id', None)
    version = request.GET.get('version', None)
    latest = request.GET.get('latest', None)
    include_multimedia = request.GET.get('include_multimedia',
                                         'false').lower() == 'true'

    # Make sure URL params make sense
    if not app_id:
        return error("You must specify `app_id` in your GET parameters")
    if version and latest:
        return error("You can't specify both 'version' and 'latest'")
    if latest not in (
            None,
            'release',
            'build',
            'save',
    ):
        return error("latest must be either 'release', 'build', or 'save'")
    if version:
        try:
            version = int(version)
        except ValueError:
            return error("'version' must be an integer")

    try:
        app = get_app(app_id, version, latest)
        if not app:
            raise ResourceNotFound()
        app = app if isinstance(app, Document) else wrap_app(app)
    except (ResourceNotFound, DocTypeError):
        return error("Application not found", code=404)

    if not app.copy_of:
        errors = app.validate_app()
    else:
        errors = None

    if errors:
        lang, langs = get_langs(request, app)
        template = get_app_manager_template(
            domain, 'app_manager/v1/partials/build_errors.html',
            'app_manager/v2/partials/build_errors.html')
        error_html = render_to_string(
            template, {
                'request': request,
                'app': app,
                'build_errors': errors,
                'domain': domain,
                'langs': langs,
                'lang': lang
            })
        return json_response(
            {'error_html': error_html},
            status_code=400,
        )

    app.set_media_versions(None)
    download = DownloadBase()
    errors = build_application_zip(
        include_multimedia_files=include_multimedia,
        include_index_files=True,
        app=app,
        download_id=download.download_id,
        compress_zip=True,
        filename='{}.ccz'.format(slugify(app.name)),
    )

    if errors is not None and errors['errors']:
        return json_response(
            errors,
            status_code=400,
        )
    return DownloadBase.get(download.download_id).toHttpResponse()