-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_models.py
More file actions
99 lines (80 loc) · 4.43 KB
/
db_models.py
File metadata and controls
99 lines (80 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from typing import List, Optional
from sqlalchemy import BigInteger, JSON, Boolean, Index
from sqlalchemy import Column, Integer, String, Text, Table, ForeignKey, DateTime, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.mutable import MutableList
from sqlalchemy.orm import declarative_base, relationship, Mapped, mapped_column
import logging
from datetime import datetime, timedelta, UTC
Base = declarative_base()
class Item(Base):
__tablename__ = "items"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
company: Mapped[str] = mapped_column(String(255), nullable=True)
title: Mapped[str] = mapped_column(String(255), nullable=True)
content: Mapped[str] = mapped_column(Text, nullable=True)
client_img_filename: Mapped[str] = mapped_column(String(512), nullable=True)
client_img_width: Mapped[int] = mapped_column(Integer, nullable=True)
client_img_height: Mapped[int] = mapped_column(Integer, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
categories: Mapped[list[int]] = mapped_column(MutableList.as_mutable(JSON), default=list)
available: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"company": self.company,
"title": self.title,
"content": self.content,
"img_filename": self.img_filename,
"img_width": self.img_width,
"img_height": self.img_height,
"client_img_filename": self.client_img_filename,
"client_img_width": self.client_img_width,
"client_img_height": self.client_img_height,
"categories": self.categories,
"created_at": self.created_at.isoformat() if self.created_at else None,
"available": self.available,
}
class Disk(Base):
__tablename__ = "disks"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
disk_hash: Mapped[str] = mapped_column(String(255), nullable=False)
available: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now(UTC))
bookings: Mapped[List["Booking"]] = relationship("Booking", back_populates="disk")
class Booking(Base):
__tablename__ = "bookings"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
disk_id: Mapped[int] = mapped_column(Integer, ForeignKey("disks.id", ondelete="CASCADE"), nullable=False)
user_login: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
start_time: Mapped[datetime] = mapped_column(DateTime, nullable=False)
end_time: Mapped[datetime] = mapped_column(DateTime, nullable=True)
pickup_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
return_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now(UTC))
disk: Mapped[List["Disk"]] = relationship("Disk", back_populates="bookings")
__table_args__ = (
Index("idx_disk_time_overlap", "disk_id", "start_time", "end_time"),
)
class IssuedToken(Base):
__tablename__ = 'issued_tokens'
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_login: Mapped[str] = mapped_column(String(128), nullable=False)
token_hash: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
issued_at = mapped_column(BigInteger, nullable=False)
expires_at = mapped_column(BigInteger, nullable=False)
class State(Base):
__tablename__ = 'states'
state: Mapped[str] = mapped_column(String(128), primary_key=True)
next: Mapped[str] = mapped_column(String(2048), nullable=True)
created_at = mapped_column(BigInteger, nullable=False)
expires_at = mapped_column(BigInteger, nullable=False)
class Blacklist(Base):
__tablename__ = "blacklist"
id: Mapped[int] = mapped_column(primary_key=True)
login: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
reason: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())