Rework get entities to use sqids id
entity.py, deps.py, crud_entities.py, and entities.py
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from uuid import UUID
|
||||
from typing import Annotated
|
||||
from app.schemas.entities import ENTITY_NAMESPACE
|
||||
|
||||
from osintbuddy import Registry
|
||||
from osintbuddy.templates.default import plugin_source_template
|
||||
from osintbuddy.utils.generic import to_snake_case
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -74,34 +76,49 @@ async def get_entities(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
):
|
||||
try:
|
||||
if limit > 50:
|
||||
limit = 50
|
||||
entities, entities_count = crud.entities.get_many_by_favorites(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
is_favorite=False
|
||||
)
|
||||
favorite_entities, favorite_count = crud.entities.get_many_by_favorites(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
is_favorite=True
|
||||
)
|
||||
return {
|
||||
"entities": entities,
|
||||
"count": entities_count,
|
||||
"favorite_entities": favorite_entities,
|
||||
"favorite_count": favorite_count
|
||||
}
|
||||
except Exception as e:
|
||||
log.error('Error inside entity.get_entities:')
|
||||
log.error(e)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="There was an error getting your entities. Please try again"
|
||||
)
|
||||
# try:
|
||||
if limit > 50:
|
||||
limit = 50
|
||||
db_entities, entities_count = crud.entities.get_many_by_favorites(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
is_favorite=False
|
||||
)
|
||||
db_favorite_entities, favorite_count = crud.entities.get_many_by_favorites(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
is_favorite=True
|
||||
)
|
||||
|
||||
entities = []
|
||||
for entity in db_entities:
|
||||
entity = entity._asdict()
|
||||
entity["id"] = deps.hid(db_id=entity.get("id"), ns=ENTITY_NAMESPACE)
|
||||
entities.append(entity)
|
||||
print(entity)
|
||||
favorite_entities = []
|
||||
for entity in db_favorite_entities:
|
||||
entity = entity._asdict()
|
||||
entity["id"] = deps.hid(db_id=entity.get("id"), ns=ENTITY_NAMESPACE)
|
||||
favorite_entities.append(entity)
|
||||
print(entity)
|
||||
|
||||
|
||||
return {
|
||||
"entities": entities,
|
||||
"count": entities_count,
|
||||
"favorite_entities": favorite_entities,
|
||||
"favorite_count": favorite_count
|
||||
}
|
||||
# except Exception as e:
|
||||
# log.error('Error inside entity.get_entities:')
|
||||
# log.error(e)
|
||||
# raise HTTPException(
|
||||
# status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
# detail="There was an error getting your entities. Please try again"
|
||||
# )
|
||||
|
||||
@router.post("")
|
||||
async def create_entity(
|
||||
@@ -114,13 +131,18 @@ async def create_entity(
|
||||
label=entity.label,
|
||||
author=entity.author,
|
||||
description=entity.description,
|
||||
source=plugin_source_template(
|
||||
label=entity.label,
|
||||
description=entity.description,
|
||||
author=entity.author
|
||||
)
|
||||
))
|
||||
except Exception as e:
|
||||
log.error('Error inside entity.create_entity:')
|
||||
log.error(e)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="There was an error getting your entities. Please try again"
|
||||
detail="There was an error creating your entity. Please try again"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from selenium.webdriver.chrome.webdriver import WebDriver
|
||||
from app import schemas, crud
|
||||
from app.db.session import SessionLocal
|
||||
from app.core.logger import get_logger
|
||||
from app.api.utils import APIRequest, HidChecker
|
||||
from app.api.utils import APIRequest, HidChecker, hid
|
||||
|
||||
log = get_logger("api.deps")
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class CRUDEntities(CRUDBase[
|
||||
def get_many_by_favorites(
|
||||
self, db: Session, *, skip: int = 0, limit: int = 100, is_favorite: bool = False
|
||||
) -> List[ModelType]:
|
||||
entities = db.query(self.model).where(self.model.is_favorite == is_favorite).offset(skip).limit(limit).all()
|
||||
entities = db.query(self.model.id, self.model.is_favorite, self.model.last_edited, self.model.label, self.model.description, self.model.author).where(self.model.is_favorite == is_favorite).offset(skip).limit(limit).all()
|
||||
entities_count = self.count_by_favorites(db, is_favorite=is_favorite)[0][0]
|
||||
return entities, entities_count
|
||||
|
||||
|
||||
@@ -54,22 +54,27 @@ class EntityInDBBase(EntityBase):
|
||||
created: datetime.datetime
|
||||
|
||||
|
||||
# Additional properties to return via API
|
||||
class Entity(EntityInDBBase):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class EntityInList(PostEntityCreate):
|
||||
id: str
|
||||
last_edited: datetime.datetime
|
||||
is_favorite: bool
|
||||
|
||||
class AllEntitiesList(BaseModel):
|
||||
entities: List[Entity]
|
||||
entities: List[EntityInList]
|
||||
count: int
|
||||
favorite_entities: List[Entity]
|
||||
favorite_entities: List[EntityInList]
|
||||
favorite_count: int
|
||||
|
||||
|
||||
class EntitiesList(BaseModel):
|
||||
entities: List[Entity]
|
||||
count: int
|
||||
|
||||
# Additional properties stored in DB
|
||||
|
||||
class EntityInDB(EntityInDBBase):
|
||||
id: Optional[int] = None
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user