distribusi-verse/verse/distribusikan/distribusiselector.py

173 lines
5.8 KiB
Python
Raw Normal View History

2022-03-19 16:19:51 +01:00
import os
import shutil
2024-04-21 20:44:08 +02:00
from flask import flash, render_template, redirect, url_for
2024-04-24 23:05:36 +02:00
from flask_login import current_user
from sqlalchemy.exc import (
2024-05-28 23:02:17 +02:00
DatabaseError,
DataError,
InterfaceError,
InvalidRequestError,
)
2024-04-28 13:04:07 +02:00
2024-05-19 11:29:40 +02:00
from app import db
2024-04-28 13:04:07 +02:00
from distribusikan.distribusisinfo import DistribusisInfo
2022-03-23 21:30:59 +01:00
from forms.distribusiform import DistribusiForm
from forms.publicthemeform import PublicThemeForm
2024-04-21 20:44:08 +02:00
from forms.selectorform import SelectorForm
from forms.themeform import ThemeForm
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
2022-03-23 21:30:59 +01:00
# UserPengguna
from statuspengguna.helper import UserHelper
2022-03-23 21:30:59 +01:00
2022-03-19 16:19:51 +01:00
def DistribusiSelector():
2024-05-28 23:02:17 +02:00
uploadform = UploadForm()
selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
current_distribusi = UserHelper.current_distribusi()
if selectorform.validate_on_submit():
if selectorform.new.data:
SelectNewDistribusi()
if selectorform.describe.data:
return SelectDescribeDistribusi(selectorform.distribusis.data)
if selectorform.delete.data:
selectorform = DeleteDistribusi(selectorform.distribusis.data)
selectorform.distribusis.choices = (
DistribusisInfo.userdistribusinames()
)
if selectorform.update.data:
SelectUpdateDistribusi(selectorform.distribusis.data)
current_distribusi = UserHelper.current_distribusi()
uploadform = AutoFillInUploadForm(uploadform, current_distribusi)
return RenderDistribusiTemplate(
selectorform, uploadform, current_distribusi
)
2022-03-23 21:30:59 +01:00
def AutoFillInUploadForm(uploadform, current_distribusi):
2024-05-28 23:02:17 +02:00
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
uploadform.sitename.data = distribusi.distribusiname
uploadform.sitename.render_kw = {"readonly": True}
uploadform.category.data = distribusi.category
uploadform.year.data = distribusi.year
uploadform.tags.data = distribusi.tags
return uploadform
2022-03-23 21:30:59 +01:00
2022-03-19 16:19:51 +01:00
def SelectNewDistribusi():
2024-05-28 23:02:17 +02:00
print("make a new distribusi")
SelectCurrentDistribusi("new")
2022-03-19 16:19:51 +01:00
def SelectDescribeDistribusi(distribusiname):
2024-05-28 23:02:17 +02:00
return redirect(
url_for(
"describer.show_distribusi_files",
distribusiname=distribusiname,
)
)
2022-03-19 16:19:51 +01:00
def SelectUpdateDistribusi(distribusiname):
2024-05-28 23:02:17 +02:00
print(f"Update this distribusi {distribusiname}")
SelectCurrentDistribusi(distribusiname)
2022-03-19 16:19:51 +01:00
def DeleteDistribusi(distribusiname):
2024-05-28 23:02:17 +02:00
print(f"delete this 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()
userfolder = os.path.join("stash", distribusi.distribusiname)
if os.path.exists(userfolder):
shutil.rmtree(userfolder)
cssfolder = os.path.join(
"themes/userthemes", distribusi.distribusiname
)
if os.path.exists(cssfolder):
shutil.rmtree(cssfolder)
if distribusi.publictheme is not None:
publicthemefolder = os.path.join(
"themes/publicthemes", distribusi.distribusiname
)
if os.path.exists(publicthemefolder):
shutil.rmtree(publicthemefolder)
# SelectField error is list is a tuple?? why??
# selectorform.distribusis.errors.append("Distribusi deleted!")
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
# selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
return selectorform
2022-03-19 16:19:51 +01:00
def SelectCurrentDistribusi(distribusiname):
2024-05-28 23:02:17 +02:00
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")
2022-03-19 16:19:51 +01:00
def DistribusiSelected():
2024-05-28 23:02:17 +02:00
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return False
return True
2022-03-19 16:19:51 +01:00
def SelectorVisible():
2024-05-28 23:02:17 +02:00
has_distribusi = UserHelper.has_distribusi()
distribusi_selected = DistribusiSelected()
if distribusi_selected:
return False
if not has_distribusi:
return False
return True
2022-03-23 21:30:59 +01:00
def RenderDistribusiTemplate(selectorform, uploadform, current_distribusi):
2024-05-28 23:02:17 +02:00
distribusiform = DistribusiForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
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 = SelectorVisible()
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