mirror of
https://github.com/kevin-DL/ShortMe-URL-Shortener.git
synced 2026-01-18 05:45:07 +00:00
Initial commit
This commit is contained in:
0
app/server/routes/internal/__init__.py
Normal file
0
app/server/routes/internal/__init__.py
Normal file
13
app/server/routes/internal/favicon.py
Normal file
13
app/server/routes/internal/favicon.py
Normal file
@@ -0,0 +1,13 @@
|
||||
# ------- standard library imports -------
|
||||
import os
|
||||
|
||||
# ------- 3rd party imports -------
|
||||
from flask import Blueprint, send_from_directory
|
||||
|
||||
app_blueprint = Blueprint('app_blueprint', __name__)
|
||||
|
||||
|
||||
@app_blueprint.route('/favicon.ico')
|
||||
def favicon():
|
||||
return send_from_directory(os.path.join(app_blueprint.root_path, 'static'),
|
||||
'favicon.ico', mimetype='image/vnd.microsoft.icon')
|
||||
24
app/server/routes/internal/redirect_to_url.py
Normal file
24
app/server/routes/internal/redirect_to_url.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# ------- 3rd party imports -------
|
||||
from flask import Blueprint, redirect, url_for
|
||||
|
||||
# ------- local imports -------
|
||||
from app.server.db.models import Url
|
||||
from app.server.db.extensions import db
|
||||
|
||||
redirect_to_url_blueprint = Blueprint('redirect_to_url_blueprint', __name__)
|
||||
|
||||
|
||||
@redirect_to_url_blueprint.route('/<short_url>')
|
||||
def redirect_to_url(short_url):
|
||||
"""
|
||||
This function will query the database with the short_url and will
|
||||
redirect to the original url if it exist in the database.
|
||||
"""
|
||||
url = Url.query.filter_by(short_url=short_url).first()
|
||||
|
||||
if url:
|
||||
url.visits = url.visits + 1
|
||||
db.session.commit()
|
||||
return redirect(url.original_url)
|
||||
|
||||
return redirect(url_for('page_not_found_blueprint.page_not_found'))
|
||||
50
app/server/routes/internal/send_verification_code.py
Normal file
50
app/server/routes/internal/send_verification_code.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# ------- standard library imports -------
|
||||
import re
|
||||
|
||||
# ------- 3rd party imports -------
|
||||
from flask_mail import Mail, Message
|
||||
from flask import Blueprint, request, redirect, url_for, session
|
||||
|
||||
# ------- local imports -------
|
||||
from app import app
|
||||
from app.server.db.extensions import db
|
||||
from app.server.db.models import VerificationCode, Email
|
||||
|
||||
send_otp_blueprint = Blueprint('send_otp_blueprint', __name__)
|
||||
|
||||
|
||||
@send_otp_blueprint.route('/email_validation', methods=['POST'])
|
||||
def email_validation():
|
||||
input_email = request.form['email']
|
||||
pattern = r'[^@]+@[^@]+\.[^@]+'
|
||||
is_email_valid = re.match(pattern, input_email)
|
||||
|
||||
if is_email_valid:
|
||||
email = Email.query.filter_by(email=input_email).first()
|
||||
|
||||
if email and email.is_verified:
|
||||
auth_token = Email.query.filter_by(email=input_email).first().auth_token
|
||||
return redirect(url_for('your_api_token_blueprint.your_api_token', auth_token=auth_token))
|
||||
|
||||
elif email and not email.is_verified:
|
||||
return redirect(url_for('verify_code_blueprint.enter_verification_code', is_verified=False))
|
||||
|
||||
else:
|
||||
if not email:
|
||||
session['user_email'] = input_email
|
||||
|
||||
email = Email(email=input_email)
|
||||
verification_code = VerificationCode(email=email)
|
||||
db.session.add(verification_code, email)
|
||||
db.session.commit()
|
||||
|
||||
# print('user added to db')
|
||||
# print('verification_code:')
|
||||
# print(verification_code)
|
||||
# print('*' * 33)
|
||||
|
||||
mail = Mail(app.app)
|
||||
msg = Message('ShortMe Verification Code', sender='shortme.biz@gmail.com', recipients=[email.email])
|
||||
msg.body = str(f'Hi!\nThis is your verification code: {verification_code.verification_code}')
|
||||
mail.send(msg)
|
||||
return redirect(url_for('verify_code_blueprint.enter_verification_code'))
|
||||
40
app/server/routes/internal/shorten_url.py
Normal file
40
app/server/routes/internal/shorten_url.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# ------- standard library imports -------
|
||||
import json
|
||||
import requests
|
||||
|
||||
# ------- 3rd party imports -------
|
||||
import flask
|
||||
from flask import Blueprint, request, redirect, url_for
|
||||
|
||||
# ------- local imports -------
|
||||
from app import app
|
||||
|
||||
shorten_url_blueprint = Blueprint('shorten_url_blueprint', __name__, template_folder='templates')
|
||||
|
||||
|
||||
@shorten_url_blueprint.route('/shorten', methods=['POST'])
|
||||
def shorten_url():
|
||||
base_url = flask.url_for("index_blueprint.index", _external=True)
|
||||
original_url = request.form['original_url']
|
||||
shorten_endpoint = base_url + 'api/shorten'
|
||||
|
||||
params = {
|
||||
'url': original_url
|
||||
}
|
||||
|
||||
headers = {
|
||||
'Authorization': f'Bearer {app.app.secret_key}'
|
||||
}
|
||||
|
||||
response = requests.post(shorten_endpoint, headers=headers, params=params)
|
||||
|
||||
if response.status_code == 200:
|
||||
response = json.loads(response.text)
|
||||
short_url = response['short_url']
|
||||
original_url = response['original_url']
|
||||
|
||||
return redirect(url_for('your_short_url_blueprint.your_short_url',
|
||||
short_url=short_url,
|
||||
original_url=original_url))
|
||||
else:
|
||||
return redirect(url_for('error_blueprint.error'))
|
||||
Reference in New Issue
Block a user