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-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 (
|
2024-05-28 23:02:17 +02:00
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
2024-04-28 13:15:23 +02:00
|
|
|
)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
2024-12-21 13:07:54 +01:00
|
|
|
from app import db, APP
|
2024-06-17 23:06:51 +02:00
|
|
|
from distribusikan.add_files_to_describer import add_distribusi_files_to_db
|
|
|
|
from distribusikan.distribusi_selector import selector_visible
|
2024-06-17 22:36:23 +02:00
|
|
|
from distribusikan.distribusis_info import DistribusisInfo
|
2024-06-02 22:14:13 +02:00
|
|
|
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-28 13:15:23 +02:00
|
|
|
|
2024-04-21 20:44:08 +02:00
|
|
|
# Forms!
|
2024-06-02 22:14:13 +02:00
|
|
|
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-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
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
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)
|
2024-06-17 22:36:23 +02:00
|
|
|
cssfile = get_css_file(distribusi)
|
|
|
|
unzip_distribusi_files(distribusi, userfolder)
|
|
|
|
clean_up_distribusi_files(userfolder)
|
2024-06-17 23:06:51 +02:00
|
|
|
add_distribusi_files_to_db(distribusi.distribusiname)
|
2024-06-17 22:36:23 +02:00
|
|
|
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"))
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
return render_distribusi_template(distribusiform, current_distribusi)
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
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)
|
2022-03-21 19:54:55 +01:00
|
|
|
|
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))
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def clean_up_distribusi_files(userfolder):
|
2024-05-28 23:02:17 +02:00
|
|
|
if os.path.exists(userfolder):
|
2024-06-17 22:36:23 +02:00
|
|
|
remove_mac_folders(userfolder)
|
2022-03-25 11:57:39 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
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):
|
2024-06-17 22:36:23 +02:00
|
|
|
remove_mac_folders(fullpath)
|
2022-03-25 11:57:39 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
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
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def run_distribusi(userfolder, cssfile):
|
2024-12-21 13:07:54 +01:00
|
|
|
APP.logger.info(
|
|
|
|
f"Run distribusi on this folder: {userfolder} with css:{cssfile}"
|
|
|
|
)
|
|
|
|
distribusify(userfolder, cssfile)
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
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!")
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def delete_css_file(cssfile):
|
2024-05-28 23:02:17 +02:00
|
|
|
if os.path.exists(cssfile):
|
|
|
|
os.remove(cssfile)
|
2022-03-21 19:54:55 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def render_distribusi_template(distribusiform, current_distribusi):
|
2024-05-28 23:02:17 +02:00
|
|
|
uploadform = UploadForm()
|
|
|
|
themeform = ThemeForm()
|
|
|
|
publicthemeform = PublicThemeForm()
|
2024-06-17 22:36:23 +02:00
|
|
|
publicthemeform.publicthemes.choices = DistribusisInfo.public_themes()
|
2024-05-28 23:02:17 +02:00
|
|
|
selectorform = SelectorForm()
|
2024-06-17 22:36:23 +02:00
|
|
|
selectorform.distribusis.choices = DistribusisInfo.user_distribusinames()
|
2024-06-17 23:06:51 +02:00
|
|
|
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
|