-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
64 lines (50 loc) · 2.19 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
from sqlalchemy import Column, Integer, String, Float, DateTime
from datetime import datetime
import pytz # Work with time zones
import db
class Location(db.Base):
__tablename__ = 't_location'
id_location = Column(Integer, primary_key=True)
desc_location = Column(String, nullable=False)
def __init__(self, id_location, desc_location):
self.id_location = id_location
self.desc_location = desc_location
def __repr__(self) -> str:
return f"Location({self.id_location}, {self.desc_location})"
def __str__(self) -> str:
return self.desc_location
class Weather(db.Base):
__tablename__ = 't_weather'
id = Column(Integer, primary_key=True)
id_location = Column(Integer, nullable=False)
date = Column(DateTime, nullable=False)
temp_high = Column(Float)
temp_low = Column(Float)
rain_total = Column(Float)
humidity_high = Column(Float)
humidity_low = Column(Float)
wind_max = Column(Float)
uv_high = Column(Float)
pressure_max = Column(Float)
pressure_min = Column(Float)
def __repr__(self) -> str:
return f"Weather({self.id_location}, {self.temp_high})"
def __str__(self) -> str:
return super().__str__()
@staticmethod
def write_weather(dict_weather_data):
weather = Weather()
weather.id_location = 1 # Cercedilla
weather.date = datetime.now(pytz.timezone('Europe/Madrid')
).strftime("%Y-%m-%d %H:%M")
weather.temp_high = float(dict_weather_data["metric"]["tempHigh"])
weather.temp_low = float(dict_weather_data["metric"]["tempLow"])
weather.rain_total = float(dict_weather_data["metric"]["precipTotal"])
weather.humidity_high = float(dict_weather_data["humidityHigh"])
weather.humidity_low = float(dict_weather_data["humidityLow"])
weather.wind_max = float(dict_weather_data["metric"]["windgustHigh"])
weather.uv_high = float(dict_weather_data["uvHigh"])
weather.pressure_max = float(dict_weather_data["metric"]["pressureMax"])
weather.pressure_min = float(dict_weather_data["metric"]["pressureMin"])
db.session.add(weather)
db.session.commit()