-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
61 lines (46 loc) · 1.75 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from sqlalchemy import String, Column, Integer, DateTime, Boolean, Enum, func
from sqlalchemy.orm import DeclarativeBase, declarative_base
from sqlalchemy.exc import IntegrityError
import enum
# from src.database import Session
from database import Session
from datetime import datetime
session = Session()
Base = declarative_base()
class BaseModel(Base):
__abstract__ = True # __abstract__ directive is used for abstract classes that should not be mapped to a database table
# These times are stored as UTC
# create_time = Column(DateTime(timezone=True), server_default=str(datetime.now()))
# update_time = Column(DateTime(timezone=True), server_default=func.now())
''' Alterações no banco de dados '''
def save(self):
session.add(self)
try:
session.commit()
except IntegrityError as err:
session.rollback()
def update(self):
try:
session.commit()
except IntegrityError as err:
session.rollback()
def delete(self):
session.delete(self)
try:
session.commit()
except IntegrityError as err:
session.rollback()
class CarpoolType(enum.Enum):
going = 'going'
returning = 'returning'
class Carpool(BaseModel):
__tablename__ = 'carpool'
id: int = Column(Integer, primary_key=True)
chat_id: str = Column(String(100)) # Not a FK
created_at: DateTime = Column(DateTime(timezone=False))
departure_datetime: DateTime = Column(DateTime(timezone=False))
telegram_username: str = Column(String(100))
place: str = Column(String(100))
available_seats: int = Column(Integer)
is_active: bool = Column(Boolean)
carpool_type: Enum = Column(Enum(CarpoolType))