-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
40 lines (31 loc) · 1.42 KB
/
models.py
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
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session
from connect import engine
class Base(DeclarativeBase):
pass
class Activity(Base):
"""
Class for persisting activities given by Bored API
"""
__tablename__ = "activities"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(nullable=False)
type: Mapped[str] = mapped_column(nullable=False)
participants: Mapped[int] = mapped_column(nullable=False)
price: Mapped[float] = mapped_column(nullable=False)
link: Mapped[str] = mapped_column(nullable=True)
key: Mapped[int] = mapped_column(nullable=False)
accessibility: Mapped[float] = mapped_column(nullable=False)
@staticmethod
def get_last_activities(self, number_of_activities=5):
activity_session = Session(bind=engine)
last_activities = activity_session.query(Activity).order_by(Activity.id.desc()).limit(number_of_activities).all()
return "".join(map(str, last_activities))
def __repr__(self):
return f"Activity: {self.name} \n" \
f"Type: {self.type} \n" \
f"Participants: {self.participants} \n" \
f"Price: {self.price} \n" \
f"Accessibility: {self.accessibility} \n" \
f"Link: {self.link} \n" \
"-----------------------------\n"
Base.metadata.create_all(engine)