forked from crunk/distribusi-verse
192 lines
6.3 KiB
Python
192 lines
6.3 KiB
Python
import os
|
|
import shutil
|
|
|
|
from flask import flash, render_template, redirect, url_for
|
|
from flask_login import current_user
|
|
from sqlalchemy.exc import (
|
|
DatabaseError,
|
|
DataError,
|
|
InterfaceError,
|
|
InvalidRequestError,
|
|
)
|
|
|
|
from app import db, APP
|
|
from distribusikan.distribusis_info import DistribusisInfo
|
|
from distribusikan.forms.distribusiform import DistribusiForm
|
|
from distribusikan.forms.publicthemeform import PublicThemeForm
|
|
from distribusikan.forms.selectorform import SelectorForm
|
|
from distribusikan.forms.themeform import ThemeForm
|
|
from distribusikan.forms.uploadform import UploadForm
|
|
from models.distribusi_model import Distribusis
|
|
from models.distribusi_file_model import DistribusiFiles
|
|
from models.user_model import User
|
|
|
|
# UserPengguna
|
|
from statuspengguna.helper import UserHelper
|
|
|
|
|
|
def distribusi_selector():
|
|
uploadform = UploadForm()
|
|
selectorform = SelectorForm()
|
|
selectorform.distribusis.choices = DistribusisInfo.user_distribusinames()
|
|
current_distribusi = UserHelper.current_distribusi()
|
|
if selectorform.validate_on_submit():
|
|
if selectorform.new.data:
|
|
select_new_distribusi()
|
|
if selectorform.describe.data:
|
|
return select_describe_distribusi(selectorform.distribusis.data)
|
|
if selectorform.delete.data:
|
|
selectorform = delete_distribusi(selectorform.distribusis.data)
|
|
selectorform.distribusis.choices = (
|
|
DistribusisInfo.user_distribusinames()
|
|
)
|
|
if selectorform.update.data:
|
|
select_update_distribusi(selectorform.distribusis.data)
|
|
current_distribusi = UserHelper.current_distribusi()
|
|
uploadform = auto_fill_in_upload_form(
|
|
uploadform, current_distribusi
|
|
)
|
|
|
|
return render_distribusi_template(
|
|
selectorform, uploadform, current_distribusi
|
|
)
|
|
|
|
|
|
def auto_fill_in_upload_form(uploadform, current_distribusi):
|
|
distribusi = Distribusis.query.filter_by(
|
|
distribusiname=current_distribusi
|
|
).first()
|
|
uploadform.sitename.data = distribusi.distribusiname
|
|
uploadform.sitename.render_kw = {"readonly": True}
|
|
uploadform.description.data = distribusi.description
|
|
uploadform.category.data = distribusi.category
|
|
uploadform.year.data = distribusi.year
|
|
uploadform.tags.data = distribusi.tags
|
|
return uploadform
|
|
|
|
|
|
def select_new_distribusi():
|
|
APP.logger.info("make a new distribusi")
|
|
select_current_distribusi("new")
|
|
|
|
|
|
def select_describe_distribusi(distribusiname):
|
|
return redirect(
|
|
url_for(
|
|
"describer.show_distribusi_files",
|
|
distribusiname=distribusiname,
|
|
)
|
|
)
|
|
|
|
|
|
def select_update_distribusi(distribusiname):
|
|
select_current_distribusi(distribusiname)
|
|
|
|
|
|
def delete_distribusi(distribusiname):
|
|
APP.logger.info(f"attempt to delete distribusi:{distribusiname}")
|
|
selectorform = SelectorForm()
|
|
try:
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
distribusi = Distribusis.query.filter_by(
|
|
distribusiname=distribusiname
|
|
).first()
|
|
if distribusi.userid is user.id:
|
|
db.session.delete(distribusi)
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
flash("An error occured !", "danger")
|
|
_delete_distribusi_files(distribusi.distribusiname)
|
|
APP.logger.info(f"deleted distribusi:{distribusiname}")
|
|
return selectorform
|
|
|
|
|
|
def _delete_distribusi_files(distribusiname):
|
|
userfolder = os.path.join("stash", distribusiname)
|
|
if os.path.exists(userfolder):
|
|
shutil.rmtree(userfolder)
|
|
cssfolder = os.path.join("themes/userthemes", distribusiname)
|
|
if os.path.exists(cssfolder):
|
|
shutil.rmtree(cssfolder)
|
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
distribusiname=distribusiname
|
|
).first()
|
|
if distribusi.publictheme is not None:
|
|
publicthemefolder = os.path.join("themes/publicthemes", distribusiname)
|
|
if os.path.exists(publicthemefolder):
|
|
shutil.rmtree(publicthemefolder)
|
|
|
|
|
|
def _delete_distribusi_database_files(distribusiname):
|
|
try:
|
|
distribusi = Distribusis.query.filter_by(
|
|
distribusiname=distribusiname
|
|
).first()
|
|
distribusi_db_files = DistribusiFiles.query.filter_by(
|
|
distribusi=distribusi
|
|
)
|
|
db.session.delete(distribusi_db_files)
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
|
|
|
|
def select_current_distribusi(distribusiname):
|
|
if not current_user.is_authenticated:
|
|
return
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
try:
|
|
user.currentdistribusi = distribusiname
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
flash("An error occured !", "danger")
|
|
|
|
|
|
def distribusi_selected():
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
if user.currentdistribusi is None:
|
|
return False
|
|
return True
|
|
|
|
|
|
def selector_visible():
|
|
has_distribusi = UserHelper.has_distribusi()
|
|
is_distribusi_selected = distribusi_selected()
|
|
if is_distribusi_selected:
|
|
return False
|
|
if not has_distribusi:
|
|
return False
|
|
return True
|
|
|
|
|
|
def render_distribusi_template(selectorform, uploadform, current_distribusi):
|
|
distribusiform = DistribusiForm()
|
|
themeform = ThemeForm()
|
|
publicthemeform = PublicThemeForm()
|
|
publicthemeform.publicthemes.choices = DistribusisInfo.public_themes()
|
|
files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
|
|
distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
|
|
|
|
# because the user has chosen to update his distribusi, we assume
|
|
# no selected css.
|
|
css_selected = False
|
|
selectorvisible = selector_visible()
|
|
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
|