2022-01-23 14:36:23 +01:00
|
|
|
import os
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2024-04-24 23:05:36 +02:00
|
|
|
from flask import flash
|
|
|
|
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
|
|
|
)
|
2022-01-23 17:33:50 +01:00
|
|
|
|
2024-04-28 13:04:07 +02:00
|
|
|
from app import db
|
|
|
|
from distribusikan.distribusisinfo import DistribusisInfo
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.distribusi_model import Distribusis
|
|
|
|
from models.user_model import User
|
2024-04-28 13:04:07 +02:00
|
|
|
|
2022-02-13 22:24:56 +01:00
|
|
|
|
2022-03-28 20:16:34 +02:00
|
|
|
class UserHelper:
|
2024-05-28 23:02:17 +02:00
|
|
|
def is_zip_uploaded(distribusiname):
|
|
|
|
userfolder = os.path.join("stash", distribusiname)
|
|
|
|
if os.path.exists(userfolder):
|
|
|
|
zipfilename = "{}.zip".format(distribusiname)
|
|
|
|
if os.path.exists(os.path.join(userfolder, zipfilename)):
|
|
|
|
print("folder with zipfile found, file uploaded")
|
|
|
|
return True
|
|
|
|
print("distribusi folder has no zipfile")
|
|
|
|
return False
|
2022-02-18 09:58:24 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def is_distribusi_live(distribusiname):
|
|
|
|
userfolder = os.path.join("stash", distribusiname)
|
|
|
|
if os.path.exists(userfolder):
|
|
|
|
zipfilename = "{}.zip".format(distribusiname)
|
|
|
|
if os.path.exists(os.path.join(userfolder, zipfilename)):
|
|
|
|
print("folder with zipfile found, file uploaded")
|
|
|
|
return False
|
|
|
|
if len(os.listdir(userfolder)) > 0:
|
|
|
|
return True
|
|
|
|
print("distribusi folder is empty")
|
|
|
|
return False
|
2022-02-18 09:58:24 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def is_css_selected(distribusiname):
|
|
|
|
userfolder = os.path.join("themes/userthemes", distribusiname)
|
|
|
|
if os.path.exists(userfolder):
|
|
|
|
for file in os.listdir(userfolder):
|
|
|
|
if file.endswith(".css"):
|
|
|
|
return True
|
|
|
|
print("distribusi folder has no css file")
|
|
|
|
return False
|
2022-03-04 16:41:14 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def has_distribusi():
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
distribusi = Distribusis.query.filter_by(userid=user.id).first()
|
|
|
|
if distribusi is None:
|
|
|
|
print("no distribusi found")
|
|
|
|
return False
|
|
|
|
return True
|
2022-03-18 17:32:49 +01:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def current_distribusi():
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
if user.currentdistribusi is None:
|
|
|
|
return "None"
|
|
|
|
return user.currentdistribusi
|
2022-03-28 20:16:34 +02:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def reset_user_state():
|
|
|
|
"""reset user state upon visiting index, so that distribusi workflow can
|
|
|
|
be done correctly"""
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
user.currentdistribusi = None
|
|
|
|
db.session.commit()
|
|
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
|
|
db.session.rollback()
|
|
|
|
flash("An error occured !", "danger")
|
2022-03-28 20:16:34 +02:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
def distribusi_limit_reached():
|
|
|
|
user = User.query.filter_by(email=current_user.email).first()
|
|
|
|
distribusiamount = len(DistribusisInfo.getuserdistribusis(user.email))
|
|
|
|
if distribusiamount > 19:
|
|
|
|
print("user already has 20 distribusis")
|
|
|
|
return True
|
|
|
|
return False
|