forked from crunk/distribusi-verse
crunk
3 years ago
9 changed files with 209 additions and 84 deletions
@ -0,0 +1,85 @@ |
|||
import os |
|||
import shutil |
|||
from flask import flash |
|||
from flask_login import current_user |
|||
from sqlalchemy.exc import ( |
|||
DataError, |
|||
DatabaseError, |
|||
InterfaceError, |
|||
InvalidRequestError, |
|||
) |
|||
|
|||
from usermodel import User |
|||
from distribusimodel import Distribusis |
|||
|
|||
|
|||
from forms.selectorform import SelectorForm |
|||
from statuspengguna.helper import HasDistribusi |
|||
from app import db |
|||
|
|||
|
|||
def SelectNewDistribusi(): |
|||
print("make a new distribusi") |
|||
selectorform = SelectorForm() |
|||
SelectCurrentDistribusi("new") |
|||
return selectorform |
|||
|
|||
|
|||
def SelectUpdateDistribusi(distribusiname): |
|||
print(f"Update this distribusi {distribusiname}") |
|||
selectorform = SelectorForm() |
|||
SelectCurrentDistribusi(distribusiname) |
|||
return selectorform |
|||
|
|||
|
|||
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) |
|||
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 |
@ -0,0 +1,24 @@ |
|||
from flask_login import current_user |
|||
|
|||
from usermodel import User |
|||
from distribusimodel import Distribusis |
|||
|
|||
|
|||
class DistribusisInfo: |
|||
def userdistribusis(): |
|||
distribusinames = [] |
|||
user = User.query.filter_by(email=current_user.email).first() |
|||
for distribusi in Distribusis.query.filter_by(userid=user.id).all(): |
|||
distribusinames.append(distribusi.distribusiname) |
|||
return distribusinames |
|||
|
|||
def publicthemes(): |
|||
publicthemes = [] |
|||
distribusis = Distribusis.query.filter( |
|||
Distribusis.publictheme.isnot(None) |
|||
).all() |
|||
for distribusi in distribusis: |
|||
publicthemes.append( |
|||
f"{distribusi.publictheme} used in {distribusi.distribusiname}" |
|||
) |
|||
return publicthemes |
@ -0,0 +1,66 @@ |
|||
import os |
|||
import shutil |
|||
from flask import render_template |
|||
from statuspengguna.helper import ( |
|||
IsZipUploaded, |
|||
IsCssSelected, |
|||
CurrentDistribusi, |
|||
IsDistribusiLive, |
|||
) |
|||
from distribusisinfo import DistribusisInfo |
|||
|
|||
from forms.uploadform import UploadForm |
|||
from forms.distribusiform import DistribusiForm |
|||
from forms.themeform import ThemeForm |
|||
from forms.selectorform import SelectorForm |
|||
|
|||
|
|||
def ThemeSelector(): |
|||
themeform = ThemeForm() |
|||
themeform.publicthemes.choices = DistribusisInfo.publicthemes() |
|||
current_distribusi = CurrentDistribusi() |
|||
if themeform.validate_on_submit(): |
|||
newcssfolder = os.path.join("themes/userthemes", current_distribusi) |
|||
if not os.path.exists(newcssfolder): |
|||
os.mkdir(newcssfolder) |
|||
if themeform.theme.data: |
|||
copycssfile = os.path.join( |
|||
"themes", |
|||
f"{themeform.theme.data}.css", |
|||
) |
|||
if themeform.publicthemes.data: |
|||
publictheme = themeform.publicthemes.data |
|||
copycssfile = os.path.join( |
|||
"themes/publicthemes", |
|||
f"{distribusiname}", |
|||
f"{themename}.css", |
|||
) |
|||
shutil.copy(copycssfile, newcssfolder) |
|||
|
|||
return RenderDistribusiTemplate(current_distribusi) |
|||
|
|||
|
|||
def RenderDistribusiTemplate(current_distribusi): |
|||
uploadform = UploadForm() |
|||
distribusiform = DistribusiForm() |
|||
themeform = ThemeForm() |
|||
themeform.publicthemes.choices = DistribusisInfo.publicthemes() |
|||
selectorform = SelectorForm() |
|||
|
|||
files_uploaded = IsZipUploaded(current_distribusi) |
|||
distribusi_live = IsDistribusiLive(current_distribusi) |
|||
css_selected = IsCssSelected(current_distribusi) |
|||
selectorvisible = False |
|||
|
|||
template = render_template( |
|||
"distribusi.html", |
|||
uploadform=uploadform, |
|||
distribusiform=distribusiform, |
|||
themeform=themeform, |
|||
selectorform=selectorform, |
|||
files_uploaded=files_uploaded, |
|||
distribusi_live=distribusi_live, |
|||
css_selected=css_selected, |
|||
selectorvisible=selectorvisible, |
|||
) |
|||
return template |
Loading…
Reference in new issue