distribusi-verse/verse/distribusikan/distribusi_workflow.py

147 lines
4.9 KiB
Python
Raw Normal View History

import os
import shutil
import zipfile
2024-04-21 20:44:08 +02:00
# Tada!
from distribusi.cli import build_argparser
2024-04-28 13:04:07 +02:00
from distribusi.distribusi import distribusify
2024-04-24 23:05:36 +02:00
from flask import flash, redirect, render_template, url_for
from flask_login import current_user
from sqlalchemy.exc import (
2024-05-28 23:02:17 +02:00
DatabaseError,
DataError,
InterfaceError,
InvalidRequestError,
)
2024-04-28 13:04:07 +02:00
from app import db
from distribusikan.add_files_to_describer import add_distribusi_files_to_db
from distribusikan.distribusi_selector import selector_visible
from distribusikan.distribusis_info import DistribusisInfo
from distribusikan.forms.distribusiform import DistribusiForm
from distribusikan.forms.publicthemeform import PublicThemeForm
from distribusikan.forms.selectorform import SelectorForm
from distribusikan.forms.themeform import ThemeForm
2024-04-21 20:44:08 +02:00
# Forms!
from distribusikan.forms.uploadform import UploadForm
2024-04-28 15:34:29 +02:00
from models.distribusi_model import Distribusis
from models.user_model import User
2024-04-21 20:44:08 +02:00
# UserPengguna
from statuspengguna.helper import UserHelper
def distribusi_workflow():
2024-05-28 23:02:17 +02:00
distribusiform = DistribusiForm()
current_distribusi = UserHelper.current_distribusi()
user = User.query.filter_by(email=current_user.email).first()
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
if distribusiform.validate_on_submit():
userfolder = os.path.join("stash", distribusi.distribusiname)
cssfile = get_css_file(distribusi)
unzip_distribusi_files(distribusi, userfolder)
clean_up_distribusi_files(userfolder)
add_distribusi_files_to_db(distribusi.distribusiname)
run_distribusi(userfolder, cssfile)
set_distribusi_to_visible(distribusi, user)
delete_css_file(cssfile)
2024-05-28 23:02:17 +02:00
return redirect(url_for("index"))
return render_distribusi_template(distribusiform, current_distribusi)
def unzip_distribusi_files(distribusi, userfolder):
2024-05-28 23:02:17 +02:00
zipfilename = "{}.zip".format(distribusi.distribusiname)
unzipfile = os.path.join(userfolder, zipfilename)
2024-05-28 23:02:17 +02:00
if os.path.exists(unzipfile):
with zipfile.ZipFile(unzipfile, "r") as zip_ref:
zip_ref.extractall(userfolder)
# after extracting all files remove zip file.
if os.path.exists(unzipfile):
os.remove(os.path.join(userfolder, zipfilename))
def clean_up_distribusi_files(userfolder):
2024-05-28 23:02:17 +02:00
if os.path.exists(userfolder):
remove_mac_folders(userfolder)
def remove_mac_folders(path):
2024-05-28 23:02:17 +02:00
for filename in os.listdir(path):
fullpath = os.path.join(path, filename)
if filename.startswith("."):
if os.path.isdir(fullpath):
shutil.rmtree(fullpath)
else:
os.remove(fullpath)
if filename == "__MACOSX":
shutil.rmtree(fullpath)
if os.path.isdir(fullpath):
remove_mac_folders(fullpath)
def get_css_file(distribusi):
2024-05-28 23:02:17 +02:00
cssfile = ""
cssfolder = os.path.join("themes/userthemes", distribusi.distribusiname)
if os.path.exists(cssfolder):
for filename in os.listdir(cssfolder):
if filename.endswith(".css"):
cssfile = os.path.join(cssfolder, filename)
return cssfile
def run_distribusi(userfolder, cssfile):
2024-06-23 18:24:05 +02:00
print(f"Run distribusi on this folder: {userfolder} with css:{cssfile}")
2024-05-28 23:02:17 +02:00
parser = build_argparser()
2024-06-23 22:31:12 +02:00
args = parser.parse_args(["-t", "-a", "--menu-with-index", "-s", cssfile])
2024-05-28 23:02:17 +02:00
distribusify(args, userfolder)
def set_distribusi_to_visible(distribusi, user):
2024-05-28 23:02:17 +02:00
try:
distribusi.visible = True
user.currentdistribusi = None
db.session.commit()
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
flash("Unknown error occured!")
def delete_css_file(cssfile):
2024-05-28 23:02:17 +02:00
if os.path.exists(cssfile):
os.remove(cssfile)
def render_distribusi_template(distribusiform, current_distribusi):
2024-05-28 23:02:17 +02:00
uploadform = UploadForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.public_themes()
2024-05-28 23:02:17 +02:00
selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.user_distribusinames()
selectorvisible = selector_visible()
2024-05-28 23:02:17 +02:00
files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = UserHelper.is_css_selected(current_distribusi)
limit_reached = UserHelper.distribusi_limit_reached()
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
publicthemeform=publicthemeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
limit_reached=limit_reached,
)
return template