-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
222 lines (199 loc) · 5.78 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
from sqlalchemy import Column, String, Integer, Date, DateTime, Boolean, create_engine
from flask_sqlalchemy import SQLAlchemy
import json
database_path = os.environ['DATABASE_URL']
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
'''
Trip
a persistent trip entity, extends the base SQLAlchemy Model
'''
class Trip(db.Model):
__tablename__ = 'trips'
id = Column(Integer, primary_key=True)
name = Column(String)
start_date = Column(Date)
end_date = Column(Date)
flights = db.relationship('Flight', cascade='all,delete-orphan', backref=db.backref('flights', lazy=True, cascade='all'))
accomodations = db.relationship('Accommodation', cascade='all,delete-orphan', backref=db.backref('accommodations', lazy=True, cascade='all'))
'''
insert()
inserts a new trip into a database
EXAMPLE
trip = Trip(
name=req_name,
start_date=req_start_date,
end_date=req_end_date)
trip.insert()
'''
def insert(self):
db.session.add(self)
db.session.commit()
'''
update()
updates a trip record in a database
the trip id must exist in the database
EXAMPLE
trip = Trip.query.filter(Trip.id == id).one_or_none()
trip.end_date = '2020-02-08T21:40:00'
trip.update()
'''
def update(self):
db.session.commit()
'''
delete()
deletes a trip record in a database
the trip id must exist in the database
EXAMPLE
trip = Trip.query.filter(Trip.id == id).one_or_none()
trip.delete()
'''
def delete(self):
db.session.delete(self)
db.session.commit()
'''
format()
formats trip record to json so it can more easily be consumed by the frontend
'''
def format(self):
return {
'id': self.id,
'name': self.name,
'start_date': self.start_date,
'end_date': self.end_date
}
'''
Flight
a persistent flight entity, extends the base SQLAlchemy Model
'''
class Flight(db.Model):
__tablename__ = 'flights'
id = Column(Integer, primary_key=True)
origin = Column(String)
destination = Column(String)
time = Column(DateTime)
booked = Column(Boolean)
trip = Column(Integer, db.ForeignKey('trips.id', ondelete="CASCADE"))
'''
insert()
inserts a new flight into a database
the trip must correspond to a trip id in the database
EXAMPLE
flight = Flight(
origin=req_origin,
destination=req_destination,
time=req_time,
booked=req_booked,
trip=req_trip)
flight.insert()
'''
def insert(self):
db.session.add(self)
db.session.commit()
'''
update()
updates a flight record in a database
the flight id must exist in the database
EXAMPLE
flight = Flight.query.filter(Flight.id == id).one_or_none()
flight.booked = True
flight.update()
'''
def update(self):
db.session.commit()
'''
delete()
deletes a flight record in a database
the flight id must exist in the database
EXAMPLE
flight = Flight.query.filter(Flight.id == id).one_or_none()
flight.delete()
'''
def delete(self):
db.session.delete(self)
db.session.commit()
'''
format()
formats flight record to json so it can more easily be consumed by the frontend
'''
def format(self):
return {
'id': self.id,
'origin': self.origin,
'destination': self.destination,
'time': self.time,
'booked': self.booked,
'trip': self.trip
}
'''
Accommodation
a persistent accommodation entity, extends the base SQLAlchemy Model
'''
class Accommodation(db.Model):
__tablename__ = 'accommodations'
id = Column(Integer, primary_key=True)
location = Column(String)
start_date = Column(Date)
end_date = Column(Date)
booked = Column(Boolean)
trip = Column(Integer, db.ForeignKey('trips.id', ondelete="CASCADE"))
'''
insert()
inserts a new accommodation into a database
the trip must correspond to a trip id in the database
EXAMPLE
accommodation = Accommodation(
location=req_location,
start_date=req_start_date,
end_date=req_end_date,
booked=req_booked,
trip=req_trip)
accommodation.insert()
'''
def insert(self):
db.session.add(self)
db.session.commit()
'''
update()
updates an accommodation record in a database
the accommodation id must exist in the database
EXAMPLE
accommodation = Accommodation.query.filter(Accommodation.id == id).one_or_none()
accommodation.booked = True
accommodation.update()
'''
def update(self):
db.session.commit()
'''
delete()
deletes an accommodation record in a database
the accommodation id must exist in the database
EXAMPLE
accommodation = Accommodation.query.filter(Accommodation.id == id).one_or_none()
accommodation.delete()
'''
def delete(self):
db.session.delete(self)
db.session.commit()
'''
format()
formats accommodation record to json so it can more easily be consumed by the frontend
'''
def format(self):
return {
'id': self.id,
'location': self.location,
'start_date': self.start_date,
'end_date': self.end_date,
'booked': self.booked,
'trip': self.trip
}