forked from SI364-Winter2018/Songs-And-Images-App
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_app.py
More file actions
124 lines (92 loc) · 4.61 KB
/
main_app.py
File metadata and controls
124 lines (92 loc) · 4.61 KB
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
#__author__ == "Jackie Cohen (jczetta)"
import os
from flask import Flask, render_template, session, redirect, url_for # tools that will make it easier to build on things
from flask_sqlalchemy import SQLAlchemy # handles database stuff for us - need to pip install flask_sqlalchemy in your virtual env, environment, etc to use this and run this
# Application configurations
app = Flask(__name__)
app.debug = True
app.use_reloader = True
app.config['SECRET_KEY'] = 'hard to guess string for app security adgsdfsadfdflsdfsj'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./sample_songs.db' # TODO: decide what your new database name will be -- that has to go here
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Set up Flask debug stuff
db = SQLAlchemy(app) # For database use
session = db.session # to make queries easy
#########
######### Everything above this line is important/useful setup, not problem-solving.
#########
##### Set up Models #####
# Set up association Table between artists and albums
collections = db.Table('collections',db.Column('album_id',db.Integer, db.ForeignKey('albums.id')),db.Column('artist_id',db.Integer, db.ForeignKey('artists.id')))
class Album(db.Model):
__tablename__ = "albums"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))
artists = db.relationship('Artist',secondary=collections,backref=db.backref('albums',lazy='dynamic'),lazy='dynamic')
songs = db.relationship('Song',backref='Album')
class Artist(db.Model):
__tablename__ = "artists"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))
songs = db.relationship('Song',backref='Artist')
def __repr__(self):
return "{} (ID: {})".format(self.name,self.id)
class Song(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64),unique=True) # Only unique title songs can exist in this data model
album_id = db.Column(db.Integer, db.ForeignKey("albums.id")) #ok to be null for now
artist_id = db.Column(db.Integer, db.ForeignKey("artists.id")) # ok to be null for now
genre = db.Column(db.String(64)) # ok to be null
# keeping genre as atomic element here even though in a more complex database it could be its own table and be referenced here
def __repr__(self):
return "{} by {} | {}".format(self.title,self.artist_id, self.genre)
##### Helper functions #####
### For database additions
### Relying on global session variable above existing
def get_or_create_artist(artist_name):
artist = Artist.query.filter_by(name=artist_name).first()
if artist:
return artist
else:
artist = Artist(name=artist_name)
session.add(artist)
session.commit()
return artist
##### Set up Controllers (route functions) #####
## Main route
@app.route('/')
def index():
songs = Song.query.all()
num_songs = len(songs)
return render_template('index.html', num_songs=num_songs)
@app.route('/song/new/<title>/<artist>/<genre>/')
def new_song(title, artist, genre):
if Song.query.filter_by(title=title).first(): # if there is a song by that title
return "That song already exists! Go back to the main app!"
else:
artist = get_or_create_artist(artist)
song = Song(title=title, artist_id=artist.id,genre=genre)
session.add(song)
session.commit()
return "New song: {} by {}. Check out the URL for ALL songs to see the whole list.".format(song.title, artist.name)
@app.route('/all_songs')
def see_all():
all_songs = [] # Will be be tuple list of title, genre
songs = Song.query.all()
for s in songs:
artist = Artist.query.filter_by(id=s.artist_id).first() # get just one artist instance
all_songs.append((s.title,artist.name, s.genre)) # get list of songs with info to easily access [not the only way to do this]
return render_template('all_songs.html',all_songs=all_songs) # check out template to see what it's doing with what we're sending!
@app.route('/all_artists')
def see_all_artists():
artists = Artist.query.all()
names = []
for a in artists:
num_songs = len(Song.query.filter_by(artist_id=a.id).all())
newtup = (a.name,num_songs)
names.append(newtup) # names will be a list of tuples
return render_template('all_artists.html',artist_names=names)
if __name__ == '__main__':
db.create_all() # This will create database in current directory, as set up, if it doesn't exist, but won't overwrite if you restart - so no worries about that
app.run() # run with this: python main_app.py runserver