189 lines
6.2 KiB
Python
189 lines
6.2 KiB
Python
import json
|
|
import os
|
|
from werkzeug.utils import secure_filename
|
|
from app.forms import *
|
|
from PIL import Image
|
|
from pathlib import Path, PurePath, PurePosixPath
|
|
|
|
class DataSite:
|
|
user_folder = None
|
|
username = str()
|
|
datas = str()
|
|
static_path = None
|
|
trash = list()
|
|
filej= None
|
|
|
|
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 = Path(PurePosixPath(self.user_folder.joinpath('public')))
|
|
self.static_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
def loadFile(self):
|
|
if not self.filej.isfile():
|
|
raise FileNotFoundError(f"Le fichier ident.json n'existe pas : {self.filej}")
|
|
|
|
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, image, form):
|
|
img = dict()
|
|
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()
|
|
|
|
img['title'] = ""
|
|
img['subtitle']=""
|
|
img['text'] = ""
|
|
img['category'] = ""
|
|
img['file'] = image_uploaded
|
|
|
|
return img
|
|
|
|
|
|
def delImg(self, bloc, img):
|
|
|
|
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:
|
|
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:
|
|
try:
|
|
remove(self.static_path+'img/portfolio/fullsize/'+file)
|
|
except FileNotFoundError:
|
|
print (f'Le fichier {file} n\'existe pas')
|
|
if path.exists((self.static_path+'img/portfolio/thumbnails/'+file)):
|
|
remove(self.static_path+'img/portfolio/thumbnails/'+file)
|
|
self.trash.clear()
|