-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
203 lines (137 loc) · 7.3 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""database model and functions"""
# Copyright (c) 2017 Bonnie Schulkin
# This file is part of My Heavens.
# My Heavens is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# My Heavens is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
# You should have received a copy of the GNU Affero General Public License
# along with My Heavens. If not, see <http://www.gnu.org/licenses/>.
from flask_sqlalchemy import SQLAlchemy
# This is the connection to the PostgreSQL database; we're getting this through
# the Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class Constellation(db.Model):
"""Constellation names and codes."""
__tablename__ = "constellations"
##### columns #####
const_code = db.Column(db.String(3), primary_key=True)
name = db.Column(db.String(64), nullable=False)
##### relationships #####
bound_vertices = db.relationship("BoundVertex",
secondary="const_bound_vertices",
order_by="ConstBoundVertex.index")
stars = db.relationship("Star")
line_groups = db.relationship("ConstLineGroup")
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Constellation code=%s name=%s>" % (self.const_code, self.name)
class Star(db.Model):
"""Star in the universe."""
__tablename__ = "stars"
star_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
const_code = db.Column(db.String(3), db.ForeignKey("constellations.const_code"))
name = db.Column(db.String(128), nullable=True)
ra = db.Column(db.Numeric(6, 3), nullable=False) # in degrees
dec = db.Column(db.Numeric(6, 3), nullable=False) # in degrees
distance = db.Column(db.Numeric(12, 2), nullable=True) # in parsecs
magnitude = db.Column(db.Numeric(4, 2), nullable=False)
absolute_magnitude = db.Column(db.Numeric(5, 3), nullable=True)
spectrum = db.Column(db.String(16), nullable=False)
color_index = db.Column(db.Numeric(4, 3), nullable=True)
color = db.Column(db.String(7), nullable=False)
constellation = db.relationship("Constellation")
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Star star_id=%s name=%s ra=%s dec=%s>" % (self.star_id,
self.name,
self.ra,
self.dec)
class ConstLineVertex(db.Model):
"""a vertex that forms one endpoint of a constellation line.
this corresponds to a star"""
__tablename__ = "const_line_vertices"
const_line_vertex_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
const_line_group_id = db.Column(db.Integer,
db.ForeignKey("const_line_group.const_line_group_id"),
nullable=False)
star_id = db.Column(db.Integer,
db.ForeignKey("stars.star_id"),
nullable=False)
# where in the constellation line sequence is this?
index = db.Column(db.Integer, nullable=False)
##### relationships #####
star = db.relationship("Star")
def __repr__(self):
"""Provide helpful representation when printed."""
return "<ConstLineVertex id=%s const_line_group_id=%s star_id=%s>" % (
self.const_line_vertex_id,
self.const_line_group_id,
self.star_id)
class ConstLineGroup(db.Model):
"""a group of vertices that make up a continuous constellation line"""
__tablename__ = "const_line_group"
const_line_group_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
const_code = db.Column(db.String(3), db.ForeignKey("constellations.const_code"), nullable=False)
# be able to get a list of stars in this group easily
constline_vertices = db.relationship("ConstLineVertex",
order_by="ConstLineVertex.index")
class BoundVertex(db.Model):
"""Vertices of constellation boundaries, not attached to constellations.
Each vertex can be a part of multiple constellation boundaries, and
each constellation boundary is made up of multiple vertices."""
__tablename__ = "bound_vertices"
vertex_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
ra = db.Column(db.Numeric(6, 3), nullable=False) # in degrees
dec = db.Column(db.Numeric(6, 3), nullable=False) # in degrees
def __repr__(self):
"""Provide helpful representation when printed."""
return "<BoundVertex id=%s ra=%s dec=%s>" % (self.vertex_id,
self.ra,
self.dec)
class ConstBoundVertex(db.Model):
"""Constellation boundary vertices.
Association table between Constellation and BoundVertex"""
__tablename__ = "const_bound_vertices"
const_bound_vertex_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
const_code = db.Column(db.String(3),
db.ForeignKey('constellations.const_code'),
nullable=False)
vertex_id = db.Column(db.Integer,
db.ForeignKey('bound_vertices.vertex_id'),
nullable=False)
# the order in the vertex list for this constellation vertex
index = db.Column(db.Integer, nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<ConstBoundVertex id=%s const_code=%s vertex_id=%s>" % (
self.const_bound_vertex_id,
self.const_code,
self.vertex_id)
##############################################################################
# Helper functions
def connect_to_db(app, db_uri='postgresql:///star_chart'):
"""Connect the database to our Flask app."""
# Configure to use our PostgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.config['SQLALCHEMY_ECHO'] = True
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print("Connected to DB.")