-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
139 lines (90 loc) · 4.7 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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
#Instatiate a SQLAlchemy object
db = SQLAlchemy()
class User(db.Model):
'''Data model for user'''
__tablename__ = 'users'
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
fname = db.Column(db.String(64), nullable = True)
lname = db.Column(db.String(64), nullable = True)
email = db.Column(db.String(100))
password = db.Column(db.String(128))
phone = db.Column(db.String(15))
def __repr__(self):
'''Returns human readable info about user object'''
return f'<User user_id={self.user_id} email={self.email}>'
#hash password functions
def set_password(self, password):
self.password = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password, password)
class MoonPhaseType(db.Model):
'''Data model for moon phase type'''
__tablename__ = 'moon_phase_types'
moon_phase_type_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(64))
description = db.Column(db.String(250))
emoji = db.Column(db.String(20))
def __repr__(self):
'''Returns human readable info about moon phase type object'''
return f'<MoonPhaseType moon_phase_type_id={self.moon_phase_type_id} title={self.title}>'
class FullMoonNickname(db.Model):
"""Data model for full moon phase nicknames"""
__tablename__ = 'full_moon_nicknames'
full_moon_nickname_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(64))
nickname_month = db.Column(db.Integer)
description = db.Column(db.String(470))
def __repr__(self):
'''Returns human readable info about full moon nickname object'''
return f'<FullMoonNickname full_moon_nickname_id={self.full_moon_nickname_id} title={self.title}>'
class MoonPhaseOccurence(db.Model):
'''Data model for moon phase occurences'''
__tablename__ = 'moon_phase_occurences'
moon_phase_occurence_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
start = db.Column(db.DateTime)
moon_phase_type_id = db.Column(db.Integer, db.ForeignKey('moon_phase_types.moon_phase_type_id'))
full_moon_nickname_id = db.Column(db.Integer, db.ForeignKey('full_moon_nicknames.full_moon_nickname_id'))
moon_phase_type = db.relationship('MoonPhaseType', backref=db.backref('moon_phase_occurences'))
full_moon_nickname = db.relationship('FullMoonNickname', backref=db.backref('moon_phase_occurences'))
def __repr__(self):
'''Returns human readable info about moon phase occurence object'''
return f'<MoonPhaseOccurence moon_phase_occurence_id={self.moon_phase_occurence_id} moon_phase_type_id ={self.moon_phase_type_id}>'
class Alert(db.Model):
'''Data model for text alert subscriptions'''
__tablename__ = 'alerts'
alert_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
moon_phase_type_id = db.Column(db.Integer, db.ForeignKey('moon_phase_types.moon_phase_type_id'))
full_moon_nickname_id = db.Column(db.Integer, db.ForeignKey('full_moon_nicknames.full_moon_nickname_id'))
is_active = db.Column(db.Boolean(False))
alert_type = db.Column(db.String(64))
user = db.relationship('User', backref=db.backref('alerts'))
moon_phase_type = db.relationship('MoonPhaseType', backref=db.backref('alerts'))
full_moon_nickname = db.relationship('FullMoonNickname', backref=db.backref('alerts'))
def __repr__(self):
'''Returns human readable info about alert object'''
return f'<Alert alert_id={self.alert_id} user_id={self.user_id} moon_phase_type_id={self.moon_phase_type_id}>'
class Solstice(db.Model):
'''Seasonal solstice and equinox occurences with dates'''
__tablename__ = 'solstices'
solstice_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(64))
start = db.Column(db.DateTime)
def __repr__(self):
'''Returns human readable info about solstice object'''
return f'<Solstice solstice_id={self.solstice_id} title={self.title}>'
def connect_to_db(app, db_uri = 'postgresql:///moon_phases'):
'''Connect the database to app'''
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__':
from server import app
connect_to_db(app)
#for using interactively
print('Connected to DB.')