-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
182 lines (144 loc) · 4.69 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
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
import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Boolean, Column, Integer, String, DateTime
from sqlalchemy.schema import ForeignKey
from sqlalchemy.orm import relationship
import json
# database_path ='postgresql://postgres:postgres@localhost:5432/capstone'
database_path = os.environ['DATABASE_URL']
if database_path[:10] != 'postgresql':
database_path = database_path.replace('postgres', 'postgresql')
db = SQLAlchemy()
'''
setup_db(app)
setup flask application with SQLAlchemy service
'''
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
def test_setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
'''
Table: Podcasts
List of all Podcasts
'''
class Podcast(db.Model):
__tablename__ = 'podcasts'
id = Column(Integer, primary_key=True)
author = Column(String)
name = Column(String)
image_link = Column(String(500))
podcast_link = Column(String(120))
speakers = relationship(
'Speaker',
secondary='episodes',
backref='podcasts',
viewonly=True
)
episodes = relationship('Episode', viewonly=True)
def __init__(self, author, name, image, podcast_link):
self.author = author
self.name = name
self.image_link = image
self.podcast_link = podcast_link
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'author': self.author,
'name': self.name,
'image': self.image_link,
'podcast_link': self.podcast_link,
'speakers': len(self.speakers),
'episodes': len(self.episodes)
}
'''
Table: Speakers
List of Guest Speakers
'''
class Speaker(db.Model):
__tablename__ = 'speakers'
id = Column(Integer, primary_key=True)
name = Column(String)
image_link = Column(String(500))
twitter_link = Column(String(120))
website_link = Column(String(120))
episodes = relationship('Episode', viewonly=True)
def __init__(self, name, image_link, twitter_link, website_link):
self.name = name
self.image_link = image_link
self.twitter_link = twitter_link
self.website_link = website_link
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'name': self.name,
'image_link': self.image_link,
'twitter_link': self.twitter_link,
'website_link': self.website_link,
'episodes': len(self.episodes)
}
'''
Table: Episodes
List if Episodes
'''
class Episode(db.Model):
__tablename__ = 'episodes'
id = Column(Integer, primary_key=True)
title = Column(String)
topics = Column(String)
podcast_link = Column(String)
speaker_id = Column(Integer, ForeignKey('speakers.id'), nullable=False)
speaker_name = relationship('Speaker', viewonly=True)
podcast_id = Column(Integer, ForeignKey('podcasts.id'), nullable=False)
podcast_name = relationship('Podcast', viewonly=True)
# start_time = Column(DateTime())
# finished = Column(Boolean, default=False, nullable=False)
def __init__(self, title, topics, podcast_link, podcast_id, speaker_id):
self.title = title
self.topics = topics
self.podcast_link = podcast_link
self.speaker_id = speaker_id
self.podcast_id = podcast_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'title': self.title,
'topics': self.topics,
'podcast_link': self.podcast_link,
'speaker_id': self.speaker_id,
'speaker_name': self.speaker_name.name,
'podcast_id': self.podcast_id,
'podcast_name': self.podcast_name.name,
# 'published': self.start_time,
# 'finished': self.finished
}