Exemplo n.º 1
0
def url(name):

    form = UrlForm()
    urls = mongo.db[name]
    mssg=''
    if session['username']:
        if request.method == 'POST':
            existing_url = urls.find_one({'original':form.urls.data})

            if existing_url:
                mssg = 'URL already shortened'
                hi = urls.find()
                return render_template('shortner.html', form=form, name=name, hi=hi, mssg=mssg)

            else:
                if form.validate_on_submit():
                    rand = random.randint(0, pow(10, 5))
                    temp = "http://127.0.0.1:5000/"+name+"/"+ str(rand)
                    urls.insert({'original':form.urls.data,'short':temp})
                    return redirect(url_for('url', name=name))

        else:
            hi = urls.find()
            return render_template('shortner.html', form=form, name=name, hi=hi,mssg=mssg)

    return redirect(url_for('login'))
Exemplo n.º 2
0
 def home():
     form = UrlForm()
     if form.validate_on_submit():
         boolIsExist = checkIfExistInDatabase(form['urlFromUser'].data)
         if boolIsExist is False:
             shortUrl = randomStringDigits(5)
             create_SUrl(shortUrl, form['urlFromUser'].data)
             flash('You are enter a good URL: ' + form['urlFromUser'].data)
             session['shortUrl'] = shortUrl
             return redirect(url_for('shortener'))
         else:
             shortUrl = getShortUrlFromDatabase(form['urlFromUser'].data)
             session['shortUrl'] = shortUrl
             return redirect(url_for('shortener'))
     return render_template('home.html', title='home', form=form)
Exemplo n.º 3
0
def add_url():
    form = UrlForm()
    if form.validate_on_submit():
        # create a unique slug
        while True:
            slug = uuid.uuid4().hex[:6]
            link = Link.query.filter_by(slug=slug).first()
            if not link:
                break

        # enter the data into database with count = 0
        link = Link(url=form.url.data, slug=slug, count=0)
        db.session.add(link)
        db.session.commit()
        
        return redirect(url_for('home'))
    return render_template('add_url.html', form=form)
Exemplo n.º 4
0
def shortener():

    form = UrlForm()
    bijective_obj = bijective.ShortURL()

    if form.validate_on_submit():
        change_key = "~ck" + str(
            bijective_obj.encode(randint(100000000, 99999999999)))

        if form.custom_alias.data == '':  #No custom alias given
            form.custom_alias.data = str(
                bijective_obj.encode(randint(100000000000, 999999999999)))

        if db_crud.add_url_record(form.custom_alias.data,
                                  f.validate_urls(form.long_url.data),
                                  change_key) == "inserted":
            flash('Great ! Go checkout ' + home_url + '/%s' %
                  (form.custom_alias.data))
            return render_template('form.html',
                                   form=form,
                                   home_url=home_url,
                                   custom_alias=form.custom_alias.data,
                                   link_to="http://" + home_url + "/" +
                                   form.custom_alias.data,
                                   short_url_creation=True,
                                   changekey=change_key)
        else:
            flash('Woops! The custom alias ' + home_url +
                  '/%s  is taken. Try a different one. ' %
                  (form.custom_alias.data))
            return render_template('form.html',
                                   form=form,
                                   home_url=home_url,
                                   link_to="http://" + home_url + "/shorten",
                                   short_url_creation=False)

    return render_template('form.html', form=form, home_url=home_url)