diff --git a/LyricsPy/app.py b/LyricsPy/app.py new file mode 100644 index 0000000..b9dfefc --- /dev/null +++ b/LyricsPy/app.py @@ -0,0 +1,45 @@ +from flask import Flask, render_template, request, redirect, url_for +from models import db, Song + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db.init_app(app) + +@app.before_request +def before_request(): + if not hasattr(app, 'db_initialized'): + db.create_all() + app.db_initialized = True + +@app.route('/') +def index(): + songs = Song.query.all() + return render_template('index.html', songs=songs) + +@app.route('/song/') +def song(song_id): + song = Song.query.get_or_404(song_id) + return render_template('song.html', song=song) + +@app.route('/add', methods=['GET', 'POST']) +def add_song(): + if request.method == 'POST': + title = request.form['title'] + author = request.form['author'] + year = request.form['year'] + album = request.form['album'] + lyrics = request.form['lyrics'] + + new_song = Song(title=title, author=author, year=year, album=album, lyrics=lyrics) + db.session.add(new_song) + db.session.commit() + + return redirect(url_for('index')) + + return render_template('add_song.html') + +if __name__ == '__main__': + with app.app_context(): + db.create_all() + app.run(debug=True) diff --git a/LyricsPy/instance/songs.db b/LyricsPy/instance/songs.db new file mode 100644 index 0000000..664751f Binary files /dev/null and b/LyricsPy/instance/songs.db differ diff --git a/LyricsPy/models.py b/LyricsPy/models.py new file mode 100644 index 0000000..2ea8fdc --- /dev/null +++ b/LyricsPy/models.py @@ -0,0 +1,14 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +class Song(db.Model): + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(100), nullable=False) + author = db.Column(db.String(100), nullable=False) + year = db.Column(db.Integer, nullable=False) + album = db.Column(db.String(100), nullable=False) + lyrics = db.Column(db.Text, nullable=False) + + def __repr__(self): + return f'' diff --git a/LyricsPy/templates/add_song.html b/LyricsPy/templates/add_song.html new file mode 100644 index 0000000..7e15059 --- /dev/null +++ b/LyricsPy/templates/add_song.html @@ -0,0 +1,23 @@ +{% extends 'base.html' %} + +{% block content %} +

Add New Song

+
+ + + + + + + + + + + + + + + + +
+{% endblock %} diff --git a/LyricsPy/templates/base.html b/LyricsPy/templates/base.html new file mode 100644 index 0000000..f641da7 --- /dev/null +++ b/LyricsPy/templates/base.html @@ -0,0 +1,19 @@ + + + + + My Song Lyrics + + + +
+

My Song Lyrics

+ +
+ {% block content %}{% endblock %} +
+ + diff --git a/LyricsPy/templates/index.html b/LyricsPy/templates/index.html new file mode 100644 index 0000000..c86dc76 --- /dev/null +++ b/LyricsPy/templates/index.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} + +{% block content %} +

Song List

+ +{% endblock %} diff --git a/LyricsPy/templates/song.html b/LyricsPy/templates/song.html new file mode 100644 index 0000000..429ddfa --- /dev/null +++ b/LyricsPy/templates/song.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +

{{ song.title }}

+

Author: {{ song.author }}

+

Year: {{ song.year }}

+

Album: {{ song.album }}

+
{{ song.lyrics }}
+{% endblock %}