From dbb418e6dd4115517e4d2fcf6c512d4081f3ab0c Mon Sep 17 00:00:00 2001 From: rohanshiva Date: Tue, 1 Jun 2021 14:38:11 -0500 Subject: [PATCH 1/2] Migrate Notes to Deta Drive --- .gitignore | 2 ++ main.py | 7 +++++- note.py | 69 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39f564d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.deta/prog_info +.deta/state diff --git a/main.py b/main.py index 1d051e6..edd8f6c 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from fastapi import FastAPI, Response from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles +from datetime import datetime from pydantic import BaseModel from jinja2 import Template from note import * @@ -41,7 +42,11 @@ async def read_note(note_name: str, json: bool = False): new_note = Note(name=note_name) note_dict = new_note.dict() note_key = urlsafe_key(note_name) - notes.put(note_dict, note_key) + drive_notes.put(note_name, note_dict["content"]) + note_dict["last_modified"] = str(datetime.now()) + note_meta = NoteMeta(name=note_dict["name"], links=note_dict["links"], + backlinks=note_dict["backlinks"], last_modified=note_dict["last_modified"]) + notes.put(note_meta.dict(), note_key) note_dict["base_url"] = base_url diff --git a/note.py b/note.py index fbf0b1d..4f93f29 100644 --- a/note.py +++ b/note.py @@ -3,10 +3,11 @@ import bleach from pydantic import BaseModel from deta import Deta - +from datetime import datetime deta = Deta() notes = deta.Base("deta_notes") +drive_notes = deta.Drive("notes") base_url = "/" @@ -26,6 +27,14 @@ class Note(BaseModel): content: str = default_content links: list = [] backlinks: list = [] + last_modified: str = "12:00:PM" + + +class NoteMeta(BaseModel): + name: str + links: list = [] + backlinks: list = [] + last_modified: str class Links(BaseModel): @@ -38,7 +47,8 @@ def urlsafe_key(note_name): # db operations def fetch_notes(term): - my_notes = next(notes.fetch([{"name?contains": term}, {"content?contains": term}])) + my_notes = next(notes.fetch( + [{"name?contains": term}, {"content?contains": term}])) links = [d["name"] for d in my_notes] return links @@ -50,23 +60,33 @@ def get_note(note_name): if not note_dict: return None - list_checks = ["links", "backlinks"] # db can't store empty lists, fix db + list_checks = ["links", "backlinks"] # db can't store empty lists, fix db for each in list_checks: - if not note_dict[each]: - note_dict[each] = [] - - if not note_dict["content"]: # db can't store empty strings, fix db + if not note_dict[each]: + note_dict[each] = [] + + note_content = drive_notes.get(note_name) + if note_content: + note_content = str(note_content.read(), 'ascii') + note_dict["content"] = note_content + + if not note_dict["content"]: # db can't store empty strings, fix db note_dict["content"] = "" return Note(**note_dict) - -# update note with new info +# update note with new info def db_update_note(note: Note): note_dict = note.dict() note_dict["content"] = bleach.clean(note_dict["content"]) - notes.put(note_dict, urlsafe_key(note.name)) + + drive_notes.put(note_dict["name"], str(note_dict["content"])) + + note_dict["last_modified"] = str(datetime.now()) + note_meta = NoteMeta(name=note_dict["name"], links=note_dict["links"], + backlinks=note_dict["backlinks"], last_modified=note_dict["last_modified"]) + notes.put(note_meta.dict(), urlsafe_key(note.name)) return Note(**note_dict) @@ -75,20 +95,33 @@ def remove_backlink(note_name, backlink): note = get_note(note_name) try: note.backlinks.remove(backlink) + note_dict = note.dict() + note_content = drive_notes.get(note_name) + if note_content: + note_content = str(note_content.read(), 'ascii') + note_dict["content"] = note_content + else: + note_dict["content"] = default_content except ValueError: pass - return db_update_note(note) - + return db_update_note(Note(**note_dict)) + # add a backlink to a new note, if non-existent, create def add_backlink_or_create(note_name, backlink): - note = get_note(note_name) - - if note: - note.backlinks.append(backlink) - return db_update_note(note) + note = get_note(note_name) - else: + if note: + note.backlinks.append(backlink) + note_dict = note.dict() + note_content = drive_notes.get(note_name) + if note_content: + note_content = str(note_content.read(), 'ascii') + note_dict["content"] = note_content + else: + note_dict["content"] = default_content + return db_update_note(Note(**note_dict)) + else: backlinks = [backlink] note = Note(name=note_name, backlinks=backlinks) return db_update_note(note) From 899c8543e79e72e088c260abb25fe14f6a3046ae Mon Sep 17 00:00:00 2001 From: Rohan Date: Tue, 1 Jun 2021 14:39:16 -0500 Subject: [PATCH 2/2] Delete .gitignore --- .gitignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 39f564d..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.deta/prog_info -.deta/state