-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
173 lines (120 loc) · 5.59 KB
/
model.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
"""Models for travel web app."""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from passlib.hash import argon2
from argon2 import PasswordHasher
ph = PasswordHasher()
db = SQLAlchemy()
class User(db.Model):
"""A user."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
fname = db.Column(db.String, nullable=False)
lname = db.Column(db.String) # previously nullable=False
email = db.Column(db.String, nullable=False, unique=True)
password = db.Column(db.String, nullable=False)
trips = db.relationship("Trip", back_populates="user")
def __repr__(self):
return f"<User user_id={self.user_id} fname={self.fname} email={self.email}>"
class Trip(db.Model):
"""A trip."""
__tablename__ = "trips"
trip_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
traveler = db.Column(db.Integer, db.ForeignKey("users.user_id"), nullable=False)
destination = db.Column(db.String, nullable=False)
trip_title = db.Column(db.String) # default=destination
longitude = db.Column(db.Float) # test
latitude = db.Column(db.Float) # test
start_date = db.Column(db.Date, nullable=False)
end_date = db.Column(db.Date, nullable=False)
description = db.Column(db.String)
img = db.Column(db.String)
user = db.relationship("User", back_populates="trips")
activities = db.relationship("Activity", back_populates="trip")
reservations = db.relationship("Reservation", back_populates="trip")
tasks = db.relationship("Checklist", back_populates="trip")
def __repr__(self):
return f"<Trip trip_id={self.trip_id} destination={self.destination}>"
class Activity(db.Model):
"""Activity chosen by the user."""
__tablename__ = "activities"
activity_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
trip_id = db.Column(db.Integer, db.ForeignKey("trips.trip_id"), nullable=False)
activity_type = db.Column(db.String)
place_name = db.Column(db.String)
datetime = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
address = db.Column(db.String)
phone_number = db.Column(db.String(20))
comments = db.Column(db.String)
place_img = db.Column(db.String)
# added relationship
trip = db.relationship("Trip", back_populates="activities")
def __repr__(self):
return f"<Activity activity_id={self.activity_id} activity_type={self.activity_type} activity_name={self.activity_name}>"
class Reservation(db.Model):
"""User's reservation."""
__tablename__ = "reservations"
reservation_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
trip_id = db.Column(db.Integer, db.ForeignKey("trips.trip_id"), nullable=False)
reservation_type = db.Column(db.String, nullable=False)
confirmation_info = db.Column(db.String, nullable=False)
location = db.Column(db.String, nullable=False)
start_date = db.Column(db.Date)
end_date = db.Column(db.Date)
notes = db.Column(db.String)
trip = db.relationship("Trip", back_populates="reservations")
def __repr__(self):
return f"<Reservation reservation_id={self.reservation_id} reservation_type={self.reservation_type} location={self.location}>"
class Checklist(db.Model):
"""A checklist."""
__tablename__ = "tasks"
checklist_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
trip_id = db.Column(db.Integer, db.ForeignKey("trips.trip_id"), nullable=False)
task_title = db.Column(db.String, nullable=False) # or description
completed = db.Column(db.Boolean, default=False)
trip = db.relationship("Trip", back_populates="tasks")
def __repr__(self):
return f"<Checklist checklist_id={self.checklist_id} trip_id={self.trip_id} task_title={self.task_title}>"
class Image(db.Model):
"""City's images."""
__tablename__ = "images"
img_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
place_name = db.Column(db.String)
city_name = db.Column(db.String)
description = db.Column(db.String)
url = db.Column(db.String)
def __repr__(self):
return f"<Image img_id={self.img_id} city_name{self.city_name} place_name={self.place_name}>"
def connect_to_db(flask_app, db_uri="postgresql:///trips", echo=True):
"""Connect to dabatase."""
flask_app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
flask_app.config["SQLALCHEMY_ECHO"] = False
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = flask_app
db.init_app(flask_app)
print("Connected to the db!")
def fake_data():
"""Create some sample data."""
########### Users ###########
amanda = User(fname="Amanda", lname="Katz", email="[email protected]", password=ph.hash("1234"))
raquel = User(fname="Raquel", lname="Pfeifle", email="[email protected]", password=ph.hash("1234"))
db.session.add_all([amanda, raquel])
########### Trips ###########
san_francisco = Trip(
traveler=amanda.user_id,
destination="San Francisco, California",
trip_title="My Honeymoon",
start_date=datetime.strptime("2022-11-10", "%Y-%m-%d"),
end_date=datetime.strptime("2022-11-16", "%Y-%m-%d"),
img="Bridge")
db.session.add(san_francisco)
amanda.trips.append(san_francisco)
db.session.commit()
if __name__ == "__main__":
from server import app
# Call connect_to_db(app, echo=False) if your program output gets
# too annoying; this will tell SQLAlchemy not to print out every
# query it executes.
connect_to_db(app)
db.create_all()
fake_data()