feat: attributes in cards and projects

This commit is contained in:
2025-02-27 18:34:38 +04:00
parent c6c006d45b
commit a2c9fd8e3b
16 changed files with 391 additions and 27 deletions

View File

@@ -1,10 +1,11 @@
from typing import Optional
from sqlalchemy import select
from sqlalchemy.orm import joinedload, selectinload
from sqlalchemy import select, and_
from sqlalchemy.ext.asyncio import AsyncResult
from sqlalchemy.orm import selectinload
from card_attributes.handlers.base_handler import BaseHandler
from models import Attribute, project_attribute, card_relations
from models import Attribute, project_attribute, card_relations, Card, Project, Board
class CardAttributesQueryHandler(BaseHandler):
@@ -12,7 +13,10 @@ class CardAttributesQueryHandler(BaseHandler):
stmt = (
select(Attribute)
.join(project_attribute, project_attribute.c.attribute_id == Attribute.id)
.where(project_attribute.c.project_id == project_id)
.where(
project_attribute.c.project_id == project_id,
Attribute.is_deleted == False,
)
)
attributes = (await self.session.scalars(stmt)).all()
return list(attributes)
@@ -23,7 +27,10 @@ class CardAttributesQueryHandler(BaseHandler):
.options(
selectinload(Attribute.projects),
)
.where(Attribute.name == attr_name)
.where(
Attribute.name == attr_name,
Attribute.is_deleted == False,
)
)
attribute = (await self.session.scalars(stmt)).first()
return attribute
@@ -35,3 +42,37 @@ class CardAttributesQueryHandler(BaseHandler):
)
ids = await self.session.scalars(stmt)
return list(ids)
async def get_all_cards_for_project(self, project_id: int) -> AsyncResult[Card]:
stmt = (
select(Card)
.join(Board)
.join(Project)
.where(Project.id == project_id)
.options(selectinload(Card.attributes))
.execution_options(yield_per=100)
)
rows: AsyncResult[tuple[Card]] = await self.session.stream(stmt)
async for row in rows:
yield row[0]
async def get_project_attr(self, project_id: int, attribute_id: int) -> project_attribute:
stmt_is_attribute_already_added = (
select(project_attribute)
.where(
and_(
project_attribute.c.project_id == project_id,
project_attribute.c.attribute_id == attribute_id,
)
)
)
project_attribute_inst = await self.session.execute(stmt_is_attribute_already_added)
return project_attribute_inst.first()
async def get_attributes_by_ids(self, attribute_ids: list[int]) -> list[Attribute]:
stmt = (
select(Attribute)
.where(Attribute.id.in_(attribute_ids))
)
attributes = (await self.session.scalars(stmt)).all()
return list(attributes)