-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
143 lines (121 loc) · 4.65 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
from app import db
from sqlalchemy.dialects.postgresql import JSON
import solarsystem
import datetime
voyages = db.Table('voyages',
db.Column('user_id', db.Integer, db.ForeignKey('users.id'),
primary_key=True),
db.Column('celestial_bodies_id', db.Integer,
db.ForeignKey('celestial_bodies.id'), primary_key=True)
)
class CelestialBodies(db.Model):
__tablename__ = 'celestial_bodies'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
image = db.Column(db.String())
celestial_body_type = db.Column(db.String())
gravity = db.Column(db.Float())
planet_day = db.Column(db.Float())
planet_year = db.Column(db.Float())
background_image = db.Column(db.String())
landmark = db.relationship('Landmark', backref='celestial_bodies',
lazy=True)
user = db.relationship('User', secondary=voyages,
back_populates='celestial_bodies')
def __init__(self, name, image, celestial_body_type, gravity, planet_day,
planet_year, background_image):
self.name = name
self.image = image
self.celestial_body_type = celestial_body_type
self.gravity = gravity
self.planet_day = planet_day
self.planet_year = planet_year
self.background_image = background_image
def __repr__(self):
return '<id {}>'.format(self.id)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'image': self.image,
'celestial_body_type': self.celestial_body_type,
'gravity': self.gravity,
'planet_day': self.planet_day,
'planet_year': self.planet_year,
'background_image': self.background_image,
'travel': self.travel_time()
}
def travel_time(self):
miles_per_au = 92955807 # Number of miles in one astronomical unit
ship_speed = 24816 #Ship speed equal to maximum speed for manned spaceflight
if self.name == 'Moon':
moon_distance = 238900
return {'distance': moon_distance, 'travel_time': moon_distance / ship_speed}
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
planets = solarsystem.Geocentric(year=year, month=month, day=day, hour=hour, minute=minute ).position()
distance_au = planets[self.name][2]
distance_miles = distance_au * miles_per_au
travel_time = distance_miles / ship_speed
return {'distance': distance_miles, 'travel_time': travel_time}
class Landmark(db.Model):
__tablename__ = 'landmarks'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
landmark_type = db.Column(db.String())
image = db.Column(db.String())
description = db.Column(db.String())
celestial_body_id = db.Column(db.Integer,
db.ForeignKey('celestial_bodies.id'), nullable=False)
def __init__(self, name, landmark_type, image, description,
celestial_body_id):
self.name = name
self.landmark_type = landmark_type
self.image = image
self.description = description
self.celestial_body_id = celestial_body_id
def __repr__(self):
return '<id {}>'.format(self.id)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'image': self.image,
'landmark_type': self.landmark_type,
'description': self.description,
'celestial_body_id': self.celestial_body_id
}
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
user_name = db.Column(db.String())
email = db.Column(db.String(), unique=True)
password_digest = db.Column(db.String())
passenger = db.relationship('Passenger', backref='passengers', lazy=True)
celestial_bodies = db.relationship('CelestialBodies', secondary=voyages,
back_populates='user')
def __init__(self, user_name, email, password_digest):
self.user_name = user_name
self.email = email
self.password_digest = password_digest
def __repr__(self):
return '<id {}>'.format(self.id)
class Passenger(db.Model):
__tablename__ = 'passengers'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
age = db.Column(db.Integer())
weight = db.Column(db.Float())
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
def __init__(self, name, age, weight):
self.name = name
self.age = age
self.weight = weight
self.user_id = user_id
def __repr__(self):
return '<id {}>'.format(self.id)