From: PhiTux Date: Fri, 18 Apr 2025 17:22:41 +0000 (+0200) Subject: added template-logic (except when adding in editor) X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=2a609a2042e2f75ae7be742afd7f3e918d10b313;p=DailyTxT.git added template-logic (except when adding in editor) --- diff --git a/backend/server/routers/logs.py b/backend/server/routers/logs.py index 2616e0e..24904d3 100644 --- a/backend/server/routers/logs.py +++ b/backend/server/routers/logs.py @@ -520,4 +520,39 @@ async def removeTagFromLog(data: AddTagToLog, cookie = Depends(users.isLoggedIn) if not fileHandling.writeMonth(cookie["user_id"], data.year, data.month, content): raise HTTPException(status_code=500, detail="Failed to remove tag - error writing log") return {"success": True} - \ No newline at end of file + +@router.get("/getTemplates") +async def getTemplates(cookie = Depends(users.isLoggedIn)): + enc_key = security.get_enc_key(cookie["user_id"], cookie["derived_key"]) + + content:dict = fileHandling.getTemplates(cookie["user_id"]) + + if not 'templates' in content: + return [] + + else: + for template in content['templates']: + template['name'] = security.decrypt_text(template['name'], enc_key) + template['text'] = security.decrypt_text(template['text'], enc_key) + return content['templates'] + +class Templates(BaseModel): + templates: list[dict] + +@router.post("/saveTemplates") +async def saveTemplates(templates: Templates, cookie = Depends(users.isLoggedIn)): + enc_key = security.get_enc_key(cookie["user_id"], cookie["derived_key"]) + + content = {'templates': []} + + for template in templates.templates: + enc_name = security.encrypt_text(template["name"], enc_key) + enc_text = security.encrypt_text(template["text"], enc_key) + + new_template = {"name": enc_name, "text": enc_text} + content['templates'].append(new_template) + + if not fileHandling.writeTemplates(cookie["user_id"], content): + raise HTTPException(status_code=500, detail="Failed to write templates - error writing templates") + else: + return {"success": True} \ No newline at end of file diff --git a/backend/server/utils/fileHandling.py b/backend/server/utils/fileHandling.py index 9f78290..954e0b8 100644 --- a/backend/server/utils/fileHandling.py +++ b/backend/server/utils/fileHandling.py @@ -157,4 +157,31 @@ def writeUserSettings(user_id, content): else: with f: f.write(content) + return True + +def getTemplates(user_id): + try: + f = open(os.path.join(settings.data_path, str(user_id), "templates.json"), "r") + except FileNotFoundError: + logger.info(f"{user_id}/templates.json - File not found") + return {} + except Exception as e: + logger.exception(e) + raise HTTPException(status_code=500, detail="Internal Server Error when trying to open templates.json") + else: + with f: + s = f.read() + if s == "": + return {} + return json.loads(s) + +def writeTemplates(user_id, content): + try: + f = open(os.path.join(settings.data_path, str(user_id), "templates.json"), "w") + except Exception as e: + logger.exception(e) + return False + else: + with f: + f.write(json.dumps(content, indent=settings.indent)) return True \ No newline at end of file diff --git a/frontend/src/lib/settingsStore.js b/frontend/src/lib/settingsStore.js index d22cd09..e052fac 100644 --- a/frontend/src/lib/settingsStore.js +++ b/frontend/src/lib/settingsStore.js @@ -2,13 +2,16 @@ import {writable} from 'svelte/store'; export const readingMode = writable(false); +// old, to be deleted export const useTrianglify = writable(true); export const trianglifyOpacity = writable(0.4); export const trianglifyColor = writable(''); export const backgroundColor = writable(''); +//========= export const settings = writable({}); export const tempSettings = writable({}); +// should be separate, since it interacts with localStorage export const autoLoadImagesThisDevice = writable(JSON.parse(localStorage.getItem('autoLoadImagesThisDevice'))); \ No newline at end of file diff --git a/frontend/src/lib/templateStore.js b/frontend/src/lib/templateStore.js new file mode 100644 index 0000000..6f04ae5 --- /dev/null +++ b/frontend/src/lib/templateStore.js @@ -0,0 +1,3 @@ +import { writable } from "svelte/store"; + +export const templates = writable([]); \ No newline at end of file diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index fed0474..807a541 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -1,5 +1,5 @@