Rescue Paperjet v1 implementation
This commit is contained in:
parent
7ff5c8130d
commit
db9e2ca51b
83 changed files with 6186 additions and 3894 deletions
|
|
@ -1,48 +1,50 @@
|
|||
"""Background tasks for sweeping trash and old versions."""
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.document import Document
|
||||
from app.config import settings
|
||||
from app.models.annotation_state import AnnotationState
|
||||
from app.models.document import Document
|
||||
from app.models.version import Version
|
||||
from app.services.assets import delete_asset_directory
|
||||
from app.services.storage import delete_pdf_file
|
||||
from app.services.thumbnails import delete_thumbnail
|
||||
from app.config import settings
|
||||
|
||||
|
||||
def sweep_trash(db: Session) -> None:
|
||||
"""Permanently delete documents that have been in the trash past the retention period."""
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(days=settings.TRASH_RETENTION_DAYS)
|
||||
cutoff = datetime.now(UTC) - timedelta(days=settings.TRASH_RETENTION_DAYS)
|
||||
cutoff_iso = cutoff.isoformat()
|
||||
|
||||
docs_to_delete = db.query(Document).filter(
|
||||
Document.in_trash == True,
|
||||
Document.deleted_at <= cutoff_iso
|
||||
).all()
|
||||
|
||||
|
||||
docs_to_delete = (
|
||||
db.query(Document)
|
||||
.filter(Document.deleted_at.is_not(None), Document.deleted_at <= cutoff_iso)
|
||||
.all()
|
||||
)
|
||||
|
||||
for doc in docs_to_delete:
|
||||
# Delete files from disk
|
||||
delete_pdf_file(doc.id)
|
||||
delete_thumbnail(doc.id)
|
||||
|
||||
delete_asset_directory(doc.id)
|
||||
|
||||
# Delete DB associations
|
||||
db.query(AnnotationState).filter(AnnotationState.document_id == doc.id).delete()
|
||||
db.query(Version).filter(Version.document_id == doc.id).delete()
|
||||
|
||||
|
||||
# Delete document record
|
||||
db.delete(doc)
|
||||
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
def prune_auto_versions(db: Session) -> None:
|
||||
"""Delete automatic versions older than the retention period."""
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(days=settings.AUTO_VERSION_RETENTION_DAYS)
|
||||
cutoff = datetime.now(UTC) - timedelta(days=settings.AUTO_VERSION_RETENTION_DAYS)
|
||||
cutoff_iso = cutoff.isoformat()
|
||||
|
||||
db.query(Version).filter(
|
||||
Version.kind == "auto",
|
||||
Version.created_at <= cutoff_iso
|
||||
).delete()
|
||||
|
||||
|
||||
db.query(Version).filter(Version.kind == "auto", Version.created_at <= cutoff_iso).delete()
|
||||
|
||||
db.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue