def add_link(): full_url = request.form['original_url'] # validate received url if not validators.url(full_url): flash('{} is not a valid url'.format(full_url)) return redirect(url_for('short.index')) if not request.form['life_period']: life_period = 90 else: life_period = int(request.form['life_period']) # validate received url if life_period not in range(1, 365): flash( '{} is not a valid life period. Choose between 1 and 365 days'. format(str(life_period))) return redirect(url_for('short.index')) # if this url already exists render it exist_url = UrlRepository.get_exist_url(full_url) if exist_url: return render_template('link_added.html', new_link=exist_url.url_hash, original_url=exist_url.full_url) result = UrlRepository.create(full_url=full_url, life_period=int(life_period)) return render_template('link_added.html', new_link=result.url_hash, original_url=result.full_url)
def urls(): ROWS_PER_PAGE = 3 # Get the data from the database page = request.args.get('page', 1, type=int) urls = UrlRepository.paginate(page=page, per_page=ROWS_PER_PAGE) return render_template('url/list.html', urls=urls)
def url(full_url): """Return full_url if valid, raise an exception in other case.""" if not validators.url(full_url): raise ValidationError('{} is not a valid url'.format(full_url)) if UrlRepository.get_exist_url(full_url): raise ValidationError('{} is already exists'.format(full_url)) return full_url
def get(self): return UrlRepository.list()
def post(self): args = post_reqparse.parse_args() result = UrlRepository.create(full_url=args['full_url'], life_period=args['life_period']) return result, 201
def url_item(id): url = UrlRepository.get_by_id(id=id) return render_template('url/item.html', url=url)
def delete_url(): delete_url_id = request.form['delete_url_id'] UrlRepository.delete(id=delete_url_id) return redirect(url_for('short.urls'))
def redirect_to_url(short_url): url = UrlRepository.get(url_hash=short_url) return redirect(url.full_url), 301