2022-01-05 13:42:24 +01:00
|
|
|
"""This is the main flask distribusi page"""
|
2022-01-05 18:14:45 +01:00
|
|
|
import os
|
2022-01-07 16:55:40 +01:00
|
|
|
import zipfile
|
|
|
|
import shutil
|
2022-01-05 18:14:45 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2022-01-07 16:55:40 +01:00
|
|
|
|
2022-01-05 13:42:24 +01:00
|
|
|
from flask import (
|
|
|
|
render_template,
|
|
|
|
redirect,
|
|
|
|
request,
|
|
|
|
flash,
|
|
|
|
url_for,
|
|
|
|
session,
|
|
|
|
abort,
|
2022-01-07 18:09:29 +01:00
|
|
|
send_from_directory,
|
2022-01-05 13:42:24 +01:00
|
|
|
)
|
|
|
|
from sqlalchemy.exc import (
|
|
|
|
IntegrityError,
|
|
|
|
DataError,
|
|
|
|
DatabaseError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
|
|
|
)
|
|
|
|
from flask_login import (
|
|
|
|
login_user,
|
|
|
|
logout_user,
|
|
|
|
login_required,
|
2022-01-05 15:10:18 +01:00
|
|
|
current_user,
|
2022-01-05 13:42:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from werkzeug.routing import BuildError
|
|
|
|
from flask_bcrypt import generate_password_hash, check_password_hash
|
|
|
|
from flask_wtf.csrf import CSRFError
|
|
|
|
|
|
|
|
from app import create_app, db, login_manager
|
|
|
|
from usermodel import User
|
2022-01-07 16:55:40 +01:00
|
|
|
|
|
|
|
# Forms!
|
2022-01-05 13:42:24 +01:00
|
|
|
from loginform import LoginForm
|
|
|
|
from registerform import RegisterForm
|
2022-01-05 18:14:45 +01:00
|
|
|
from uploadform import UploadForm
|
2022-01-07 16:55:40 +01:00
|
|
|
from distribusiform import DistribusiForm
|
2022-01-05 18:14:45 +01:00
|
|
|
|
|
|
|
# Tada!
|
2022-01-05 19:36:18 +01:00
|
|
|
from distribusi.cli import build_argparser
|
|
|
|
from distribusi.distribusi import distribusify
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
APP = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
@APP.before_request
|
|
|
|
def session_handler():
|
|
|
|
session.permanent = True
|
|
|
|
APP.permanent_session_lifetime = timedelta(minutes=1)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/")
|
|
|
|
def index():
|
2022-01-07 18:09:29 +01:00
|
|
|
users = User.query.filter(User.distribusiname.isnot(None)).all()
|
|
|
|
distribusinames = []
|
|
|
|
for user in users:
|
|
|
|
distribusinames.append(user.distribusiname)
|
|
|
|
return render_template("index.html", distribusinames=distribusinames)
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
loginform = LoginForm()
|
|
|
|
if loginform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
user = User.query.filter_by(email=loginform.email.data).first()
|
2022-01-05 19:36:18 +01:00
|
|
|
if check_password_hash(user.password, loginform.password.data):
|
2022-01-05 13:42:24 +01:00
|
|
|
login_user(user)
|
|
|
|
flash("Logged in successfully.", "success")
|
|
|
|
next = request.args.get("next")
|
2022-01-07 18:09:29 +01:00
|
|
|
if next is not None and not is_safe_url(next): # noqa: F821
|
2022-01-05 13:42:24 +01:00
|
|
|
return abort(400)
|
|
|
|
return redirect(next or url_for("index"))
|
|
|
|
else:
|
|
|
|
flash("Invalid Username or password!", "danger")
|
|
|
|
except Exception as e:
|
|
|
|
flash(e, "danger")
|
|
|
|
return render_template("login.html", loginform=loginform)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
registerform = RegisterForm()
|
|
|
|
if registerform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
email = registerform.email.data
|
2022-01-05 19:36:18 +01:00
|
|
|
password = registerform.confirmpassword.data
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
newuser = User(
|
|
|
|
email=email,
|
2022-01-05 19:36:18 +01:00
|
|
|
password=generate_password_hash(password),
|
2022-01-05 13:42:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(newuser)
|
|
|
|
db.session.commit()
|
|
|
|
flash("Account Succesfully created", "success")
|
2022-01-09 13:10:20 +01:00
|
|
|
login_user(newuser)
|
2022-01-05 19:36:18 +01:00
|
|
|
return redirect(url_for("index"))
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
except InvalidRequestError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Something went wrong!", "danger")
|
|
|
|
except IntegrityError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("User already exists!.", "warning")
|
|
|
|
except DataError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Invalid Entry", "warning")
|
|
|
|
except InterfaceError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Error connecting to the database", "danger")
|
|
|
|
except DatabaseError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Error connecting to the database", "danger")
|
|
|
|
except BuildError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("An error occured !", "danger")
|
|
|
|
return render_template("register.html", registerform=registerform)
|
|
|
|
|
|
|
|
|
2022-01-07 16:55:40 +01:00
|
|
|
@APP.route("/distribusi", methods=["GET", "POST"])
|
2022-01-05 13:42:24 +01:00
|
|
|
@login_required
|
|
|
|
def distribusi():
|
2022-01-05 18:14:45 +01:00
|
|
|
uploadform = UploadForm()
|
2022-01-07 16:55:40 +01:00
|
|
|
distribusiform = DistribusiForm()
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
if user.distribusiname is None:
|
|
|
|
print("nothing to deploy!")
|
|
|
|
if distribusiform.validate_on_submit():
|
|
|
|
zipfilename = "{}.zip".format(user.distribusiname)
|
|
|
|
copyzipfile = os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename)
|
|
|
|
|
|
|
|
newuserfolder = os.path.join("stash", user.distribusiname)
|
|
|
|
os.mkdir(newuserfolder)
|
|
|
|
shutil.copy(copyzipfile, newuserfolder)
|
|
|
|
unzipfile = os.path.join(newuserfolder, zipfilename)
|
|
|
|
with zipfile.ZipFile(unzipfile, "r") as zip_ref:
|
|
|
|
# To do, replace extractall with inspection and extract
|
|
|
|
zip_ref.extractall(newuserfolder)
|
|
|
|
|
|
|
|
os.remove(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename))
|
|
|
|
os.remove(os.path.join(newuserfolder, zipfilename))
|
|
|
|
# Make sure nothing can be executed from the upload folder
|
|
|
|
parser = build_argparser()
|
|
|
|
args = parser.parse_args()
|
|
|
|
distribusify(args, newuserfolder)
|
2022-01-07 18:09:29 +01:00
|
|
|
return redirect(url_for("index"))
|
2022-01-07 16:55:40 +01:00
|
|
|
template = render_template(
|
|
|
|
"distribusi.html",
|
|
|
|
uploadform=uploadform,
|
|
|
|
distribusiform=distribusiform,
|
|
|
|
)
|
|
|
|
return template
|
2022-01-05 18:14:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/upload", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
def upload():
|
2022-01-07 16:55:40 +01:00
|
|
|
success = False
|
2022-01-05 18:14:45 +01:00
|
|
|
uploadform = UploadForm()
|
2022-01-07 16:55:40 +01:00
|
|
|
distribusiform = DistribusiForm()
|
|
|
|
if uploadform.validate_on_submit():
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
user.distribusiname = uploadform.sitename.data
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
zipfilename = "{}.zip".format(user.distribusiname)
|
2022-01-05 18:14:45 +01:00
|
|
|
zipfile = uploadform.zipfile.data
|
2022-01-07 16:55:40 +01:00
|
|
|
zipfile.save(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename))
|
|
|
|
|
|
|
|
success = True
|
|
|
|
template = render_template(
|
|
|
|
"distribusi.html",
|
|
|
|
uploadform=uploadform,
|
|
|
|
distribusiform=distribusiform,
|
|
|
|
success=success,
|
|
|
|
)
|
|
|
|
return template
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
|
2022-01-07 18:09:29 +01:00
|
|
|
@APP.route("/stash/<path:path>")
|
|
|
|
def distribusistash(path):
|
|
|
|
return send_from_directory("stash", path)
|
|
|
|
|
|
|
|
|
2022-01-05 13:42:24 +01:00
|
|
|
@APP.route("/admin")
|
|
|
|
@login_required
|
|
|
|
def admin():
|
|
|
|
return "admin"
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/logout")
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
2022-01-05 15:10:18 +01:00
|
|
|
return redirect(url_for("index"))
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.errorhandler(CSRFError)
|
|
|
|
def handle_csrf_error(e):
|
|
|
|
return render_template("csrf_error.html", reason=e.description), 400
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
return User.query.get(int(user_id))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
APP.debug = True
|
|
|
|
APP.run(port=5000)
|