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.

167 lines
5.4 KiB

import os
import shutil
from flask import flash, render_template
from flask_login import current_user
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from usermodel import User
from distribusimodel import Distribusis
from distribusisinfo import DistribusisInfo
from forms.selectorform import SelectorForm
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
from forms.publicthemeform import PublicThemeForm
# UserPengguna
from statuspengguna.helper import (
IsZipUploaded,
CurrentDistribusi,
HasDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from app import db
def DistribusiSelector():
uploadform = UploadForm()
selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
current_distribusi = CurrentDistribusi()
if selectorform.validate_on_submit():
if selectorform.new.data:
SelectNewDistribusi()
if selectorform.delete.data:
selectorform = DeleteDistribusi(selectorform.distribusis.data)
selectorform.distribusis.choices = (
DistribusisInfo.userdistribusinames()
)
if selectorform.update.data:
SelectUpdateDistribusi(selectorform.distribusis.data)
current_distribusi = CurrentDistribusi()
uploadform = AutoFillInUploadForm(uploadform, current_distribusi)
return RenderDistribusiTemplate(
selectorform, uploadform, current_distribusi
)
def AutoFillInUploadForm(uploadform, current_distribusi):
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
uploadform.sitename.data = distribusi.distribusiname
uploadform.sitename.render_kw = {"readonly": True}
uploadform.term.data = distribusi.term
uploadform.course.data = distribusi.course
uploadform.academicyear.data = distribusi.year
uploadform.tags.data = distribusi.tags
return uploadform
def SelectNewDistribusi():
print("make a new distribusi")
SelectCurrentDistribusi("new")
def SelectUpdateDistribusi(distribusiname):
print(f"Update this distribusi {distribusiname}")
SelectCurrentDistribusi(distribusiname)
def DeleteDistribusi(distribusiname):
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)
shutil.rmtree(userfolder)
cssfolder = os.path.join(
"themes/userthemes", distribusi.distribusiname
)
shutil.rmtree(cssfolder)
if distribusi.publictheme is not None:
publicthemefolder = os.path.join(
"themes/publicthemes", distribusi.distribusiname
)
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
def SelectCurrentDistribusi(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 DistribusiSelected():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return False
return True
def SelectorVisible():
has_distribusi = HasDistribusi()
distribusi_selected = DistribusiSelected()
if distribusi_selected:
return False
if not has_distribusi:
return False
return True
def RenderDistribusiTemplate(selectorform, uploadform, current_distribusi):
distribusiform = DistribusiForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
# because the user has chosen to update his distribusi, we assume
# no selected css.
css_selected = False
selectorvisible = SelectorVisible()
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