2022-03-04 19:08:55 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
from flask import flash
|
|
|
|
from flask_login import current_user
|
2022-03-04 16:41:14 +01:00
|
|
|
|
|
|
|
from usermodel import User
|
2022-03-04 19:08:55 +01:00
|
|
|
from distribusimodel import Distribusis
|
2022-03-04 16:41:14 +01:00
|
|
|
|
|
|
|
from forms.selectorform import SelectorForm
|
|
|
|
|
|
|
|
from statuspengguna.helper import HasDistribusi
|
|
|
|
from statuspengguna.helper import DistribusiSelected
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
|
|
|
|
|
|
|
|
def SelectNewDistribusi():
|
|
|
|
print("make a new distribusi")
|
2022-03-04 19:08:55 +01:00
|
|
|
selectorform = SelectorForm()
|
2022-03-04 16:41:14 +01:00
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
try:
|
|
|
|
user.currentdistribusi = "new"
|
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
2022-03-04 19:08:55 +01:00
|
|
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
2022-03-04 16:41:14 +01:00
|
|
|
flash("An error occured !", "danger")
|
2022-03-04 19:08:55 +01:00
|
|
|
return selectorform
|
2022-03-04 16:41:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def SelectUpdateDistribusi(distribusiname):
|
|
|
|
print(f"Update this distribusi {distribusiname}")
|
2022-03-04 19:08:55 +01:00
|
|
|
selectorform = SelectorForm()
|
2022-03-04 16:41:14 +01:00
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
try:
|
|
|
|
user.currentdistribusi = distribusiname
|
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
2022-03-04 19:08:55 +01:00
|
|
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
2022-03-04 16:41:14 +01:00
|
|
|
flash("An error occured !", "danger")
|
2022-03-04 19:08:55 +01:00
|
|
|
return selectorform
|
2022-03-04 16:41:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def DeleteDistribusi(distribusiname):
|
|
|
|
print(f"delete this distribusi {distribusiname}")
|
2022-03-04 19:08:55 +01:00
|
|
|
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)
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
|
|
|
selectorform.distribusis.errors.append("Unknown error occured!")
|
|
|
|
flash("An error occured !", "danger")
|
|
|
|
return selectorform
|
2022-03-04 16:41:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def SelectorVisible():
|
|
|
|
has_distribusi = HasDistribusi()
|
|
|
|
distribusi_selected = DistribusiSelected()
|
|
|
|
if distribusi_selected:
|
|
|
|
return False
|
|
|
|
if not has_distribusi:
|
|
|
|
return False
|
|
|
|
return True
|