Esempio n. 1
0
def add_link():
    """Adds a new link for the current user."""
    form = LinkForm(request.form,
                    banlist=[strip_protocol(app.config["LINKSERVER_URL"])])
    client = get_db_client(app, g)

    if request.method == "POST":
        # Validate the form
        form.long_url.data = ensure_protocol(form.long_url.data)
        if form.validate():
            # TODO Decide whether we want to do something with the response
            kwargs = form.to_json()
            try:
                client.create_short_url(
                    netid=current_user.netid,
                    **kwargs
                )
                return redirect("/")
            except Exception as e:
                return render_template("add.html",
                                       errors=["{}".format(e)],
                                       netid=current_user.netid,
                                       admin=current_user.is_admin())

        else:
            # WTForms detects a form validation error
            return render_template("add.html",
                                   errors=form.errors,
                                   netid=current_user.netid,
                                   admin=current_user.is_admin())
    else:
        # GET request
        return render_template("add.html",
                               netid=current_user.netid,
                               admin=current_user.is_admin())
Esempio n. 2
0
def edit_link():
    """Edits a link.

    On POST, this route expects a form that contains the unique short URL that
    will be edited.
    """
    client = get_db_client(app, g)
    form = LinkForm(request.form)

    if request.method == "POST":
        # Validate form before continuing
        if form.long_url.data.startswith(app.config['LINKSERVER_URL']) or form.long_url.data.startswith(app.config['LINKSERVER_URL'][:7]):
            return render_template("add.html",
                                    errors=["Blocked Link"],
                                    admin=current_user.is_admin()
            )
        if form.validate():
            # Success - make the edits in the database
            kwargs = form.to_json()
            response = client.modify_url(
                request.form["short_url"],
                **kwargs
            )
            return redirect("/")
        else:
            form.long_url.data = "http://" + form.long_url.data
            # TODO Can we do anything with the database response?
            if form.validate():
                kwargs = form.to_json()
                response = client.modify_url(
                    request.form["short_url"],
                    **kwargs
                )
                return redirect("/")
            else:
                # Validation error
                short_url = request.form["short_url"]
                info = client.get_url_info(short_url)
                long_url = info["long_url"]
                title = info["title"]
                return render_template("edit.html",
                                    errors=form.errors,
                                    netid=current_user.netid,
                                    title=title,
                                    short_url=short_url,
                                    long_url=long_url)
    else: # GET request
        # Hit the database to get information
        short_url = request.args["url"]
        info = client.get_url_info(short_url)
        owner = info["netid"]
        if owner != current_user.netid and not current_user.is_admin():
            return render_index(wrong_owner=True)

        long_url = info["long_url"]
        title = info["title"]
        # Render the edit template
        return render_template("edit.html", netid=current_user.netid,
                                            title=title,
                                            short_url=short_url,
                                            long_url=long_url)