def index(): if 'username' not in session: #flash("Please login to continue") return redirect(url_for('login')) form = ShortenForm() if form.validate_on_submit(): if(form.short_link.data): short_link = form.short_link.data else: short_link = ''.join(random.choices(string.ascii_letters + string.digits, k=5)) long_link = form.link.data while(not is_short_link_valid(short_link)): short_link = ''.join(random.choices(string.ascii_letters + string.digits, k=5)) if not (long_link.startswith("http://") or long_link.startswith("https://")): long_link = 'http://'+long_link owner = session['id'] if add_link(short_link, long_link, owner): # return jsonify(short_link = short_link) short_link = host+short_link # print("Short Link:"+short_link) return render_template('result.html', short_link=short_link) else: return '<h1>Server Error</h1>', 500 return render_template('index.html',form=form)
def homepage(request): """ This view is responsible for displaying the landing page with the shortening form. This page has three flows: first -- post method for accepting the url to be shortened second -- if not post show form third -- error in submitted form Keyword arguments: """ if request.method == 'POST': form = ShortenForm(request.POST) if form.is_valid(): #print form.cleaned_data url = form.cleaned_data['url'] url_shortened = form.cleaned_data['url'] site = strip_to_domain(url) result = make_url_model(url, site) request.session['url_short'] = result.url_shortened return HttpResponseRedirect('/thanks/') else: form = ShortenForm() return render (request, 'index.html', {'form':form})
def index(request): current_site = Site.objects.get_current() data = {} if request.method == 'POST': form = ShortenForm(request.POST) if form.is_valid(): try: link = Link.objects.filter(url=form.cleaned_data['url'], is_autoslug=True)[0] except IndexError: link = form.save() link.users.add(request.user) link.save() # success data data.update({ 'success': True, 'long_url': form.cleaned_data['url'], 'short_url': 'http://%s/%s' % (current_site.domain, link.slug) }) else: # allow pre-populating url with GET (from bookmarklet) initial_url = request.GET.get('url', '') # no circular bookmarking... if initial_url.startswith('http://%s' % current_site.domain): initial_url = '' form = ShortenForm(initial={'url': initial_url}) data.update({'form': form}) return render(request, 'shortener/index.html', data)
def shorts(): form = ShortenForm(request.form) if request.method == 'POST': longurl = form.longurl.data shorturl = form.shorturl.data form_is_valid = form.validate_on_submit() user_is_logged_in = ( g.user is not None and g.user.is_authenticated()) if form_is_valid: shorturl_query = Link.query.\ filter_by(shorturl=shorturl).first() if shorturl_query is not None: flash('Link already exists. Try choosing another one.') return redirect(url_for('index')) else: link = Link(longurl, shorturl) if user_is_logged_in: link.user = g.user db.session.add(link) db.session.commit() return render_template( 'shorts.html', shorturl='{}s/{}'.format(request.url_root, link.shorturl), longurl=link.longurl, user=g.user) elif not form_is_valid: return render_template('index.html', form=form) elif request.method == 'GET': return redirect(url_for('index'))