-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
244 lines (175 loc) · 6.86 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from base import db
from datetime import datetime
class BaseModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False, unique=True)
name = db.Column(db.String(120), nullable=True)
def add(self):
db.session.add(self)
db.session.commit()
if self.id:
return True
def delete(self):
db.session.delete(self)
db.session.commit()
if self.id:
return True
def update(self):
db.session.commit()
if self.id:
return True
def repr(self):
return f'{self.name}'
class Area(BaseModel):
__tablename__ = 'Area'
city = db.Column(db.String(120))
state = db.Column(db.String(120))
def add(self):
self.name = f'{self.city}, {self.state}'
db.session.add(self)
db.session.commit()
if self.id:
return True
class Venue(BaseModel):
__tablename__ = 'Venue'
address = db.Column(db.String(120))
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120), nullable=True)
seeking_talent = db.Column(db.Boolean, nullable=True)
seeking_description = db.Column(db.String(500), nullable=True)
website_link = db.Column(db.String(120), nullable=True)
area = db.relationship('Area', backref=db.backref('venues', cascade='all, delete'))
area_id = db.Column(db.Integer, db.ForeignKey('Area.id'))
@property
def past_shows(self):
return Show.query.filter_by(venue_id=self.id).filter(Show.start_time < datetime.now()).all()
@property
def upcoming_shows(self):
return Show.query.filter_by(venue_id=self.id).filter(Show.start_time > datetime.now()).all()
@property
def past_shows_count(self):
return len(self.past_shows)
@property
def upcoming_shows_count(self):
return len(self.upcoming_shows)
@property
def genres(self):
return [genre.genre.name for genre in self.genres]
class Artist(BaseModel):
__tablename__ = 'Artist'
city = db.Column(db.String(120))
state = db.Column(db.String(120))
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120), nullable=True)
website_link = db.Column(db.String(120), nullable=True)
seeking_venue = db.Column(db.Boolean, nullable=True)
seeking_description = db.Column(db.String(500), nullable=True)
@property
def past_shows(self):
return Show.query.filter_by(artist_id=self.id).filter(Show.start_time < datetime.now()).all()
@property
def upcoming_shows(self):
return Show.query.filter_by(artist_id=self.id).filter(Show.start_time > datetime.now()).all()
@property
def past_shows_count(self):
return len(self.past_shows)
class Artist_Availability(BaseModel):
__tablename__ = 'Artist_Availabilty'
start_time = db.Column(db.DateTime)
end_time = db.Column(db.DateTime)
artist = db.relationship('Artist', backref=db.backref('availability', cascade='all, delete'))
artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'))
def repr(self):
return f'{self.artist.name} at {self.venue.name}'
@property
def artist_name(self):
return self.artist.name
@property
def start_day(self):
return self.start_time.strftime("%A")
@property
def start_month(self):
return self.start_time.strftime("%B")
@property
def start_date(self):
return self.start_time.strftime("%d")
@property
def start_year(self):
return self.start_time.strftime("%Y")
@property
def end_day(self):
return self.end_time.strftime("%A")
@property
def end_month(self):
return self.end_time.strftime("%B")
@property
def end_date(self):
return self.end_time.strftime("%d")
@property
def end_year(self):
return self.end_time.strftime("%Y")
class Artist_Genre(BaseModel):
__tablename__ = 'Artist_Genre'
genre = db.relationship('Genre', backref=db.backref('artists', cascade='all, delete'))
genre_id = db.Column(db.Integer, db.ForeignKey('Genre.id'))
artist = db.relationship('Artist', backref=db.backref('genres', cascade='all, delete'))
artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'))
class Venue_Genre(BaseModel):
__tablename__ = 'Venue_Genre'
genre = db.relationship('Genre', backref=db.backref('venues', cascade='all, delete'))
genre_id = db.Column(db.Integer, db.ForeignKey('Genre.id'))
venue = db.relationship('Venue', backref=db.backref('genres', cascade='all, delete'))
venue_id = db.Column(db.Integer, db.ForeignKey('Venue.id'))
def repr(self):
return f'{self.genre.name}'
class Genre(BaseModel):
__tablename__ = 'Genre'
def repr(self):
return f'{self.name}'
class Show(BaseModel):
__tablename__ = 'Show'
start_time = db.Column(db.DateTime)
artist = db.relationship('Artist', backref=db.backref('shows', cascade='all, delete'))
venue = db.relationship('Venue', backref=db.backref('shows', cascade='all, delete'))
artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'))
venue_id = db.Column(db.Integer, db.ForeignKey('Venue.id'))
genre = db.relationship('Genre', backref=db.backref('shows', cascade='all, delete'))
genre_id = db.Column(db.Integer, db.ForeignKey('Genre.id'))
def create_show(self):
venue = Venue.query.get(self.venue_id)
artist = Artist.query.get(self.artist_id)
print(f"artist: {artist.name}")
print(f"venue: {venue.name}")
available_dates = Artist_Availability.query.filter_by(artist_id=self.artist_id)
print(f"available dates: {available_dates}")
if available_dates:
for date in available_dates:
print(f"date: {date}")
if date.start_time <= self.start_time <= date.end_time:
print(f"date: {date}")
db.session.add(self)
db.session.commit()
return True
else:
print(f"date: {date}")
return False
@property
def artist_name(self):
return self.artist.name
@property
def venue_name(self):
return self.venue.name
@property
def start_time_formatted(self):
return self.start_time.strftime("%m/%d/%Y, %H:%M:%S")
@property
def start_day(self):
return self.start_time.strftime("%A")
@property
def start_month(self):
return self.start_time.strftime("%B")
@property
def start_date(self):
return self.start_time.strftime("%d")