示例#1
0
 def insertURL(url):
     nonlocal shortID, urlObject
     urlObject, shortID = URL().set_value(
         full=url,
         userid=current_user.id if current_user.is_authenticated else 1)
     db.session.add(urlObject)
     db.session.commit()
示例#2
0
def generate_link():
    # TODO: add data validation

    forward_url = request.form.get("forward_url", "", str)
    if not forward_url:
        return jsonify({
            "status_msg":
            "A forward_url must be supplied for link generation."
        }), 400

    slug = request.form.get("slug", "", str)
    if not slug:
        slug = generate_uuid()

    if URL.query.filter_by(slug=slug).first():
        return jsonify({"status_msg": "Slug already in use :/"}), 400

    db.session.add(URL(slug, forward_url))
    db.session.commit()

    return jsonify({
        "slug": slug,
        "forward_url": forward_url,
        "short_url": urllib.parse.urljoin(request.base_url, slug)
    }), 200
示例#3
0
def save_url(url):
    try:
        total_words = get_count_words(url)
        URL().create_entry(url, total_words)
        return True
    except Exception as exception:
        print('[Error in save url tasks:]', exception)
    return False
    def post(self):
        try:
            from app.models import URL
            url = request.values.get('url')
            if url == None or url == '':
                return APIReturn(status=False, message="need url")

                #!/usr/bin/python3

            import random

            chars = "abcdefghijklmnopqrstuvwxyziABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
            newId = ""

            while len(newId) != 10:
                newId = newId + random.choice(chars)

            u = URL(id=newId, webURL=url)
            u.save()
            return APIReturn(status=True, data=newId)
        except Exception as e:
            return APIReturn(status=False, message=str(e))
示例#5
0
def index():
    form = UrlForm()
    short_url = None
    base_url = request.url_root
    if form.validate_on_submit():
        existing_url = URL.query.filter_by(url=form.url.data).first()
        if existing_url:
            short_url = f'{base_url}{UrlShortner.id_to_url(existing_url.id)}'
        else:
            new_url = URL(url=form.url.data)
            db.session.add(new_url)
            db.session.commit()
            short_url = f'{base_url}{UrlShortner.id_to_url(new_url.id)}'
    return render_template('index.html', form=form, short_url=short_url)
示例#6
0
def custom_code():
    form = GetCustomURLForm()
    if form.validate_on_submit():
        u = URL.query.filter_by(url=form.url.data).first()
        if u is None:
            u = URL(url=form.url.data)
            db.session.add(u)
        c = Code(code=form.customcode.data, url=u)
        db.session.add(c)
        db.session.commit()

        return render_template("custom.html.jinja",
                               title="Custom URL",
                               form=form,
                               code=c.code)

    return render_template("custom.html.jinja", title="Custom URL", form=form)
示例#7
0
def index():
    form = GetURLForm()
    if form.validate_on_submit():
        u = URL.query.filter_by(url=form.url.data).first()
        if u is None:
            u = URL(url=form.url.data)
            db.session.add(u)
            c = Code(code=code_generator(), url=u)
            db.session.add(c)
            db.session.commit()
        else:
            c = u.codes.order_by(func.length(Code.code)).first()
        return render_template("index.html.jinja",
                               title="Index",
                               form=form,
                               code=c.code)
    return render_template("index.html.jinja", title="Index", form=form)