-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
92 lines (71 loc) · 2.2 KB
/
Copy pathdatabase.py
File metadata and controls
92 lines (71 loc) · 2.2 KB
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
from sqlalchemy import Column, Integer, String, Float
from app_setup import db, marsh
def database_create():
db.create_all()
print('Database created.')
def database_drop():
db.drop_all()
print('Database dropped.')
def database_seed():
mercury = Planet(planet_name='Mercury',
planet_type='Class D',
home_star='Sol',
mass=3.258e23,
radius=1516,
distance=35.98e6)
venus = Planet(planet_name='Venus',
planet_type='Class K',
home_star='Sol',
mass=4.867e24,
radius=3760,
distance=67.24e6)
earth = Planet(planet_name='Earth',
planet_type='Class M',
home_star='Sol',
mass=5.972e24,
radius=3959,
distance=92.96e6)
db.session.add(mercury)
db.session.add(venus)
db.session.add(earth)
test_user = User(first_name='Vitor',
last_name='Toledo',
email='vitor@toledo.com',
password='passowrd')
db.session.add(test_user)
db.session.commit()
print('Database seeded.')
# database models
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String, unique=True)
first_name = Column(String)
last_name = Column(String)
password = Column(String)
class Planet(db.Model):
__tablename__ = 'planets'
planet_id = Column(Integer, primary_key=True)
planet_name = Column(String)
planet_type = Column(String)
home_star = Column(String)
# mass in kg
mass = Column(Float)
#radius in miles
radius = Column(Float)
# miles distance from the star
distance = Column(Float)
class PlanetSchema(marsh.Schema):
class Meta:
fields = (
'planet_id',
'planet_name',
'planet_type',
'home_star',
'mass',
'radius',
'distance',
)
# Schemas to serialize the queryset to JSON
planet_schema = PlanetSchema()
planets_schema = PlanetSchema(many=True)