2022-03-19 16:19:51 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2024-05-19 11:13:21 +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
|
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-05-19 11:29:40 +02:00
|
|
|
from app import db
|
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
|
|
|
|
from distribusikan.forms.uploadform import UploadForm
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.distribusi_model import Distribusis
|
2024-11-25 21:21:31 +01:00
|
|
|
from models.distribusi_file_model import DistribusiFiles
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.user_model import User
|
2024-04-28 13:15:23 +02:00
|
|
|
|
2022-03-23 21:30:59 +01:00
|
|
|
# UserPengguna
|
2022-03-28 20:16:34 +02:00
|
|
|
from statuspengguna.helper import UserHelper
|
2022-03-23 21:30:59 +01:00
|
|
|
|
2022-03-19 16:19:51 +01:00
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def distribusi_selector():
|
2024-05-28 23:02:17 +02:00
|
|
|
uploadform = UploadForm()
|
|
|
|
selectorform = SelectorForm()
|
2024-06-17 22:36:23 +02:00
|
|
|
selectorform.distribusis.choices = DistribusisInfo.user_distribusinames()
|
2024-05-28 23:02:17 +02:00
|
|
|
current_distribusi = UserHelper.current_distribusi()
|
|
|
|
if selectorform.validate_on_submit():
|
|
|
|
if selectorform.new.data:
|
2024-06-17 22:36:23 +02:00
|
|
|
select_new_distribusi()
|
2024-05-28 23:02:17 +02:00
|
|
|
if selectorform.describe.data:
|
2024-06-17 22:36:23 +02:00
|
|
|
return select_describe_distribusi(selectorform.distribusis.data)
|
2024-05-28 23:02:17 +02:00
|
|
|
if selectorform.delete.data:
|
2024-06-17 22:36:23 +02:00
|
|
|
selectorform = delete_distribusi(selectorform.distribusis.data)
|
2024-05-28 23:02:17 +02:00
|
|
|
selectorform.distribusis.choices = (
|
2024-06-17 22:36:23 +02:00
|
|
|
DistribusisInfo.user_distribusinames()
|
2024-05-28 23:02:17 +02:00
|
|
|
)
|
|
|
|
if selectorform.update.data:
|
2024-06-17 22:36:23 +02:00
|
|
|
select_update_distribusi(selectorform.distribusis.data)
|
2024-05-28 23:02:17 +02:00
|
|
|
current_distribusi = UserHelper.current_distribusi()
|
2024-06-27 11:03:35 +02:00
|
|
|
uploadform = auto_fill_in_upload_form(
|
|
|
|
uploadform, current_distribusi
|
|
|
|
)
|
2024-05-28 23:02:17 +02:00
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
return render_distribusi_template(
|
2024-05-28 23:02:17 +02:00
|
|
|
selectorform, uploadform, current_distribusi
|
|
|
|
)
|
2022-03-23 21:30:59 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def auto_fill_in_upload_form(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}
|
2024-06-17 22:36:23 +02:00
|
|
|
uploadform.description.data = distribusi.description
|
2024-05-28 23:02:17 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def select_new_distribusi():
|
2024-05-28 23:02:17 +02:00
|
|
|
print("make a new distribusi")
|
2024-06-17 22:36:23 +02:00
|
|
|
select_current_distribusi("new")
|
2022-03-19 16:19:51 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def select_describe_distribusi(distribusiname):
|
2024-05-28 23:02:17 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"describer.show_distribusi_files",
|
|
|
|
distribusiname=distribusiname,
|
|
|
|
)
|
|
|
|
)
|
2024-05-19 11:13:21 +02:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def select_update_distribusi(distribusiname):
|
2024-05-28 23:02:17 +02:00
|
|
|
print(f"Update this distribusi {distribusiname}")
|
2024-06-17 22:36:23 +02:00
|
|
|
select_current_distribusi(distribusiname)
|
2022-03-19 16:19:51 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def delete_distribusi(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()
|
|
|
|
# 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")
|
2024-11-25 21:21:31 +01:00
|
|
|
_delete_distribusi_files(distribusi.distribusiname)
|
2024-05-28 23:02:17 +02:00
|
|
|
return selectorform
|
2022-03-19 16:19:51 +01:00
|
|
|
|
|
|
|
|
2024-11-25 21:21:31 +01:00
|
|
|
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)
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def select_current_distribusi(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
|
|
|
|
|
|
|
|
2024-06-17 23:06:51 +02:00
|
|
|
def distribusi_selected():
|
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
|
|
|
|
|
|
|
|
2024-06-17 23:06:51 +02:00
|
|
|
def selector_visible():
|
2024-05-28 23:02:17 +02:00
|
|
|
has_distribusi = UserHelper.has_distribusi()
|
2024-06-17 23:06:51 +02:00
|
|
|
is_distribusi_selected = distribusi_selected()
|
|
|
|
if is_distribusi_selected:
|
2024-05-28 23:02:17 +02:00
|
|
|
return False
|
|
|
|
if not has_distribusi:
|
|
|
|
return False
|
|
|
|
return True
|
2022-03-23 21:30:59 +01:00
|
|
|
|
|
|
|
|
2024-06-17 22:36:23 +02:00
|
|
|
def render_distribusi_template(selectorform, uploadform, current_distribusi):
|
2024-05-28 23:02:17 +02:00
|
|
|
distribusiform = DistribusiForm()
|
|
|
|
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
|
|
|
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
|
2024-06-17 23:06:51 +02:00
|
|
|
selectorvisible = selector_visible()
|
2024-05-28 23:02:17 +02:00
|
|
|
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
|