Skip to content

Commit

Permalink
refactor: anonoymous users drafts are now stored in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
redimp committed Jan 23, 2025
1 parent 1ebeb8d commit 091a9e7
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions otterwiki/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
from werkzeug.utils import secure_filename
from urllib.parse import unquote
from io import BytesIO
from uuid import uuid4

import PIL.Image

Expand Down Expand Up @@ -1079,12 +1078,22 @@ def edit_attachment(
# show edit form
return a.edit()

def expire_anonymous_drafts(self):
drafts_to_expire = Drafts.query.filter(
db.and_(
Drafts.author_email.like("anonymous_uid:%"),
Drafts.datetime < datetime.now() - timedelta(days=1),
)
).all()
for draft in drafts_to_expire:
db.session.delete(draft)
db.session.commit()

def load_draft(self, author):
self.expire_anonymous_drafts()

if current_user.is_anonymous:
if "drafts_uid" not in session:
return None
else:
author_email = session["drafts_uid"]
author_email = current_user.anonymous_uid()
else:
author_email = author[1]

Expand All @@ -1095,10 +1104,7 @@ def load_draft(self, author):

def discard_draft(self, author):
if current_user.is_anonymous:
if "drafts_uid" not in session:
return
else:
author_email = session["drafts_uid"]
author_email = current_user.anonymous_uid()
else:
author_email = author[1]

Expand All @@ -1114,10 +1120,7 @@ def save_draft(
abort(403)
# Handle anonymous users, save draft in session
if current_user.is_anonymous:
if "drafts_uid" not in session:
session["drafts_uid"] = str(uuid4())
session.modified = True
author_email = session["drafts_uid"]
author_email = current_user.anonymous_uid()
else:
author_email = author[1]

Expand All @@ -1135,7 +1138,7 @@ def save_draft(
draft.revision = revision
draft.cursor_line = cursor_line
draft.cursor_ch = cursor_ch
print(f"{draft}")

db.session.add(draft)
db.session.commit()

Expand Down

0 comments on commit 091a9e7

Please sign in to comment.