-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
111 lines (74 loc) · 3.5 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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
db = SQLAlchemy()
class City(db.Model):
""" Cities with their lat, lon, and country info """
__tablename__ = 'cities'
location_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
city_name = db.Column(db.String, nullable=False)
iso2 = db.Column(db.String) # country 2-letter code
country = db.Column(db.String, nullable=False)
lat = db.Column(db.Float, nullable=False)
lon = db.Column(db.Float, nullable=False)
pop = db.Column(db.Integer)
continent_id = db.Column(db.Integer, db.ForeignKey('continents.continent_id'))
continent = db.relationship('Continent', backref='cities')
#climates = a list of Climate objects by months
def __repr__(self):
"""Show coordinate's lat and long"""
return f'<city={self.city_name}: lat={self.lat} lon={self.lon}>'
class Continent(db.Model):
"""List of countries and their continents"""
__tablename__ = 'continents'
continent_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
continent_name = db.Column(db.String, nullable=False)
continent_code = db.Column(db.String, nullable=False)
# cities = a list of City objects belonging to a given continent
# climates = a list of Climate objects associated with each continent
def __repr__(self):
"""Show country name, 2_letter_country_code, continent"""
return f'<continent_code={self.continent_code} continent={self.continent_name}>'
class Climate(db.Model):
""" Climate in a given month with temperature in celcius and precipitation in millimeter"""
__tablename__ = 'climates'
climate_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True,)
month = db.Column(db.Integer)
tavg = db.Column(db.Float)
tmin = db.Column(db.Float)
tmax = db.Column(db.Float)
prcp = db.Column(db.Integer)
pres = db.Column(db.Float)
tsun = db.Column(db.Integer)
location_id = db.Column(db.Integer, db.ForeignKey('cities.location_id'))
continent_id = db.Column(db.Integer, db.ForeignKey('continents.continent_id'))
city = db.relationship('City', backref='climates')
continent = db.relationship('Continent', backref='climates')
def __repr__(self):
"""Show month & avg temp """
return f'<month={self.month} tavg={self.tavg}>'
def connect_to_db(flask_app, db_uri='postgresql:///climates', echo=True):
# Configuration: https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/
flask_app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
flask_app.config['SQLALCHEMY_ECHO'] = echo
flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = flask_app
db.init_app(flask_app)
print('Connected to the db!')
if __name__ == '__main__':
from server import app
connect_to_db(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.
# Relationship:
# One city or continent can have multiple climates.
# One continent can have multiple cities
# Reference: https://fellowship.hackbrightacademy.com/materials/pt7g/exercises/ratings-v2/
# Reference: https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html
# In many-to-one, the many table holds the foreign key.