Skip to content

Commit 9952f27

Browse files
committed
Initial
0 parents  commit 9952f27

13 files changed

+197
-0
lines changed

__pycache__/app.cpython-37.pyc

367 Bytes
Binary file not shown.

__pycache__/db_setup.cpython-37.pyc

691 Bytes
Binary file not shown.

__pycache__/forms.cpython-37.pyc

552 Bytes
Binary file not shown.

__pycache__/models.cpython-37.pyc

1.34 KB
Binary file not shown.

app.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# app.py
2+
3+
from flask import Flask
4+
from flask_sqlalchemy import SQLAlchemy
5+
6+
app = Flask(__name__)
7+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mymusic.db'
8+
app.secret_key = "flask rocks!"
9+
10+
db = SQLAlchemy(app)

db_creator.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from sqlalchemy import create_engine, ForeignKey
2+
from sqlalchemy import Column, Date, Integer, String
3+
from sqlalchemy.ext.declarative import declarative_base
4+
from sqlalchemy.orm import relationship, backref
5+
6+
engine = create_engine('sqlite:///mymusic.db', echo=True)
7+
Base = declarative_base()
8+
9+
10+
class Artist(Base):
11+
__tablename__ = "artists"
12+
13+
id = Column(Integer, primary_key=True)
14+
name = Column(String)
15+
16+
def __init__(self, name):
17+
""""""
18+
self.name = name
19+
20+
def __repr__(self):
21+
return "<Artist: {}>".format(self.name)
22+
23+
24+
class Album(Base):
25+
""""""
26+
__tablename__ = "albums"
27+
28+
id = Column(Integer, primary_key=True)
29+
title = Column(String)
30+
release_date = Column(Date)
31+
publisher = Column(String)
32+
media_type = Column(String)
33+
34+
artist_id = Column(Integer, ForeignKey("artists.id"))
35+
artist = relationship("Artist", backref=backref(
36+
"albums", order_by=id))
37+
38+
def __init__(self, title, release_date, publisher, media_type):
39+
""""""
40+
self.title = title
41+
self.release_date = release_date
42+
self.publisher = publisher
43+
self.media_type = media_type
44+
45+
46+
# create tables
47+
Base.metadata.create_all(engine)

db_setup.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.orm import scoped_session, sessionmaker
3+
from sqlalchemy.ext.declarative import declarative_base
4+
5+
engine = create_engine('sqlite:///mymusic.db', convert_unicode=True)
6+
db_session = scoped_session(sessionmaker(autocommit=False,
7+
autoflush=False,
8+
bind=engine))
9+
Base = declarative_base()
10+
Base.query = db_session.query_property()
11+
12+
def init_db():
13+
import models
14+
Base.metadata.create_all(bind=engine)

forms.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# forms.py
2+
3+
from wtforms import Form, StringField, SelectField, validators
4+
5+
class MusicSearchForm(Form):
6+
choices = [('Artist', 'Artist'),
7+
('Album', 'Album'),
8+
('Publisher', 'Publisher')]
9+
select = SelectField('Search for music:', choices=choices)
10+
search = StringField('')

main.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# main.py
2+
3+
from app import app
4+
from db_setup import init_db, db_session
5+
from forms import MusicSearchForm
6+
from flask import flash, render_template, request, redirect
7+
from models import Album
8+
9+
init_db()
10+
11+
12+
@app.route('/', methods=['GET', 'POST'])
13+
def index():
14+
search = MusicSearchForm(request.form)
15+
if request.method == 'POST':
16+
return search_results(search)
17+
18+
return render_template('index.html', form=search)
19+
20+
21+
@app.route('/results')
22+
def search_results(search):
23+
results = []
24+
search_string = search.data['search']
25+
26+
if search.data['search'] == '':
27+
qry = db_session.query(Album)
28+
results = qry.all()
29+
30+
if not results:
31+
flash('No results found!')
32+
return redirect('/')
33+
else:
34+
# display results
35+
return render_template('results.html', table=table)
36+
37+
if __name__ == '__main__':
38+
import os
39+
if 'WINGDB_ACTIVE' in os.environ:
40+
app.debug = False
41+
app.run(port=5001)

models.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from app import db
2+
3+
4+
class Artist(db.Model):
5+
__tablename__ = "artists"
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
name = db.Column(db.String)
9+
10+
def __init__(self, name):
11+
""""""
12+
self.name = name
13+
14+
def __repr__(self):
15+
return "<Artist: {}>".format(self.name)
16+
17+
18+
class Album(db.Model):
19+
""""""
20+
__tablename__ = "albums"
21+
22+
id = db.Column(db.Integer, primary_key=True)
23+
title = db.Column(db.String)
24+
release_date = db.Column(db.Date)
25+
publisher = db.Column(db.String)
26+
media_type = db.Column(db.String)
27+
28+
artist_id = db.Column(db.Integer, db.ForeignKey("artists.id"))
29+
artist = db.relationship("Artist", backref=db.backref(
30+
"albums", order_by=id), lazy=True)
31+
32+
def __init__(self, title, release_date, publisher, media_type):
33+
""""""
34+
self.title = title
35+
self.release_date = release_date
36+
self.publisher = publisher
37+
self.media_type = media_type

mymusic.db

Whitespace-only changes.

templates/_formhelpers.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% macro render_field(field) %}
2+
<dt>{{ field.label }}
3+
<dd>{{ field(**kwargs)|safe }}
4+
{% if field.errors %}
5+
<ul class=errors>
6+
{% for error in field.errors %}
7+
<li>{{ error }}</li>
8+
{% endfor %}
9+
</ul>
10+
{% endif %}
11+
</dd>
12+
{% endmacro %}

templates/index.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<doctype html>
2+
<head>
3+
<title>Flask Music Database</title>
4+
</head>
5+
6+
<h2>Flask - SQLite Database</h2>
7+
8+
{% with messages = get_flashed_messages() %}
9+
{% if messages %}
10+
<ul class=flashes>
11+
{% for message in messages %}
12+
<li>{{ message }}</li>
13+
{% endfor %}
14+
</ul>
15+
{% endif %}
16+
{% endwith %}
17+
18+
{% from "_formhelpers.html" import render_field %}
19+
<form method=post>
20+
<dl>
21+
{{ render_field(form.select) }}
22+
<p>
23+
{{ render_field(form.search) }}
24+
</dl>
25+
<p><input type=submit value=Search>
26+
</form>

0 commit comments

Comments
 (0)