Update entities and models for resource/service tagging

This commit is contained in:
pmoharana-cmd 2024-10-20 14:14:03 -04:00
parent 68f07b8c53
commit 21167f7a6b
5 changed files with 28 additions and 29 deletions

View File

@ -64,4 +64,5 @@ class ResourceEntity(EntityBase):
summary=self.summary, summary=self.summary,
link=self.link, link=self.link,
program=self.program, program=self.program,
tags=[tag.tag.to_model() for tag in self.resourceTags],
) )

View File

@ -15,6 +15,8 @@ from datetime import datetime
# Import self for to model # Import self for to model
from typing import Self from typing import Self
from ..models import ResourceTag
class ResourceTagEntity(EntityBase): class ResourceTagEntity(EntityBase):
@ -30,13 +32,13 @@ class ResourceTagEntity(EntityBase):
resource: Mapped["ResourceEntity"] = relationship(back_populates="resourceTags") resource: Mapped["ResourceEntity"] = relationship(back_populates="resourceTags")
tag: Mapped["TagEntity"] = relationship(back_populates="resourceTags") tag: Mapped["TagEntity"] = relationship(back_populates="resourceTags")
# @classmethod @classmethod
# def from_model (cls, model: resource_tag_model) -> Self: def from_model(cls, model: ResourceTag) -> Self:
# return cls ( return cls(
# id = model.id, id=model.id,
# resourceId = model.resourceId, resourceId=model.resource_id,
# tagId = model.tagId, tagId=model.tag_id,
# ) )
# def to_model (self) -> resource_tag_model: # def to_model (self) -> resource_tag_model:
# return user_model( # return user_model(

View File

@ -16,6 +16,7 @@ from ..models.tag_model import Tag
from typing import Self from typing import Self
class TagEntity(EntityBase): class TagEntity(EntityBase):
# set table name # set table name
@ -27,10 +28,12 @@ class TagEntity(EntityBase):
content: Mapped[str] = mapped_column(String(100), nullable=False) content: Mapped[str] = mapped_column(String(100), nullable=False)
# relationships # relationships
resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete") resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete") back_populates="tag", cascade="all,delete"
)
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(
back_populates="tag", cascade="all,delete"
)
@classmethod @classmethod
def from_model(cls, model: Tag) -> Self: def from_model(cls, model: Tag) -> Self:
@ -45,7 +48,7 @@ class TagEntity(EntityBase):
return cls( return cls(
id=model.id, id=model.id,
content=model.id, content=model.content,
) )
def to_model(self) -> Tag: def to_model(self) -> Tag:
@ -60,6 +63,3 @@ class TagEntity(EntityBase):
id=self.id, id=self.id,
content=self.content, content=self.content,
) )

View File

@ -4,6 +4,7 @@ from typing import List
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from .enum_for_models import ProgramTypeEnum from .enum_for_models import ProgramTypeEnum
from .tag_model import Tag
class Resource(BaseModel): class Resource(BaseModel):
@ -13,3 +14,4 @@ class Resource(BaseModel):
link: str = Field(..., max_length=150, description="link to the resource") link: str = Field(..., max_length=150, description="link to the resource")
program: ProgramTypeEnum program: ProgramTypeEnum
created_at: Optional[datetime] created_at: Optional[datetime]
tags: List[Tag] = []

View File

@ -1,13 +1,7 @@
from pydantic import BaseModel, Field from pydantic import BaseModel
from enum import Enum
from typing import List
from datetime import datetime
from typing import Optional
from .tag_model import Tag
from .resource_model import Resource
class ResourceTag(Resource, BaseModel): class ResourceTag(BaseModel):
id: int | None = None id: int | None = None
resourceid: int | None = None tag_id: int
tagid: List[Tag] resource_id: int