Multimedika/Bot_Development
0
1from typing import Literal2from typing_extensions import Annotated3import sqlalchemy4from sqlalchemy.orm import mapped_column5from sqlalchemy import Integer, String, ForeignKey,func, DateTime, Boolean, LargeBinary6from sqlalchemy.orm import DeclarativeBase, Mapped7import uuid8import datetime9import pytz10 11# Set Jakarta timezone12jakarta_tz = pytz.timezone('Asia/Jakarta')13 14def get_jakarta_time():15 return datetime.datetime.now(jakarta_tz)16 17# Use the timezone-aware function in SQLAlchemy annotations18timestamp_current = Annotated[19 datetime.datetime,20 mapped_column(nullable=False, default=get_jakarta_time) # Use default instead of server_default21]22 23timestamp_update = Annotated[24 datetime.datetime,25 mapped_column(nullable=False, default=get_jakarta_time, onupdate=get_jakarta_time) # onupdate uses the Python function26]27 28message_role = Literal["user", "assistant"]29 30class Base(DeclarativeBase):31 type_annotation_map = {32 message_role: sqlalchemy.Enum("user", "assistant", name="message_role"),33 }34 35class User(Base):36 __tablename__ = "user"37 38 id = mapped_column(Integer, primary_key=True)39 name = mapped_column(String(100), nullable=False)40 username = mapped_column(String(100), unique=True, nullable=False)41 role_id = mapped_column(Integer, ForeignKey("role.id"))42 email = mapped_column(String(100), unique=True, nullable=False)43 password_hash = mapped_column(String(100), nullable=False)44 created_at: Mapped[timestamp_current] 45 updated_at : Mapped[timestamp_update] 46 47class Feedback(Base):48 __tablename__ = "feedback"49 50 id = mapped_column(Integer, primary_key=True)51 user_id = mapped_column(Integer, ForeignKey("user.id"))52 rating = mapped_column(Integer)53 comment = mapped_column(String(1000))54 created_at : Mapped[timestamp_current] 55 56class Role(Base):57 __tablename__ = "role"58 59 id = mapped_column(Integer, primary_key=True)60 role_name = mapped_column(String(200), nullable=False)61 description = mapped_column(String(200))62 63class User_Role(Base):64 __tablename__ = "user_role"65 66 id = mapped_column(Integer, primary_key=True)67 user_id = mapped_column(Integer, ForeignKey("user.id"))68 role_id = mapped_column(Integer, ForeignKey("role.id"))69 70class Bot(Base):71 __tablename__ = "bot"72 73 id = mapped_column(Integer, primary_key=True)74 user_id = mapped_column(Integer, ForeignKey("user.id"))75 bot_name = mapped_column(String(200), nullable=False)76 created_at : Mapped[timestamp_current] 77 78class Session(Base):79 __tablename__ = "session"80 81 id = mapped_column(String(36), primary_key=True, index=True, default=lambda: str(uuid.uuid4())) # Store as string82 user_id = mapped_column(Integer, ForeignKey("user.id"))83 bot_id = mapped_column(Integer, ForeignKey("bot.id"))84 created_at : Mapped[timestamp_current]85 updated_at : Mapped[timestamp_update] 86 87class Message(Base):88 __tablename__ = "message"89 90 id = mapped_column(Integer, primary_key=True)91 session_id = mapped_column(String(36), ForeignKey("session.id"), nullable=False) # Store as string92 role : Mapped[message_role]93 goal = mapped_column(String(200))94 created_at : Mapped[timestamp_current] 95 96class Category(Base):97 __tablename__ = "category"98 99 id = mapped_column(Integer, primary_key=True)100 category = mapped_column(String(200))101 created_at : Mapped[timestamp_current]102 updated_at : Mapped[timestamp_update] 103 104class Metadata(Base):105 __tablename__ = "metadata"106 107 id = mapped_column(Integer, primary_key=True)108 title = mapped_column(String(200))109 # image_data = mapped_column(LargeBinary, nullable=True)110 category_id = mapped_column(Integer, ForeignKey("category.id"))111 author = mapped_column(String(200))112 year = mapped_column(Integer)113 publisher = mapped_column(String(100))114 thumbnail = mapped_column(LargeBinary, nullable=True)115 created_at : Mapped[timestamp_current] 116 updated_at : Mapped[timestamp_update] 117 118 119class Bot_Meta(Base):120 __tablename__ = "bot_meta"121 122 id = mapped_column(Integer, primary_key=True)123 bot_id = mapped_column(Integer, ForeignKey("bot.id"))124 metadata_id = mapped_column(Integer, ForeignKey("metadata.id"))125 created_at : Mapped[timestamp_current]126 updated_at : Mapped[timestamp_update] 127 128class User_Meta(Base):129 __tablename__ = "user_meta"130 131 id = mapped_column(Integer, primary_key=True)132 user_id = mapped_column(Integer, ForeignKey("user.id"))133 metadata_id = mapped_column(Integer, ForeignKey("metadata.id"))134 created_at : Mapped[timestamp_current]135 updated_at : Mapped[timestamp_update] 136 137class Planning(Base):138 __tablename__="planning"139 140 id = mapped_column(Integer, primary_key=True)141 trials_id = mapped_column(Integer, ForeignKey("trials.id"))142 planning_name = mapped_column(String(200), nullable=False)143 duration = mapped_column(Integer, nullable=False) # Duration in months144 start_date = mapped_column(DateTime, nullable=False) # Start date of the planning145 end_date = mapped_column(DateTime, nullable=False) # End date of the planning146 is_activated = mapped_column(Boolean)147 created_at : Mapped[timestamp_current] # Automatically sets the current timestamp148 149class Trials(Base):150 __tablename__ = "trials"151 152 id = mapped_column(Integer, primary_key=True)153 token_used = mapped_column(Integer, nullable=False) # Adjust length as needed154 token_planned = mapped_column(Integer, nullable=False)155 156 157class Session_Publisher(Base):158 __tablename__ = "session_publisher"159 160 id = mapped_column(String(36), primary_key=True, index=True, default=lambda: str(uuid.uuid4())) # Store as string161 user_id = mapped_column(Integer, ForeignKey("user.id"))162 bot_name = mapped_column(String(100), nullable=True)163 metadata_id = mapped_column(Integer, ForeignKey("metadata.id"))164 created_at : Mapped[timestamp_current] 165 updated_at : Mapped[timestamp_update]