Skip to content

Workflow to delete images when entry deleted #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions spire/journal/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from uuid import UUID, uuid4

import boto3
import requests

from sqlalchemy.orm import Session, Query
from sqlalchemy import or_, func, text, and_
Expand Down Expand Up @@ -585,6 +586,27 @@ async def get_journal_entries(
return query.all()


async def delete_entry_images(
access_token: str, journal_spec: JournalSpec, entry_id: UUID
) -> None:
images_url = f"https://files.bugout.dev/files/{str(journal_spec.id)}/entries/{str(entry_id)}/images"
headers = {"Authorization": f"Bearer {access_token}"}
images_response = requests.get(images_url, headers=headers, timeout=4)
images_response.raise_for_status()
images = images_response.json()

for image in images.images:
try:
r = requests.delete(
f"{images_url}/{image['id']}", headers=headers, timeout=4
)
r.raise_for_status()
except Exception as e:
logger.error(
f"Unable to delete image with id: {image['id']} for entry with id: {str(entry_id)}"
)


async def delete_journal_entry(
db_session: Session,
journal_spec: JournalSpec,
Expand Down
7 changes: 7 additions & 0 deletions spire/journal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,8 @@ async def delete_entry(
{JournalEntryScopes.DELETE},
)

access_token = request.state.token

journal_spec = JournalSpec(id=journal_id, bugout_user_id=request.state.user_id)
try:
journal = await actions.find_journal(
Expand All @@ -1422,6 +1424,11 @@ async def delete_entry(
raise HTTPException(status_code=500)
es_index = journal.search_index

try:
await actions.delete_entry_images(access_token, journal_spec, entry_id)
except Exception as e:
logger.error(f"Error due deleting entry images: {str(e)}")

try:
journal_entry = await actions.delete_journal_entry(
db_session,
Expand Down