Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 3
0
def shorten_url(request, template_name='shortnr/index.html', reverse_url='shorten_url'):    
    context = RequestContext(request)
    dict = {}
    if request.POST:
        form = ShortenerForm(request.POST)
        if form.is_valid():
            try:
                url = ShortenedUrl.objects.get(url=form.cleaned_data['url'])
                if (url.user is None and request.user.is_authenticated() == False) or (url.user == request.user): 
                    dict['shortened_url'] = url.short_url
                    dict['form'] = ShortenerForm()
                    return render_to_response(
                        template_name,
                        dict,
                        context,
                    )
                        
            except:
                print form.cleaned_data['url']
            url = form.save(commit=False)
            if request.user.is_authenticated():
                url.user = request.user
                reverse_url = 'manage_urls'
            url.save()
            url.shorten_url()
            
            dict['shortened_url'] = url.short_url
            dict['form'] = ShortenerForm()
            return render_to_response(
                template_name,
                dict,
                context,
            )
    else:
        form = ShortenerForm()
    
    dict['form'] = form
    
    return render_to_response(
        template_name,
        dict,
        context,
    )
Ejemplo n.º 4
0
def shortener(request):
    link = ShortUrl.objects.filter()
    if request.method == 'POST':
        form = ShortenerForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            if ShortUrl.objects.filter(original_url = data['original_url']).count()>0:
                shortUrl = ShortUrl.objects.filter(original_url = data['original_url']).get()
                context = {'shortener': ShortenerForm(), 'shortUrl': shortUrl, 'links': link}
                return render(request, 'result.html', context)
            else:
                symbols = string.digits + string.letters
                key = ''.join(choice(symbols) for item in range(7))
                short_url = 'http://127.0.0.1:8000/%s' %key
                new_shortUrl = ShortUrl.objects.create(original_url = data['original_url'], short_url=short_url, clicks=0)
                context = {'shortener': ShortenerForm(),'links': link, 'new_shortUrl': new_shortUrl}
                return render(request,'result.html', context)
        context = {'shortener': form, 'links': link}
        return render(request, 'result.html', context)
    else:
        context = {'shortener': ShortenerForm(), 'links': link}
        return render(request, 'result.html', context)