Ejemplo n.º 1
0
def app_xml(identifier):
    application = db.session.query(EmbedApplication).filter_by(
        identifier=identifier).first()
    if application is None:
        return render_template(
            "embed/error.xml",
            user=current_golab_user(),
            message=gettext("Application '{identifier}' not found").format(
                identifier=identifier)), 404

    apps_per_language = {}
    languages = ['en']
    for translation in application.translations:
        apps_per_language[translation.language] = {
            'url': translation.url,
            'full_url': translation.full_url,
        }
        languages.append(translation.language)

    author = application.owner.display_name

    response = make_response(
        render_template(
            "embed/app.xml",
            author=author,
            user=current_golab_user(),
            identifier=identifier,
            app=application,
            languages=languages,
            apps_per_language=apps_per_language,
            title=gettext("Application {name}").format(name=application.name)))
    response.content_type = 'application/xml'
    return response
Ejemplo n.º 2
0
def index():
    applications = db.session.query(EmbedApplication).filter_by(
        owner=current_golab_user()).order_by(
            EmbedApplication.last_update).all()
    return render_template("embed/index.html",
                           applications=applications,
                           user=current_golab_user())
Ejemplo n.º 3
0
def app_html(identifier):
    application = db.session.query(EmbedApplication).filter_by(
        identifier=identifier).first()
    if application is None:
        return render_template(
            "embed/error.html",
            user=current_golab_user(),
            message=gettext("Application '{identifier}' not found").format(
                identifier=identifier)), 404

    apps_per_language = {}
    languages = ['en']
    for translation in application.translations:
        apps_per_language[translation.language] = {
            'url': translation.url,
            'full_url': translation.full_url,
        }
        languages.append(translation.language)

    author = application.owner.display_name

    domain = urlparse.urlparse(application.url).netloc

    unsupported_url = db.session.query(HttpsUnsupportedUrl).filter_by(
        url=domain).first()
    # TODO: is this really useful? (unsupported_url)

    supports_https = application.url.startswith(
        'https://') or application.uses_proxy

    requires_https = False
    if (request.args.get('requires_https') or '').lower() in ['true', '1']:
        requires_https = True

    if request.environ.get('old_wsgi.url_scheme') == 'https':
        requires_https = True

    print(requires_https, supports_https)

    if requires_https and not supports_https:
        return render_template("embed/popup.html",
                               identifier=identifier,
                               app=application,
                               apps_per_language=apps_per_language,
                               name=application.name,
                               title=application.name)

    return render_template(
        "embed/app-embed.html",
        author=author,
        user=current_golab_user(),
        identifier=identifier,
        app=application,
        languages=languages,
        apps_per_language=apps_per_language,
        supports_https=supports_https,
        requires_https=requires_https,
        title=gettext("Application {name}").format(name=application.name))
Ejemplo n.º 4
0
def app(identifier):
    application = db.session.query(EmbedApplication).filter_by(
        identifier=identifier).first()
    if application is None:
        return render_template(
            "embed/error.html",
            message=gettext("Application '{identifier}' not found").format(
                identifier=identifier),
            user=current_golab_user()), 404

    return render_template(
        "embed/app.html",
        user=current_golab_user(),
        app=application,
        title=gettext("Application {name}").format(name=application.name))
Ejemplo n.º 5
0
def apps():
    applications = db.session.query(EmbedApplication).order_by(
        EmbedApplication.last_update).all()
    return render_template("embed/apps.html",
                           user=current_golab_user(),
                           applications=applications,
                           title=gettext("List of applications"))
Ejemplo n.º 6
0
def create():
    url = request.args.get('url') or ''
    
    sg_link = find_smartgateway_link(url, url)
    if sg_link:
        return redirect(sg_link)

    existing_embed_app = db.session.query(EmbedApplication).filter_by(owner=current_golab_user(), url=url).first()
    if existing_embed_app is None:
        return redirect(url_for('embed.create', url=url))
    
    return redirect(url_for('embed.edit', identifier=existing_embed_app.identifier, url=url))
Ejemplo n.º 7
0
def app_html(identifier):
    application = db.session.query(EmbedApplication).filter_by(identifier = identifier).first()
    if application is None:
        return render_template("embed/error.html", user = current_golab_user(), message = gettext("Application '{identifier}' not found").format(identifier=identifier)), 404

    apps_per_language = {}
    languages = ['en']
    for translation in application.translations:
        apps_per_language[translation.language] = {
            'url': translation.url,
            'full_url': translation.full_url,
        }
        languages.append(translation.language)

    author = application.owner.display_name

    domain = urlparse.urlparse(application.url).netloc

    unsupported_url = db.session.query(HttpsUnsupportedUrl).filter_by(url=domain).first()
    # TODO: is this really useful? (unsupported_url)

    supports_https = application.url.startswith('https://') or application.uses_proxy

    requires_https = False
    if (request.args.get('requires_https') or '').lower() in ['true', '1']:
        requires_https = True

    if request.environ.get('old_wsgi.url_scheme') == 'https':
        requires_https = True

    print(requires_https, supports_https)

    if requires_https and not supports_https:
        return render_template("embed/popup.html", identifier=identifier, app=application, apps_per_language=apps_per_language, name=application.name, title=application.name)

    return render_template("embed/app-embed.html", author = author, user = current_golab_user(), 
                                                   identifier=identifier, app = application, languages=languages, 
                                                   apps_per_language = apps_per_language, supports_https=supports_https, 
                                                   requires_https = requires_https,
                                                   title = gettext("Application {name}").format(name=application.name))
Ejemplo n.º 8
0
def app_xml(identifier):
    application = db.session.query(EmbedApplication).filter_by(identifier = identifier).first()
    if application is None:
        return render_template("embed/error.xml", user = current_golab_user(), message = gettext("Application '{identifier}' not found").format(identifier=identifier)), 404

    apps_per_language = {}
    languages = ['en']
    for translation in application.translations:
        apps_per_language[translation.language] = translation.url
        languages.append(translation.language)

    author = application.owner.display_name

    response = make_response(render_template("embed/app.xml", author = author, user = current_golab_user(), identifier=identifier, app = application, languages=languages, apps_per_language = apps_per_language, title = gettext("Application {name}").format(name=application.name)))
    response.content_type = 'application/xml'
    return response
Ejemplo n.º 9
0
def _return_lab(db_rlms, lab, identifier_links, langs, public_rlms):
    form = SimplifiedApplicationForm()
    form.name.data = lab.name or ''
    form.description.data = lab.description or ''
    # If available, override whatever
    if lab.domains:
        form.domains_text.data = ', '.join(lab.domains or [])
    # If available, override whatever
    if lab.age_ranges:
        form.age_ranges_range.data = EmbedApplication.age_ranges2text(lab.age_ranges or [])
    
    # TODO: does this still make sense?
    if form.validate_on_submit():
        if public_rlms:
            single_lab = None
        else:
            single_lab = lab.laboratory_id
        
        lab_unique_id = create_lab_id(db_rlms, lab.laboratory_id, single = single_lab is not None)
        all_labs = extract_labs(db_rlms, single_lab, fmt='json', 
                        age_ranges=EmbedApplication.text2age_ranges(form.age_ranges_range.data), 
                        domains=form.domains_text.data.split(', '))
        formatted_labs = [ cur_lab for cur_lab in all_labs if cur_lab['id'] == lab_unique_id ]
        if len(formatted_labs) == 0:
            return "Invalid lab identifier"

        # return _post_contents(formatted_labs[0])
        # TODO

    languages = list_of_languages()
    new_langs = []
    for lang in langs:
        lang_name = languages.get(lang)
        if lang_name:
            new_langs.append(lang_name)

    bookmarklet_from = request.args.get('url')

    return render_template("embed/create.html", user = current_golab_user(), form=form, identifier_links=identifier_links, header_message=gettext("View resource"), languages=[], existing_languages=[], all_languages=[], disabled=True, langs = sorted(new_langs), bookmarklet_from=bookmarklet_from, domains_provided=lab.domains is not None, age_ranges_provided=lab.age_ranges is not None)
Ejemplo n.º 10
0
def edit(identifier):
    existing_languages = {
        # lang: {
        #     'code': 'es',
        #     'name': 'Spanish',
        #     'url': 'http://....'
        # }
    }
    existing_languages_db = {
        # lang: db_instance
    }
    all_languages = list_of_languages()

    # Obtain from the database
    application = db.session.query(EmbedApplication).filter_by(
        identifier=identifier).first()
    if application is None:
        return "Application does not exist", 404

    for translation in application.translations:
        existing_languages_db[translation.language] = translation
        existing_languages[translation.language] = {
            'code': translation.language,
            'name': all_languages.get(translation.language)
            or 'Language not supported anymore',
            'url': translation.url
        }

    # languages added by the UI
    posted_languages = {
        # 'es' : 'http://.../'
    }

    if request.method == 'POST':
        for key in request.form:
            if key.startswith('language.'):
                lang_code = key[len('language.'):]
                if lang_code in all_languages:
                    posted_languages[lang_code] = request.form[key]

    form = ApplicationForm(obj=application)
    if form.validate_on_submit():
        # Check for new ones or changed
        for posted_language, url in posted_languages.items():
            if posted_language in existing_languages_db:
                translation = existing_languages_db[posted_language]
                if translation.url != url:  # Don't trigger unnecessary UPDATEs
                    translation.url = url
            else:
                translation = EmbedApplicationTranslation(
                    embed_application=application,
                    url=url,
                    language=posted_language)
                db.session.add(translation)

        # Delete old ones
        for existing_language, translation in existing_languages_db.items():
            if existing_language not in posted_languages:
                existing_languages.pop(existing_language)
                db.session.delete(translation)

        form_scale = _get_scale_value(form)
        application.update(url=form.url.data,
                           name=form.name.data,
                           height=form.height.data,
                           scale=form_scale,
                           age_ranges_range=form.age_ranges_range.data,
                           description=form.description.data,
                           domains_text=form.domains_text.data)
        db.session.commit()

        # TODO: does this still make sense?
        # if request.form.get('action') == 'publish':
        #     return _post_contents(app_to_json(application), application.url)

    # Add the posted languages to the existing ones
    for lang_code, url in posted_languages.items():
        existing_languages[lang_code] = {
            'code': lang_code,
            'name': all_languages[lang_code],
            'url': url
        }

    # Obtain the languages formatted as required but excluding those already added
    languages = obtain_formatted_languages(existing_languages)
    bookmarklet_from = request.args.get('url')
    return render_template("embed/create.html",
                           user=current_golab_user(),
                           form=form,
                           identifier=identifier,
                           header_message=gettext("Edit web"),
                           languages=languages,
                           existing_languages=list(
                               existing_languages.values()),
                           all_languages=all_languages,
                           bookmarklet_from=bookmarklet_from,
                           edit=True,
                           create=False)
Ejemplo n.º 11
0
def app(identifier):
    application = db.session.query(EmbedApplication).filter_by(identifier = identifier).first()
    if application is None:
        return render_template("embed/error.html", message = gettext("Application '{identifier}' not found").format(identifier=identifier), user = current_golab_user()), 404

    return render_template("embed/app.html", user = current_golab_user(), app = application, title = gettext("Application {name}").format(name=application.name))
Ejemplo n.º 12
0
def index():
    applications = db.session.query(EmbedApplication).filter_by(owner = current_golab_user()).order_by(EmbedApplication.last_update).all()
    return render_template("embed/index.html", applications = applications, user = current_golab_user())
Ejemplo n.º 13
0
def create():
    check_certificates()

    original_url = request.args.get('url')
    if original_url:
        bookmarklet_from = original_url
    else:
        bookmarklet_from = None

    original_application = None
    if original_url:
        applications = db.session.query(EmbedApplication).filter_by(
            url=original_url).all()
        if applications:
            original_application = applications[0]
            for app in applications:
                if len(app.translations) > len(
                        original_application.translations):
                    original_application = app
                if app.name and not original_application.name:
                    original_application = app
                    continue
                if app.description and not original_application.description:
                    original_application = app
                    continue

    if original_application is not None:
        form = ApplicationForm(obj=original_application)
    else:
        form = ApplicationForm()

    if not form.url.data and original_url:
        form.url.data = original_url
        if not form.name.data:
            result = get_url_metadata(original_url, timeout=5)
            if result['name']:
                form.name.data = result['name']
            if result['description'] and not form.description.data:
                form.description.data = result['description']

    if form.url.data:
        form.url.data = form.url.data.strip()

    if form.validate_on_submit():
        form_scale = _get_scale_value(form)
        application = EmbedApplication(
            url=form.url.data,
            name=form.name.data,
            owner=current_golab_user(),
            height=form.height.data,
            scale=form_scale,
            description=form.description.data,
            age_ranges_range=form.age_ranges_range.data)
        application.domains_text = form.domains_text.data
        db.session.add(application)
        try:
            db.session.commit()
        except Exception as e:
            traceback.print_exc()
            return render_template(
                "embed/error.html",
                message=gettext("There was an error creating an application"),
                user=current_golab_user()), 500
        else:
            kwargs = {}
            if bookmarklet_from:
                kwargs['url'] = bookmarklet_from
            return redirect(
                url_for('.edit', identifier=application.identifier, **kwargs))

    return render_template("embed/create.html",
                           form=form,
                           header_message=gettext("Add a web"),
                           user=current_golab_user(),
                           bookmarklet_from=bookmarklet_from,
                           create=True,
                           edit=False)
Ejemplo n.º 14
0
def inject_variables():
    return dict(current_golab_user=current_golab_user())
Ejemplo n.º 15
0
def inject_variables():
    return dict(current_golab_user=current_golab_user())
Ejemplo n.º 16
0
def create():
    check_certificates()

    original_url = request.args.get('url')
    if original_url:
        bookmarklet_from = original_url
    else:
        bookmarklet_from = None

    original_application = None
    if original_url:
        applications = db.session.query(EmbedApplication).filter_by(url=original_url).all()
        if applications:
            original_application = applications[0]
            for app in applications:
                if len(app.translations) > len(original_application.translations):
                    original_application = app
                if app.name and not original_application.name:
                    original_application = app
                    continue
                if app.description and not original_application.description:
                    original_application = app
                    continue

    if original_application is not None:
        form = ApplicationForm(obj=original_application)
    else:
        form = ApplicationForm()

    if not form.url.data and original_url:
        form.url.data = original_url
        if not form.name.data:
            result = get_url_metadata(original_url, timeout = 5)
            if result['name']:
                form.name.data = result['name']
            if result['description'] and not form.description.data:
                form.description.data = result['description']

    if form.url.data:
        form.url.data = form.url.data.strip()

    if form.validate_on_submit():
        form_scale = _get_scale_value(form)
        application = EmbedApplication(url = form.url.data, name = form.name.data, owner = current_golab_user(), height=form.height.data, scale=form_scale, description=form.description.data, age_ranges_range = form.age_ranges_range.data)
        application.domains_text = form.domains_text.data
        db.session.add(application)
        try:
            db.session.commit()
        except Exception as e:
            traceback.print_exc()
            return render_template("embed/error.html", message = gettext("There was an error creating an application"), user = current_golab_user()), 500
        else:
            kwargs = {}
            if bookmarklet_from:
                kwargs['url'] = bookmarklet_from
            return redirect(url_for('.edit', identifier=application.identifier, **kwargs))
            
    return render_template("embed/create.html", form=form, header_message=gettext("Add a web"), user = current_golab_user(), bookmarklet_from=bookmarklet_from, create=True, edit=False)
Ejemplo n.º 17
0
def apps():
    applications = db.session.query(EmbedApplication).order_by(EmbedApplication.last_update).all()
    return render_template("embed/apps.html", user = current_golab_user(), applications = applications, title = gettext("List of applications"))
Ejemplo n.º 18
0
def edit(identifier):
    existing_languages = {
        # lang: {
        #     'code': 'es',
        #     'name': 'Spanish',
        #     'url': 'http://....'
        # }
    }
    existing_languages_db = {
        # lang: db_instance
    }
    all_languages = list_of_languages()
    
    # Obtain from the database
    application = db.session.query(EmbedApplication).filter_by(identifier = identifier).first()
    if application is None:
        return "Application does not exist", 404

    for translation in application.translations:
        existing_languages_db[translation.language] = translation
        existing_languages[translation.language] = {
            'code': translation.language,
            'name': all_languages.get(translation.language) or 'Language not supported anymore',
            'url': translation.url
        }
    
    # languages added by the UI
    posted_languages = {
        # 'es' : 'http://.../'
    }

    if request.method == 'POST':
        for key in request.form:
            if key.startswith('language.'):
                lang_code = key[len('language.'):]
                if lang_code in all_languages:
                    posted_languages[lang_code] = request.form[key]
                

    form = ApplicationForm(obj=application)
    if form.validate_on_submit():
        # Check for new ones or changed
        for posted_language, url in posted_languages.items():
            if posted_language in existing_languages_db:
                translation = existing_languages_db[posted_language]
                if translation.url != url: # Don't trigger unnecessary UPDATEs
                    translation.url = url
            else:
                translation = EmbedApplicationTranslation(embed_application = application, url=url, language=posted_language)
                db.session.add(translation)

        # Delete old ones
        for existing_language, translation in existing_languages_db.items():
            if existing_language not in posted_languages:
                existing_languages.pop(existing_language)
                db.session.delete(translation)

        form_scale = _get_scale_value(form)
        application.update(url=form.url.data, name=form.name.data, height=form.height.data, scale=form_scale, age_ranges_range=form.age_ranges_range.data, description=form.description.data, domains_text=form.domains_text.data)
        db.session.commit()
    
        # TODO: does this still make sense?
        # if request.form.get('action') == 'publish':
        #     return _post_contents(app_to_json(application), application.url)

    # Add the posted languages to the existing ones
    for lang_code, url in posted_languages.items():
        existing_languages[lang_code] = {
            'code' : lang_code,
            'name' : all_languages[lang_code],
            'url' : url
        }

    # Obtain the languages formatted as required but excluding those already added
    languages = obtain_formatted_languages(existing_languages)
    bookmarklet_from = request.args.get('url')
    return render_template("embed/create.html", user = current_golab_user(), form=form, identifier=identifier, header_message=gettext("Edit web"), languages=languages, existing_languages=list(existing_languages.values()), all_languages=all_languages, bookmarklet_from=bookmarklet_from, edit=True, create=False)