Ejemplo n.º 1
0
    def create_url():
        if not request.is_json:
            abort(422)

        content = request.json
        redirect_url = content.get('url', '').strip()

        if not is_valid_url(redirect_url):
            abort(422)

        # Check if it already exists
        url = Url.query.filter(Url.redirect == redirect_url).first()

        if url:
            return jsonify({'shorter': url.get_full_short()})

        next_id = db.session.execute(Sequence("urls_id_seq"))
        url = Url(id=next_id,
                  redirect=redirect_url,
                  slug=to_emoji_slug(next_id))

        db.session.add(url)
        db.session.commit()

        return jsonify({'shorter': url.get_full_short()})
Ejemplo n.º 2
0
    def mutate(self, info, redirect_url):
        redirect_url = redirect_url.strip()
        if not is_valid_url(redirect_url):
            return 'Malformed URL: {}'.format(redirect_url)

        # Check if it already exists
        url_obj = Url.query.filter(Url.redirect==redirect_url).first()

        if url_obj:
            ok = False # return false because it was found, but not created
            return CreateShortUrl(short=url_obj.get_full_short(), ok=ok)

        next_id = db.session.execute(Sequence("urls_id_seq"))
        url_obj = Url(id=next_id, redirect=redirect_url, slug=to_emoji_slug(next_id))

        db.session.add(url_obj)
        db.session.commit()
        ok = True

        return CreateShortUrl(short=url_obj.get_full_short(), ok=ok)