-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
150 lines (113 loc) · 4.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from flask import Flask, request, render_template, redirect, session, flash, url_for
from flask_sqlalchemy import SQLAlchemy
import base64
import json
with open('config.json', 'r') as c:
params = json.load(c)["params"]
local_server = False
# DB_NAME = urlshortner, TABLE_NAME = links, COLUMNS = url_slug, original_url
app = Flask(__name__)
app.secret_key = "application-secret-key"
app.config['SQLALCHEMY_DATABASE_URI'] = params['db_uri']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Links(db.Model):
url_slug = db.Column(db.String(100), primary_key=True, nullable=False)
original_url = db.Column(db.String(1000), nullable=False)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(503)
def page_not_found(e):
return "503 | Database Server Error | Please Try Again Later", 503
@app.errorhandler(500)
def page_not_found(e):
return "500 | Internal Server Error | Please Try Again Later", 503
@app.route("/")
def home():
return render_template("home.html")
@app.route("/static")
@app.route("/static/")
def block_static():
return render_template("404.html")
@app.route("/login", methods=["GET", "POST"])
def login():
if ('user' in session and session["user"] == params["admin_user"]):
links = Links.query.all()
return redirect("/dashboard")
if request.method == "GET":
return render_template("login.html")
elif request.method == "POST":
return redirect("/dashboard")
else:
return "Method Not Supported!"
@app.route("/<string:slug>")
def shortened(slug):
slug = slug.lower()
link = Links.query.filter_by(url_slug=slug).first()
if link == None:
return render_template("404.html")
redirect_url = link.original_url
return redirect(f"{redirect_url}")
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method == "GET":
return redirect("/")
elif request.method == "POST":
url = request.form.get('url')
slug = request.form.get('slug').lower()
slug = ''.join(e for e in slug if e.isalnum())
slug = slug.replace(" ", "")
if slug.lower() in ['login', 'add', 'logout', 'delete', 'dashboard']:
flash("red")
flash("Error")
flash("Cannot Add A System Used Slug! Try Another Slug!")
return redirect(url_for("home"))
if len(slug) == 0:
flash("red")
flash("Error")
flash("Enter A Valid Slug!")
return redirect(url_for("home"))
links = Links.query.filter_by(url_slug=slug).all()
if len(links) == 0:
link = Links(url_slug=slug, original_url=url)
db.session.add(link)
db.session.commit()
flash("green")
flash("Success")
flash("Shortened your url as:")
flash(f"shortyy.ml/{slug}")
return redirect(url_for("home"))
else:
flash("red")
flash("Error")
flash("That Slug Is Already Used! Try Another Slug!")
return redirect(url_for("home"))
else:
return "Method Not Supported!"
@app.route("/logout")
def logout():
session.pop('user')
return redirect('/dashboard')
@app.route("/delete/<string:slug>", methods=["GET", "POST"])
def delete(slug):
if ('user' in session and session['user'] == params['admin_user']):
link = Links.query.filter_by(url_slug=slug).first()
db.session.delete(link)
db.session.commit()
return redirect('/dashboard')
@app.route("/dashboard", methods=["GET", "POST"])
def dashboard():
if ('user' in session and session["user"] == params["admin_user"]):
links = Links.query.all()
return render_template("dashboard.html", params=params, links=links)
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if (username == params['admin_user'] and password == params['admin_password']):
session['user'] = username
links = Links.query.all()
return render_template('dashboard.html', params=params, links=links)
return render_template('login.html', params=params)
if __name__ == "__main__":
app.run(debug=True)