2022-03-21 19:54:55 +01:00
|
|
|
import os
|
2022-03-25 11:57:39 +01:00
|
|
|
import shutil
|
2022-03-21 19:54:55 +01:00
|
|
|
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,
|
2022-03-21 19:54:55 +01:00
|
|
|
)
|
|
|
|
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)
|
2022-03-25 11:57:39 +01:00
|
|
|
CleanUpDistribusiFiles(userfolder)
|
2022-03-21 19:54:55 +01:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
2022-03-25 11:57:39 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-03-21 19:54:55 +01:00
|
|
|
def GetCssFile(distribusi):
|
|
|
|
cssfile = ""
|
2022-03-21 20:08:56 +01:00
|
|
|
cssfolder = os.path.join("themes/userthemes", distribusi.distribusiname)
|
2022-03-21 19:54:55 +01:00
|
|
|
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()
|
2022-03-25 11:24:49 +01:00
|
|
|
args = parser.parse_args(["--menu-with-index", "-s", cssfile])
|
2022-03-21 19:54:55 +01:00
|
|
|
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!")
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|