distribusi-verse/verse/distribusiworkflow.py

156 lines
4.5 KiB
Python
Raw Normal View History

import os
import shutil
import zipfile
from flask_login import current_user
from flask import (
render_template,
redirect,
url_for,
2022-03-21 20:08:56 +01:00
flash,
)
from sqlalchemy.exc import (
InvalidRequestError,
DataError,
InterfaceError,
DatabaseError,
)
from app import db
from usermodel import User
from distribusimodel import Distribusis
# UserPengguna
from statuspengguna.helper import (
IsZipUploaded,
IsCssSelected,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from distribusiselector import SelectorVisible
# Forms!
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
from forms.publicthemeform import PublicThemeForm
from forms.selectorform import SelectorForm
from distribusisinfo import DistribusisInfo
# Tada!
from distribusi.cli import build_argparser
from distribusi.distribusi import distribusify
def DistribusiWorkflow():
distribusiform = DistribusiForm()
current_distribusi = CurrentDistribusi()
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 = GetCssFile(distribusi)
UnzipDistribusiFiles(distribusi, userfolder)
CleanUpDistribusiFiles(userfolder)
RunDistribusi(userfolder, cssfile)
SetDistribusiToVisible(distribusi, user)
DeleteCssFile(cssfile)
return redirect(url_for("index"))
return RenderDistribusiTemplate(distribusiform, current_distribusi)
def UnzipDistribusiFiles(distribusi, userfolder):
zipfilename = "{}.zip".format(distribusi.distribusiname)
unzipfile = os.path.join(userfolder, zipfilename)
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 CleanUpDistribusiFiles(userfolder):
if os.path.exists(userfolder):
RemoveMacFolders(userfolder)
def RemoveMacFolders(path):
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):
RemoveMacFolders(fullpath)
def GetCssFile(distribusi):
cssfile = ""
2022-03-21 20:08:56 +01:00
cssfolder = os.path.join("themes/userthemes", distribusi.distribusiname)
for filename in os.listdir(cssfolder):
if filename.endswith(".css"):
cssfile = os.path.join(cssfolder, filename)
return cssfile
def RunDistribusi(userfolder, cssfile):
parser = build_argparser()
args = parser.parse_args(["--menu-with-index", "-s", cssfile])
distribusify(args, userfolder)
def SetDistribusiToVisible(distribusi, user):
try:
distribusi.visible = True
user.currentdistribusi = None
db.session.commit()
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
2022-03-21 20:08:56 +01:00
flash("Unknown error occured!")
def DeleteCssFile(cssfile):
if os.path.exists(cssfile):
os.remove(cssfile)
def RenderDistribusiTemplate(distribusiform, current_distribusi):
uploadform = UploadForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
selectorvisible = SelectorVisible()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
limit_reached = DistribusiLimitReached()
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