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: filej = "" datas = "" static_path ="" trash = list() def __init__(self, filej, static_path): self.filej = filej self.static_path = static_path def loadFile(self): with open(self.filej, 'r') as f: self.datas = json.load(f) def updateDatas(self, bloc, form): match bloc['type']: case "presentation": self.datas['blocs'][bloc['name']]['title'] = form.title.data self.datas['blocs'][bloc['name']]['text'] = form.text.data self.datas['blocs'][bloc['name']]['text_button']=form.text_button.data self.datas['blocs'][bloc['name']]['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)) self.datas['blocs'][bloc['name']]['bg_img']='bg_custom.'+extension case "text" : self.datas['blocs'][bloc['name']]['title']=form.title.data self.datas['blocs'][bloc['name']]['text']=form.text.data case "external_link" : self.datas['blocs'][bloc['name']]['title'] = form.title.data self.datas['blocs'][bloc['name']]['text'] = form.text.data self.datas['blocs'][bloc['name']]['link'] = form.link.data self.datas['blocs'][bloc['name']]['text_button']=form.text_button.data case 'contact': self.datas['blocs'][bloc['name']]['title'] = form.title.data self.datas['blocs'][bloc['name']]['text'] = form.text.data self.datas['blocs'][bloc['name']]['email'] = form.email.data self.datas['blocs'][bloc['name']]['phone'] = form.phone.data self.datas['blocs'][bloc['name']]['postal_code'] = form.postal_code.data self.datas['blocs'][bloc['name']]['city'] = form.city.data 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 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 saveJson(self, datas): remove(self.filej) with open (self.filej, 'w') as f: f.write(json.dumps(datas, indent=4)) 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()