-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
118 lines (69 loc) · 3.43 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
"""Models for meditation app."""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class User(db.Model):
"""User class"""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
google_id = db.Column(db.String, nullable=True)
picture = db.Column(db.String, nullable=True)
email = db.Column(db.String, unique=True)
password = db.Column(db.String)
meditations = db.relationship("Meditation", back_populates="users")
reflections = db.relationship("Reflection", back_populates="users")
def __repr__(self):
return f'<User user_id={self.user_id} email={self.email} password={self.password}>'
class Meditation(db.Model):
"""A meditation session"""
__tablename__ = "meditations"
meditation_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.user_id"))
length = db.Column(db.Integer)
date = db.Column(db.DateTime)
users = db.relationship("User", back_populates="meditations")
reflection = db.relationship("Reflection", uselist=False, back_populates='meditation')
def __repr__(self):
return f'<Meditation meditation_id={self.meditation_id} length={self.length} date={self.date}>'
class Reflection(db.Model):
"""A journal reflection"""
__tablename__ = "reflections"
meditation_id = db.Column(db.Integer, db.ForeignKey("meditations.meditation_id"), primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.user_id"))
title = db.Column(db.String, nullable=False)
content = db.Column(db.Text, nullable=False)
users = db.relationship("User", uselist=False,back_populates="reflections")
meditation = db.relationship("Meditation", uselist=False, back_populates='reflection')
tags = db.relationship("Tag", secondary='reflection_tags', back_populates="reflections")
def __repr__(self):
return f'<Reflection id={self.meditation_id} title={self.title}>'
class ReflectionTag(db.Model):
"""Tag of a specific reflection"""
__tablename__ = 'reflection_tags'
reflection_tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
reflection_id = db.Column(db.Integer, db.ForeignKey("reflections.meditation_id"), nullable=False)
tag_id = db.Column(db.Integer, db.ForeignKey("tags.tag_id"), nullable=False)
def __repr__(self):
return f"<ReflectionTag reflection_id = {self.reflection_id} tag_id = {self.tag_id}>"
class Tag(db.Model):
"""A journal tag"""
__tablename__ = "tags"
tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
tag = db.Column(db.String)
reflections = db.relationship("Reflection", secondary='reflection_tags', back_populates="tags")
def __repr__(self):
return f"{self.tag}"
def connect_to_db(app, db_name):
"""Connect to database"""
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql:///{db_name}"
app.config["SQLALCHEMY_ECHO"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
print("Connected to the db!")
if __name__ == "__main__":
from server import app
# Call connect_to_db(app, echo=False) if your program output gets
# too annoying; this will tell SQLAlchemy not to print out every
# query it executes.
connect_to_db(app, "meditations")