Big commit i'm sorry ...
This commit is contained in:
@@ -3,105 +3,179 @@ import os
|
||||
from werkzeug.utils import secure_filename
|
||||
from app.forms import *
|
||||
from PIL import Image
|
||||
from os import remove, path
|
||||
|
||||
from pathlib import Path, PurePath, PurePosixPath
|
||||
|
||||
class DataSite:
|
||||
|
||||
datas = ""
|
||||
static_path = ""
|
||||
user_folder = None
|
||||
username = str()
|
||||
datas = str()
|
||||
static_path = None
|
||||
trash = list()
|
||||
filej= ""
|
||||
filej= None
|
||||
|
||||
def __init__(self, username, users_folder ):
|
||||
self.filej = os.path.join(users_folder, username,'ident.json')
|
||||
|
||||
with open(self.filej, 'r') as f:
|
||||
def __init__(self, username :str , users_folder :str ):
|
||||
self.username = username
|
||||
self.user_folder = PurePosixPath(users_folder).joinpath(username)
|
||||
self.filej = Path(PurePosixPath(self.user_folder).joinpath('ident.json'))
|
||||
|
||||
|
||||
with self.filej.open(newline='', mode='r', encoding='utf-8') as f:
|
||||
self.datas = json.load(f)
|
||||
self.static_path = os.path.join(users_folder, username, 'public')
|
||||
self.static_path = Path(PurePosixPath(self.user_folder.joinpath('public')))
|
||||
self.static_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def loadFile(self):
|
||||
with open(self.filej, 'r') as f:
|
||||
self.datas = json.load(f)
|
||||
|
||||
def loadFile_tmp(self):
|
||||
with open(self.filej_tmp, 'r') as f:
|
||||
self.datas = json.load(f)
|
||||
|
||||
|
||||
def updateBloc(bloc, form):
|
||||
bloc['name'] = form.name.data
|
||||
bloc['bg_color'] = form.bg_color.data
|
||||
bloc['fg_color']=form.fg_color.data
|
||||
bloc['title'] = form.title.data
|
||||
bloc['text'] = form.text.data
|
||||
bloc['text_button']=form.text_button.data
|
||||
bloc['link'] = form.link.data
|
||||
if form.image.data :
|
||||
f=form.image.data
|
||||
filename = secure_filename(f.filename)
|
||||
extension = filename.rsplit('.', 1)[1].lower()
|
||||
f.save(os.path.join(
|
||||
self.static_path, 'img','bg_custom.'+ extension))
|
||||
if not self.filej.isfile():
|
||||
raise FileNotFoundError(f"Le fichier ident.json n'existe pas : {self.filej}")
|
||||
|
||||
bloc['bg_img']='bg_custom.'+extension
|
||||
bloc['email'] = form.email.data
|
||||
bloc['address']=form.address.data
|
||||
bloc['phone'] = form.phone.data
|
||||
bloc['postal_code'] = form.postal_code.data
|
||||
bloc['city'] = form.city.data
|
||||
with self.filej.open(newline='', mode='r', encoding='utf-8') as f:
|
||||
self.datas = json.load(f)
|
||||
|
||||
|
||||
|
||||
|
||||
def getBlocs(self, page: str):
|
||||
|
||||
if page != '':
|
||||
page = Path(self.user_folder.joinpath(page, 'page.json'))
|
||||
datas = ""
|
||||
if not page.exists():
|
||||
raise FileNotFoundError(f"Le fichier page.json n'existe pas : {page}")
|
||||
|
||||
datas = json.loads(page.read_text())
|
||||
else:
|
||||
datas = self.datas
|
||||
|
||||
return datas
|
||||
|
||||
|
||||
def updateBloc(self, bloc, form, files=None):
|
||||
print (form)
|
||||
for field in form():
|
||||
print(field)
|
||||
|
||||
if files != None:
|
||||
f = files['image']
|
||||
filename = secure_filename(f.filename)
|
||||
extension = filename.rsplit('.', 1)[1].lower()
|
||||
bg_custom = 'bg_custom_'+bloc['name']+'.'+extension
|
||||
f.save(PurePath.joinpath(
|
||||
self.static_path, 'img', bg_custom))
|
||||
|
||||
bloc['bg_img'] = bg_custom
|
||||
|
||||
return bloc
|
||||
|
||||
def updateImg(self, bloc, img, form):
|
||||
filename = img['file']
|
||||
img['title'] = form.title.data
|
||||
img['subtitle'] = form.subtitle.data
|
||||
img['description'] = form.description.data
|
||||
img['category'] = form.category.data
|
||||
self.datas['blocs'][bloc][filename]=img
|
||||
|
||||
def updateImg_tmp(self, bloc, img, form):
|
||||
filename = img['file']
|
||||
img['title'] = form.title.data
|
||||
img['subtitle'] = form.subtitle.data
|
||||
img['description'] = form.description.data
|
||||
img['category'] = form.category.data
|
||||
self.datas['blocs'][bloc][filename]=img
|
||||
|
||||
|
||||
def addImg(self, bloc, image_uploaded):
|
||||
def updateImg(self, image, form):
|
||||
img = dict()
|
||||
img['title'] = ""
|
||||
img['subtitle']=""
|
||||
img['text'] = ""
|
||||
img['category'] = ""
|
||||
img['file']=image_uploaded
|
||||
self.datas['blocs'][bloc][image_uploaded]=img
|
||||
img['title'] = form.title.data
|
||||
img['subtitle'] = form.subtitle.data
|
||||
img['description'] = form.description.data
|
||||
img['category'] = form.category.data
|
||||
img['file'] = image
|
||||
return img
|
||||
|
||||
def addImg(self, image_uploaded):
|
||||
img = dict()
|
||||
|
||||
def addImg_tmp(self, bloc, image_uploaded):
|
||||
img = dict()
|
||||
img['title'] = ""
|
||||
img['subtitle']=""
|
||||
img['text'] = ""
|
||||
img['category'] = ""
|
||||
img['file']=image_uploaded
|
||||
self.datas_tmp['blocs'][bloc][image_uploaded]=img
|
||||
img['file'] = image_uploaded
|
||||
|
||||
return img
|
||||
|
||||
|
||||
def saveJson(self):
|
||||
remove(self.filej)
|
||||
with open (self.filej, 'w') as f:
|
||||
f.write(json.dumps(self.datas, indent=5))
|
||||
def delImg(self, bloc, img):
|
||||
|
||||
def delImg(self, img):
|
||||
try:
|
||||
images = self.datas['images']
|
||||
images.pop(img)
|
||||
except KeyError:
|
||||
print("Le fichier n'est pas dans la liste")
|
||||
if bloc['type'] == 'gallery':
|
||||
img_file = Path(PurePosixPath(self.user_folder).joinpath('public', 'img', bloc[img]['file'] ))
|
||||
img_thumb_file = Path(PurePosixPath(self.user_folder).joinpath('public','img', 'thumbnails', bloc[img]['file']))
|
||||
if img_file.exists():
|
||||
img_file.unlink()
|
||||
|
||||
if img_thumb_file.exists():
|
||||
img_thumb_file.unlink()
|
||||
|
||||
bloc.pop(img)
|
||||
else:
|
||||
self.trash.append(img)
|
||||
img_file = Path(PurePosixPath(self.user_folder).joinpath('public', 'img', img ))
|
||||
|
||||
return bloc
|
||||
|
||||
|
||||
def saveMenu(self, menu:dict):
|
||||
self.datas['menu'] = menu
|
||||
self.filej.write_text(json.dumps(self.datas, indent=5))
|
||||
|
||||
|
||||
def saveBlocs(self, page='', blocs=dict()):
|
||||
if page != '':
|
||||
datas = dict()
|
||||
datas['blocs'] = blocs
|
||||
|
||||
page_info = Path(PurePosixPath(self.user_folder).joinpath(page, 'page.json'))
|
||||
|
||||
if not page_info.exists():
|
||||
raise FileNotFoundError(f"Le fichier page.json n'existe pas : {page}")
|
||||
|
||||
page_info.write_text(json.dumps(datas, indent=3))
|
||||
else:
|
||||
self.datas['blocs']= blocs
|
||||
self.filej.write_text(json.dumps(self.datas, indent=5))
|
||||
|
||||
|
||||
def getPages(self):
|
||||
mypages = list()
|
||||
folders = Path(self.user_folder)
|
||||
for folder in folders.iterdir():
|
||||
if folder.name != 'public' and folder.is_dir():
|
||||
mypages.append(folder.name)
|
||||
|
||||
return mypages
|
||||
|
||||
def new_page(self, pageName:str, pageType:str):
|
||||
page_folder = Path(PurePosixPath(self.user_folder).joinpath(pageName))
|
||||
page = Path(PurePosixPath(self.user_folder).joinpath(pageName, 'page.json'))
|
||||
page_type = Path('./app/templates/themes/'+self.datas['config']['theme']+'/pages/'+pageType+'.json')
|
||||
|
||||
if not page_type.exists():
|
||||
raise FileNotFoundError(f"Le type de page n'existe pas : {page_type}")
|
||||
else:
|
||||
page_folder.mkdir()
|
||||
page.write_text(page_type.read_text())
|
||||
|
||||
if not page.exists():
|
||||
raise FileNotFoundError(f"La page n'a pas pu etre créé : {page}")
|
||||
|
||||
return True
|
||||
|
||||
def rm_page(self, pageName :str):
|
||||
page_folder = Path(PurePosixPath(self.user_folder).joinpath(pageName))
|
||||
page = Path(PurePosixPath(self.user_folder).joinpath(pageName, 'page.json'))
|
||||
|
||||
#First delete the json page
|
||||
try:
|
||||
page.unlink()
|
||||
print(f"Successfully deleted: {page}")
|
||||
except FileNotFoundError:
|
||||
print(f"File not found: {page}. Nothing to delete.")
|
||||
# Delete the empty directory
|
||||
if page_folder.is_dir():
|
||||
try:
|
||||
page_folder.rmdir()
|
||||
print(f"Successfully removed empty directory: {page_folder}")
|
||||
except OSError as e:
|
||||
print(f"Error removing directory (might not be empty): {e}")
|
||||
|
||||
|
||||
def writeHTML(self, pageName :str, htmlExport :str):
|
||||
public_folder = Path(PurePosixPath(self.user_folder).joinpath('public'))
|
||||
pageName = pageName+'.html'
|
||||
page = Path(PurePosixPath(self.user_folder).joinpath('public', pageName))
|
||||
page.write_text(htmlExport)
|
||||
|
||||
|
||||
def emptyTrash(self):
|
||||
for file in self.trash:
|
||||
|
||||
Reference in New Issue
Block a user