-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
60 lines (48 loc) · 1.92 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
from peewee import *
import datetime
db = SqliteDatabase('weather.db')
class Location(Model):
id = PrimaryKeyField()
name = CharField(max_length=50)
province = CharField(max_length=50, null=True)
country = CharField(max_length=50)
latitude = FloatField()
longitude = FloatField()
class Meta:
database = db
class Forecast(Model):
location_id = ForeignKeyField(Location, backref='weather')
created_at = DateTimeField(default=datetime.datetime.now)
date = DateTimeField() #? Will DateField work here?
icon = CharField(max_length=10)
description = CharField(max_length=100)
temperature = FloatField()
humidity = IntegerField()
wind_speed = FloatField()
wind_direction = IntegerField()
class Meta:
database = db
def get_readable_weather_data(self):
readable_weather_data = {
'icon': self.icon,
#'description': self.get_proper_case_description(),
'description': self.description,
'temperature': f"{round(self.temperature)}°C",
'humidity': f"{self.humidity}%",
'wind_speed': f"{round(self.wind_speed)}km/s",
'wind_direction': self.get_readable_wind_direction()
}
return readable_weather_data
# def get_proper_case_description(self):
# return self.description.title()
def get_readable_wind_direction(self):
# I'm not smart enough to make this, someone else did it in Javascript:
# https://stackoverflow.com/questions/48750528/get-direction-from-degrees
# Define the direction names
directions = ['north', 'north-east', 'east', 'south-east', 'south', 'south-west', 'west', 'north-west']
# Convert degrees to an index
index = round(self.wind_direction / 45) % 8
# Return the direction name
return directions[index]
db.connect()
db.create_tables([Location, Forecast])