mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-11 13:08:34 +03:00
Format and lint shared directory (#64)
This commit is contained in:
@@ -9,7 +9,9 @@ from app.shared.db import models
|
||||
# TODO: isolate database operations away from worker and into WEB
|
||||
# ONLY WORKER
|
||||
def update_sheet_last_url_archived_at(db: Session, sheet_id: str):
|
||||
db_sheet = db.query(models.Sheet).filter(models.Sheet.id == sheet_id).first()
|
||||
db_sheet = (
|
||||
db.query(models.Sheet).filter(models.Sheet.id == sheet_id).first()
|
||||
)
|
||||
if db_sheet:
|
||||
db_sheet.last_url_archived_at = datetime.now()
|
||||
db.commit()
|
||||
@@ -19,12 +21,17 @@ def update_sheet_last_url_archived_at(db: Session, sheet_id: str):
|
||||
|
||||
# ONLY WORKER and INTEROP
|
||||
|
||||
|
||||
def get_group(db: Session, group_name: str) -> models.Group:
|
||||
return db.query(models.Group).filter(models.Group.id == group_name).first()
|
||||
|
||||
|
||||
def create_or_get_user(db: Session, author_id: str) -> models.User:
|
||||
if type(author_id) == str: author_id = author_id.lower()
|
||||
db_user = db.query(models.User).filter(models.User.email == author_id).first()
|
||||
if isinstance(author_id, str):
|
||||
author_id = author_id.lower()
|
||||
db_user = (
|
||||
db.query(models.User).filter(models.User.email == author_id).first()
|
||||
)
|
||||
if not db_user:
|
||||
db_user = models.User(email=author_id)
|
||||
db.add(db_user)
|
||||
@@ -43,8 +50,22 @@ def create_tag(db: Session, tag: str) -> models.Tag:
|
||||
return db_tag
|
||||
|
||||
|
||||
def create_archive(db: Session, archive: schemas.ArchiveCreate, tags: list[models.Tag], urls: list[models.ArchiveUrl]) -> models.Archive:
|
||||
db_archive = models.Archive(id=archive.id, url=archive.url, result=archive.result, public=archive.public, author_id=archive.author_id, group_id=archive.group_id, sheet_id=archive.sheet_id, store_until=archive.store_until)
|
||||
def create_archive(
|
||||
db: Session,
|
||||
archive: schemas.ArchiveCreate,
|
||||
tags: list[models.Tag],
|
||||
urls: list[models.ArchiveUrl],
|
||||
) -> models.Archive:
|
||||
db_archive = models.Archive(
|
||||
id=archive.id,
|
||||
url=archive.url,
|
||||
result=archive.result,
|
||||
public=archive.public,
|
||||
author_id=archive.author_id,
|
||||
group_id=archive.group_id,
|
||||
sheet_id=archive.sheet_id,
|
||||
store_until=archive.store_until,
|
||||
)
|
||||
db_archive.tags = tags
|
||||
db_archive.urls = urls
|
||||
db.add(db_archive)
|
||||
@@ -53,10 +74,14 @@ def create_archive(db: Session, archive: schemas.ArchiveCreate, tags: list[model
|
||||
return db_archive
|
||||
|
||||
|
||||
def store_archived_url(db: Session, archive: schemas.ArchiveCreate) -> models.Archive:
|
||||
def store_archived_url(
|
||||
db: Session, archive: schemas.ArchiveCreate
|
||||
) -> models.Archive:
|
||||
# create and load user, tags, if needed
|
||||
create_or_get_user(db, archive.author_id)
|
||||
db_tags = [create_tag(db, tag) for tag in (archive.tags or [])]
|
||||
# insert everything
|
||||
db_archive = create_archive(db, archive=archive, tags=db_tags, urls=archive.urls)
|
||||
db_archive = create_archive(
|
||||
db, archive=archive, tags=db_tags, urls=archive.urls
|
||||
)
|
||||
return db_archive
|
||||
|
||||
Reference in New Issue
Block a user