Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this create a human readable string? I always used .strftime()

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

Expand Down
69 changes: 51 additions & 18 deletions note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/"

Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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"]))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll have to put it under the url safe key e.g. urlsafe_key(note.name)?


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)


Expand All @@ -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:
Copy link
Copy Markdown
Owner

@xeust xeust Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this function, if it's only a backlink update, we don't need to read & write from drive (because the content itself doesn't change).

Maybe a slight refactor of db_update_note?

Or maybe 3 functions:

  1. update_note: calls 2 & 3
  2. drive_update_note: reads / writes from Drive
  3. db_update_note: reads / writes from Base

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)