Last active
August 14, 2021 09:54
-
-
Save ashutoshkrris/4dd6efc3575377676fb6f937e5fcc1f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from app.models import ShortUrls | |
from app import app, db | |
from flask import render_template, request, flash, redirect, url_for | |
@app.route('/', methods=['GET', 'POST']) | |
def index(): | |
if request.method == 'POST': | |
url = request.form['url'] | |
short_id = request.form['custom_id'] | |
if short_id and ShortUrls.query.filter_by(short_id=short_id).first() is not None: | |
flash('Please enter different custom id!') | |
return redirect(url_for('index')) | |
if not url: | |
flash('The URL is required!') | |
return redirect(url_for('index')) | |
if not short_id: | |
short_id = generate_short_id(8) | |
new_link = ShortUrls( | |
original_url=url, short_id=short_id, created_at=datetime.now()) | |
db.session.add(new_link) | |
db.session.commit() | |
short_url = request.host_url + short_id | |
return render_template('index.html', short_url=short_url) | |
return render_template('index.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment