Fix redirecting
This commit is contained in:
parent
cf6acce120
commit
0c912bf226
29
app/main.py
29
app/main.py
@ -12,10 +12,10 @@ import os
|
|||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from models import User
|
from models import User, Link
|
||||||
from database import *
|
from database import *
|
||||||
from app.util.log import log
|
from app.util.log import log
|
||||||
from var import BASE_URL
|
from var import BASE_URL
|
||||||
|
|
||||||
|
|
||||||
class FlaskUser(UserMixin):
|
class FlaskUser(UserMixin):
|
||||||
@ -139,18 +139,23 @@ Log all records for visits to shortened links
|
|||||||
|
|
||||||
@app.route("/<link>", methods=["GET"])
|
@app.route("/<link>", methods=["GET"])
|
||||||
def log_redirect(link):
|
def log_redirect(link):
|
||||||
# If the `link` is more than 5 characters, ignore
|
# If `link` is not exactly 5 characters, return redirect to base url
|
||||||
if len(link) > 5:
|
if len(link) != 5:
|
||||||
return redirect(BASE_URL)
|
return redirect(BASE_URL)
|
||||||
|
|
||||||
# If the `link` is one of the registered routes, ignore
|
# Make sure the link exists in the database
|
||||||
if link in ["login", "signup", "dashboard", "logout", "api"]:
|
db = SessionLocal()
|
||||||
return
|
link_record = db.query(Link).filter(Link.link == link).first()
|
||||||
|
if not link_record:
|
||||||
ip = request.remote_addr
|
db.close()
|
||||||
user_agent = request.headers.get("user-agent")
|
return redirect(BASE_URL)
|
||||||
redirect_link = log(link, ip, user_agent)
|
else:
|
||||||
return redirect(redirect_link)
|
# Log the visit
|
||||||
|
ip = request.remote_addr
|
||||||
|
user_agent = request.headers.get("User-Agent")
|
||||||
|
log(link, ip, user_agent)
|
||||||
|
db.close()
|
||||||
|
return redirect(link_record.redirect_link)
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(401)
|
@app.errorhandler(401)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user