-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
139 lines (116 loc) · 4.71 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
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from sqlalchemy import (
Column,
create_engine,
Date,
ForeignKey,
Integer,
String)
from flask_sqlalchemy import SQLAlchemy
import json
import os
# database_path = os.environ['DATABASE_URL']
database_path = 'postgres://alanabellucci@localhost:5432/fyyurapp'
db = SQLAlchemy()
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)
# db.create_all()
#----------------------------------------------------------------------------#
# Models.
#----------------------------------------------------------------------------#
class Venue(db.Model):
__tablename__ = 'venues'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
city = db.Column(db.String(120))
state = db.Column(db.String(120))
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))
genres = db.Column(db.ARRAY(db.String(120)))
website = db.Column(db.String(120))
seeking_talent = db.Column(db.Boolean, nullable = False, default = False)
seeking_description = db.Column(db.String(500))
# TODO: implement any missing fields, as a database migration using Flask-Migrate - DONE
artists = db.relationship('Artist', secondary = 'shows') #relates Venue to Show via Artist, this is a bidirectional relationship
# return a dictionary of venues
def venue_to_dictionary(self):
return{
'id' : self.id,
'name' : self.name,
'city' : self.city,
'state' : self.state,
'address' : self.address,
'phone' : self.phone,
'image_link' : self.image_link,
'facebook_link' : self.facebook_link,
'genres' : self.genres,
'website' : self.website,
'seeking_talent' : self.seeking_talent,
'seeking_description' : self.seeking_description
}
def __repr__(self):
return f'<Venue Id: {self.id}, Name: {self.name}>'
class Artist(db.Model):
__tablename__ = 'artists'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
city = db.Column(db.String(120))
state = db.Column(db.String(120))
phone = db.Column(db.String(120))
genres = db.Column(db.ARRAY(db.String(120)))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120))
website = db.Column(db.String(120))
seeking_venue = db.Column(db.Boolean, nullable = False, default = False)
seeking_description = db.Column(db.String(500))
# TODO: implement any missing fields, as a database migration using Flask-Migrate - DONE
venues = db.relationship('Venue', secondary='shows') #relates Artist to Show via Venue, this is a bidirectional relationship
# return a dictionary of artists
def artist_to_dictionary(self):
return{
'id' : self.id,
'name' : self.name,
'city' : self.city,
'state' : self.state,
'phone' : self.phone,
'genres' : self.genres,
'image_link' : self.image_link,
'facebook_link' : self.facebook_link,
'website' : self.website,
'seeking_venue' : self.seeking_venue,
'seeking_description' : self.seeking_description
}
def __repr__(self):
return f'<Artist Id: {self.id}, Name: {self.name}>'
# TODO Implement Show and Artist models, and complete all model relationships and properties, as a database migration. - DONE
class Show(db.Model):
__tablename__ = 'shows'
id = db.Column(db.Integer, primary_key = True, autoincrement = True)
venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), primary_key = True) #venues is the table name, not the name of the class
artist_id = db.Column(db.Integer, db.ForeignKey('artists.id'), primary_key = True) #artists is the table name, not the name of the class
start_time = db.Column(db.DateTime, nullable = False)
venue = db.relationship('Venue') # allows us to call Venue fields on Show
artist = db.relationship('Artist') # allows us to call Artist fields on Show
#returns a dictionary of artists for the show
def show_artist(self):
return {
'artist_id' : self.artist_id,
'artist_name' : self.artist.name,
'artist_image_link' : self.artist.image_link,
'start_time' : self.start_time
}
#returns a dictionary of venues for the show
def show_venue(self):
return {
'venue_id' : self.venue_id,
'venue_name' : self.venue.name,
'venue_image_link' : self.venue.image_link,
'start_time' : self.start_time
}