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
|
|
|
|
|
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
|
2024-04-28 13:15:23 +02:00
|
|
|
from sqlalchemy.exc import (
|
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
|
|
|
)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
|
|
|
from app import db
|
|
|
|
from distribusikan.distribusiselector import SelectorVisible
|
|
|
|
from distribusikan.distribusisinfo import DistribusisInfo
|
2022-03-21 19:54:55 +01:00
|
|
|
from forms.distribusiform import DistribusiForm
|
|
|
|
from forms.publicthemeform import PublicThemeForm
|
|
|
|
from forms.selectorform import SelectorForm
|
2024-04-21 20:44:08 +02:00
|
|
|
from forms.themeform import ThemeForm
|
2024-04-28 13:15:23 +02:00
|
|
|
|
2024-04-21 20:44:08 +02:00
|
|
|
# Forms!
|
|
|
|
from 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-28 13:15:23 +02:00
|
|
|
|
2024-04-21 20:44:08 +02:00
|
|
|
# UserPengguna
|
|
|
|
from statuspengguna.helper import UserHelper
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def DistribusiWorkflow():
|
|
|
|
distribusiform = DistribusiForm()
|
2022-03-28 20:16:34 +02:00
|
|
|
current_distribusi = UserHelper.current_distribusi()
|
2022-03-21 19:54:55 +01:00
|
|
|
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)
|
2024-04-21 20:44:08 +02:00
|
|
|
if filename.startswith("."):
|
2022-03-25 11:57:39 +01:00
|
|
|
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-04-04 22:00:35 +02:00
|
|
|
if os.path.exists(cssfolder):
|
|
|
|
for filename in os.listdir(cssfolder):
|
|
|
|
if filename.endswith(".css"):
|
|
|
|
cssfile = os.path.join(cssfolder, filename)
|
2022-03-21 19:54:55 +01:00
|
|
|
return cssfile
|
|
|
|
|
|
|
|
|
|
|
|
def RunDistribusi(userfolder, cssfile):
|
|
|
|
parser = build_argparser()
|
2024-04-27 11:26:19 +02:00
|
|
|
args = parser.parse_args(["-t", "--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()
|
|
|
|
|
2022-03-28 20:16:34 +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()
|
2022-03-21 19:54:55 +01:00
|
|
|
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
|