distribusi-verse: medium-tech web app content management system for the web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

136 lines
3.9 KiB

import os
import zipfile
from flask_login import current_user
from flask import (
render_template,
redirect,
url_for,
)
from sqlalchemy.exc import (
IntegrityError,
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)
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 GetCssFile(distribusi):
cssfile = ""
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(["-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()
distribusiform.submit.errors.append("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