-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
117 lines (95 loc) · 2.84 KB
/
db.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from sqlalchemy import (
create_engine,
ForeignKey,
CheckConstraint,
Column,
DateTime,
Integer,
String,
Text,
Float,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
import os
dirpath = os.path.dirname(os.path.realpath(__file__))
db_dir = os.path.join(dirpath, "database/database.db")
SQLITE_DB = "".join(["sqlite:///", db_dir])
print(SQLITE_DB)
engine = create_engine(SQLITE_DB, echo=True)
Base = declarative_base(engine)
session_factory = sessionmaker(bind=engine)
class Planetype(Base):
__tablename__ = "Planetype"
id = Column(Integer, primary_key=True)
amdarid = Column(String)
flightid = Column(String)
planetype = Column(String)
time = Column(String)
dep = Column(String)
arr = Column(String)
datasource = Column(String)
def serialize(self):
return {
"amdarid": self.amdarid,
"fligthid": self.flightid,
"planetype": self.planetype,
"time": self.time,
"dep": self.dep,
"arr": self.arr,
"datasource": self.datasource,
}
class Timezone(Base):
__tablename__ = "Timezone"
id = Column(Integer, primary_key=True)
timezone = Column(String)
utcdiff = Column(String)
class Route(Base):
__tablename__ = "Route"
id = Column(Integer, primary_key=True)
flightid = Column(String(20))
dep = Column(String(5), nullable=False)
arr = Column(String(5), nullable=False)
def serialize(self):
return {
"flightid": self.flightid,
"dep": self.dep,
"arr": self.arr,
}
class Airport(Base):
__tablename__ = "Airport"
iata = Column(String(5), primary_key=True)
icao = Column(String(5))
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
altitude = Column(Float, nullable=False)
international = Column(Integer, default=0)
def serialize(self):
return {
"icao": self.icao,
"latitude": self.latitude,
"longitude": self.longitude,
"altitude": self.altitude,
}
class Airline(Base):
__tablename__ = "Airline"
id = Column(Integer, primary_key=True)
iata = Column(String(2))
icao = Column(String(3))
name = Column(String(50))
class Noroute(Base):
__tablename__ = "noroute"
id = Column(Integer, primary_key=True)
arr = Column(String(4))
dep = Column(String(4))
def reinit():
Base.metadata.drop_all()
Base.metadata.create_all()
def recreate_table(table_name):
session = session_factory()
session.execute(f"drop table {table_name}")
session.commit()
Base.metadata.tables[f"{table_name}"].create(bind=engine)
def create_table(table_name):
Base.metadata.tables[f"{table_name}"].create(bind=engine)