Exemple #1
0
def create_short_url():
    '''
        Takes long _url as url, custom string(opt),
        tag(opt) for input and returns short_url
    '''
    if request.method == 'POST':
        if 'url' in request.args:
            og_url = request.args['url']

            if url_check(og_url) is True:
                if 'custom' in request.args:
                    token_string = request.args['custom']
                    if 'tag' in request.args:
                        tag_url = request.args['tag']
                    else:
                        tag_url = ''
                else:
                    token_string = random_token()

                    if 'tag' in request.args:
                        tag_url = request.args['tag']
                    else:
                        tag_url = ''

                conn = MySQLdb.connect(host, user, passwrd, db)
                cursor = conn.cursor()
                check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE"
                cursor.execute(check_row, (token_string,))
                check_fetch = cursor.fetchone()

                if (check_fetch is None):
                    insert_row = """
                        INSERT INTO WEB_URL(URL, S_URL, TAG) VALUES( %s, %s, %s)
                        """
                    cursor.execute(insert_row, (og_url, token_string, tag_url,))

                    conn.commit()
                    conn.close()

                    short_url = shorty_host + token_string
                    data = jsonify({
                        'long_url': og_url,
                        'short_url': short_url,
                        'custom': token_string,
                        'tag': tag_url
                    })

                    return make_response(data, 200)
                else:
                    data = jsonify({'error': 'suffix already present'})
                    return make_response(data, 200)
            else:
                data = jsonify({'error': 'URL given is not valid . Enter a valid URL.'})
                return make_response(data, 200)
        else:
            data = jsonify({'error': 'invalid request'})
            return make_response(data, 405)
    else:
        data = jsonify({'error': 'Invalid Method Used'})
        return make_response(data, 405)
Exemple #2
0
def index():
    access = False
    if not config.is_secure:
        access = True
    elif 'Auth' in request.headers:
        if request.headers['Auth'] == config.SECRET_KEY:
            access = True
    if not access:
        return redirect('http://basalam.com')

    conn = psycopg2.connect(host=host,
                            user=user,
                            password=passwrd,
                            database=db)
    cursor = conn.cursor()

    # Return the full table to displat on index.
    list_sql = "SELECT * FROM WEB_URL;"
    cursor.execute(list_sql)
    result_all_fetch = cursor.fetchall()

    if request.method == 'POST':
        og_url = request.form.get('url_input')
        custom_suff = request.form.get('url_custom')
        tag_url = request.form.get('url_tag')
        if custom_suff == '':
            token_string = random_token()
        else:
            token_string = custom_suff
        if og_url != '':
            if url_check(og_url):
                # Check's for existing suffix
                check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE"
                cursor.execute(check_row, (token_string, ))
                check_fetch = cursor.fetchone()

                if check_fetch is None:
                    insert_row = """
						INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s)
						"""
                    result_cur = cursor.execute(insert_row, (
                        og_url,
                        token_string,
                        tag_url,
                    ))
                    conn.commit()
                    conn.close()
                    e = ''
                    return render_template('index.html',
                                           shorty_url=shorty_host +
                                           token_string,
                                           error=e)
                else:
                    e = "The Custom suffix already exists." \
                        " Please use another suffix or leave it blank for random suffix."
                    return render_template('index.html',
                                           table=result_all_fetch,
                                           host=shorty_host,
                                           error=e)
            else:
                e = "URL entered doesn't seem valid , Enter a valid URL."
                return render_template('index.html',
                                       table=result_all_fetch,
                                       host=shorty_host,
                                       error=e)

        else:
            e = "Enter a URL."
            return render_template('index.html',
                                   table=result_all_fetch,
                                   host=shorty_host,
                                   error=e)
    else:
        e = ''
        return render_template('index.html',
                               table=result_all_fetch,
                               host=shorty_host,
                               error=e)
Exemple #3
0
def index():

    conn = MySQLdb.connect(host, user, passwrd, db)
    cursor = conn.cursor()

    # Return the full table to displat on index.
    list_sql = "SELECT * FROM WEB_URL;"
    cursor.execute(list_sql)
    result_all_fetch = cursor.fetchall()

    if request.method == 'POST':
        og_url = request.form.get('url_input')
        custom_suff = request.form.get('url_custom')
        tag_url = request.form.get('url_tag')
        if custom_suff == '':
            token_string = random_token()
        else:
            token_string = custom_suff
        if og_url != '':
            if url_check(og_url) == True:

                # Check's for existing suffix
                check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE"
                cursor.execute(check_row, (token_string, ))
                check_fetch = cursor.fetchone()

                if (check_fetch is None):
                    insert_row = """
						INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s)
						"""
                    result_cur = cursor.execute(insert_row, (
                        og_url,
                        token_string,
                        tag_url,
                    ))
                    conn.commit()
                    conn.close()
                    e = ''
                    return render_template('index.html',
                                           shorty_url=shorty_host +
                                           token_string,
                                           error=e)
                else:
                    e = "The Custom suffix already exists . Please use another suffix or leave it blank for random suffix."
                    return render_template('index.html',
                                           table=result_all_fetch,
                                           host=shorty_host,
                                           error=e)
            else:
                e = "URL entered doesn't seem valid , Enter a valid URL."
                return render_template('index.html',
                                       table=result_all_fetch,
                                       host=shorty_host,
                                       error=e)

        else:
            e = "Enter a URL."
            return render_template('index.html',
                                   table=result_all_fetch,
                                   host=shorty_host,
                                   error=e)
    else:
        e = ''
        return render_template('index.html',
                               table=result_all_fetch,
                               host=shorty_host,
                               error=e)