Beispiel #1
0
    def wrapper(*args):
        arg = args[0]  # Get the Record object since wrapper acquires a tuple

        # Authentication: check if username exists and correct API key is
        # provided
        providedKey = arg.key
        username = arg.username
        try:
            with pg_simple.PgSimple() as db:
                key = db.fetchone(
                    'users', fields=['api'], where=('name = %s', [username])
                )[0][
                    0]  # delete the last zero once 24-char API keys are implemented
                latest = db.fetchone('data',
                                     fields=['datetime'],
                                     where=('api = %s', [key]),
                                     order=['datetime', 'DESC'])[0]
            if providedKey != key:
                return 401

        except TypeError:
            return 401

        # Parse datetime or completely reject request if incorrect (because
        # where would you place it on a graph?)
        try:
            d = datetime.strptime(arg.timestamp, '%Y-%m-%d,%H:%M:%S')
            if not isinstance(d, datetime):
                return 400
            if d - latest < timedelta(minutes=15):
                return 403
        except (ValueError, TypeError):
            return 400

        # The speed data validation
        # 0.0 values in case of an error will still indicate connection
        # and point out a client-side problem with speedtest visually on a graph

        # These ones are separate so in case one breaks the rest is still saved
        try:
            arg.download = float(re.findall('([0-9]+\.[0-9]+)', \
                                            arg.download)[0])
        except:
            arg.download = 0.0

        try:
            arg.upload = float(re.findall('([0-9]+\.[0-9]+)', arg.upload)[0])
        except:
            arg.upload = 0.0

        try:
            arg.ping = float(re.findall('([0-9]+\.[0-9]+)', arg.ping)[0])
        except:
            arg.ping = 0.0

        return func(arg)
Beispiel #2
0
def insert_url(url):
    db.execute("SELECT id from url_short where url='%s'" %url)
    if db.rowcount():
        return db.fetchone()[0]
    db.execute("INSERT INTO url_short(url, count) VALUES('%s',0)" %url)
    db.con.commit()
    return db.cursor.lastrowid
Beispiel #3
0
def on_follow_short_link(request, short_id):
    db.execute("SELECT * from url_short where id=%s" %short_id)
    link_target = db.fetchone()
    if link_target is None:
        raise NotFound()
    db.execute("UPDATE url_short SET count=%d where id=%d "%(link_target[2]+1, link_target[0]))
    db.con.commit()
    return redirect(link_target[1])
Beispiel #4
0
def on_short_link_details(request, short_id):
    query = "SELECT * from url_short where id=%d" %int(short_id)
    # pdb.set_trace()
    db.execute("SELECT * from url_short where id=%d" %int(short_id))
    link_target = db.fetchone()
    if link_target is None:
        raise NotFound()
    click_count = int(link_target[2])
    return render_template('short_link_details.html',
        link_target=link_target[1],
        short_id=link_target[0],
        click_count=click_count+1
    )