50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
|
|
"""News and sentiment models: Article, ArticleSentiment."""
|
||
|
|
|
||
|
|
import uuid
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from sqlalchemy import DateTime, Float, ForeignKey, String, Text
|
||
|
|
from sqlalchemy.dialects.postgresql import UUID
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
|
|
||
|
|
from shared.models.base import Base, TimestampMixin
|
||
|
|
|
||
|
|
|
||
|
|
class Article(TimestampMixin, Base):
|
||
|
|
__tablename__ = "articles"
|
||
|
|
|
||
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||
|
|
)
|
||
|
|
source: Mapped[str] = mapped_column(String(100), nullable=False)
|
||
|
|
url: Mapped[str] = mapped_column(Text, nullable=False)
|
||
|
|
title: Mapped[str] = mapped_column(Text, nullable=False)
|
||
|
|
published_at: Mapped[datetime | None] = mapped_column(
|
||
|
|
DateTime(timezone=True), nullable=True
|
||
|
|
)
|
||
|
|
fetched_at: Mapped[datetime] = mapped_column(
|
||
|
|
DateTime(timezone=True), nullable=False
|
||
|
|
)
|
||
|
|
content_hash: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||
|
|
|
||
|
|
# Relationships
|
||
|
|
sentiments: Mapped[list["ArticleSentiment"]] = relationship(back_populates="article")
|
||
|
|
|
||
|
|
|
||
|
|
class ArticleSentiment(TimestampMixin, Base):
|
||
|
|
__tablename__ = "article_sentiments"
|
||
|
|
|
||
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||
|
|
)
|
||
|
|
article_id: Mapped[uuid.UUID] = mapped_column(
|
||
|
|
UUID(as_uuid=True), ForeignKey("articles.id"), nullable=False
|
||
|
|
)
|
||
|
|
ticker: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
|
||
|
|
score: Mapped[float] = mapped_column(Float, nullable=False)
|
||
|
|
confidence: Mapped[float] = mapped_column(Float, nullable=False)
|
||
|
|
model_used: Mapped[str] = mapped_column(String(50), nullable=False)
|
||
|
|
|
||
|
|
# Relationships
|
||
|
|
article: Mapped[Article] = relationship(back_populates="sentiments")
|