Beispiel #1
0
def shortener():
    shortener_form = ShortenerForm(request.form)
    short_url = ''
    if request.method == 'POST' and shortener_form.validate():
        full_url = shortener_form.full_url.data
        logged_in = session.has_key('login') and session['login']
        if logged_in:
            url_hash = make_hash(full_url+session['login']) 
        else:
            url_hash = make_hash(full_url)
        short_url = make_short_url(app.config['HOST'],
                                   app.config['PORT'],
                                   url_hash)
        if Hash.query.filter_by(url_hash=url_hash).first() == None:
            if logged_in:
                user = User.query.filter_by(login=session['login']).first()
                hash_obj = Hash(url_hash, full_url)
                user.hashes.append(hash_obj)
                db.session.commit()
            else:
                user = User.query.filter_by(login='******').first()
                if not user:
                    user = User('not_registered', 'pass')
                    db.session.add(user)
                hash_obj = Hash(url_hash, full_url)
                user.hashes.append(hash_obj)
                db.session.commit()
               
    return render_template('shortener.html', short_url=short_url, form=shortener_form)
Beispiel #2
0
def shortener():
    shortener_form = ShortenerForm(request.form)
    short_url = ''
    if request.method == 'POST' and shortener_form.validate():
        full_url = shortener_form.full_url.data
        logged_in = session.has_key('login') and session['login']
        if logged_in:
            url_hash = make_hash(full_url + session['login'])
        else:
            url_hash = make_hash(full_url)
        short_url = make_short_url(app.config['HOST'], app.config['PORT'],
                                   url_hash)
        if Hash.query.filter_by(url_hash=url_hash).first() == None:
            if logged_in:
                user = User.query.filter_by(login=session['login']).first()
                hash_obj = Hash(url_hash, full_url)
                user.hashes.append(hash_obj)
                db.session.commit()
            else:
                user = User.query.filter_by(login='******').first()
                if not user:
                    user = User('not_registered', 'pass')
                    db.session.add(user)
                hash_obj = Hash(url_hash, full_url)
                user.hashes.append(hash_obj)
                db.session.commit()

    return render_template('shortener.html',
                           short_url=short_url,
                           form=shortener_form)
Beispiel #3
0
def statistics():
    logged_in = session.has_key('login') and session['login']
    if logged_in:
        user_id = User.query.filter_by(login=session['login']).first().id
        hashes = Hash.query.filter_by(user_id=user_id)
        for hash_obj in hashes:
            short_url = make_short_url(app.config['HOST'], app.config['PORT'],
                                       hash_obj.url_hash)
            hash_obj.url_hash = short_url
        return render_template('statistics.html', hashes=enumerate(hashes))
    else:
        errors = ['You should log in first']
        return render_template('errors.html', errors=errors)
Beispiel #4
0
def statistics():
    logged_in = session.has_key('login') and session['login']
    if logged_in:
        user_id = User.query.filter_by(login=session['login']).first().id
        hashes = Hash.query.filter_by(user_id=user_id)
        for hash_obj in hashes:
            short_url = make_short_url(app.config['HOST'],
                                       app.config['PORT'],
                                       hash_obj.url_hash)
            hash_obj.url_hash = short_url 
        return render_template('statistics.html', hashes=enumerate(hashes))
    else:
        errors = ['You should log in first']
        return render_template('errors.html', errors=errors)