import json import os from werkzeug.utils import secure_filename from app.forms import * from PIL import Image from os import remove, path class DataSite: datas = "" static_path = "" trash = list() filej= "" def __init__(self, username, users_folder ): self.filej = os.path.join(users_folder, username,'ident.json') with open(self.filej, 'r') as f: self.datas = json.load(f) self.static_path = os.path.join(users_folder, username, 'public') 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)) 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 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): img = dict() img['title'] = "" img['subtitle']="" img['text'] = "" img['category'] = "" img['file']=image_uploaded self.datas['blocs'][bloc][image_uploaded]=img 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 def saveJson(self): remove(self.filej) with open (self.filej, 'w') as f: f.write(json.dumps(self.datas, indent=5)) def delImg(self, img): try: images = self.datas['images'] images.pop(img) except KeyError: print("Le fichier n'est pas dans la liste") else: self.trash.append(img) 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()