Browse Source

Tabla de canciones y álbumes

politica
Celestino Rey 1 year ago
parent
commit
7d306528b2
9 changed files with 163 additions and 25 deletions
  1. +23
    -7
      LyricsPy/app.py
  2. BIN
      LyricsPy/instance/songs.db
  3. +11
    -2
      LyricsPy/models.py
  4. +85
    -0
      LyricsPy/static/style.css
  5. +17
    -0
      LyricsPy/templates/add_album.html
  6. +10
    -9
      LyricsPy/templates/add_song.html
  7. +5
    -4
      LyricsPy/templates/base.html
  8. +10
    -1
      LyricsPy/templates/index.html
  9. +2
    -2
      LyricsPy/templates/song.html

+ 23
- 7
LyricsPy/app.py View File

@ -1,5 +1,5 @@
from flask import Flask, render_template, request, redirect, url_for from flask import Flask, render_template, request, redirect, url_for
from models import db, Song
from models import db, Song, Album
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db'
@ -15,29 +15,45 @@ def before_request():
@app.route('/') @app.route('/')
def index(): def index():
songs = Song.query.all() songs = Song.query.all()
return render_template('index.html', songs=songs)
albums = Album.query.all()
return render_template('index.html', songs=songs, albums=albums)
@app.route('/song/<int:song_id>') @app.route('/song/<int:song_id>')
def song(song_id): def song(song_id):
song = Song.query.get_or_404(song_id) song = Song.query.get_or_404(song_id)
return render_template('song.html', song=song) return render_template('song.html', song=song)
@app.route('/add', methods=['GET', 'POST'])
@app.route('/add_song', methods=['GET', 'POST'])
def add_song(): def add_song():
if request.method == 'POST': if request.method == 'POST':
title = request.form['title'] title = request.form['title']
author = request.form['author'] author = request.form['author']
year = request.form['year']
album = request.form['album']
album_id = request.form['album_id']
lyrics = request.form['lyrics'] lyrics = request.form['lyrics']
new_song = Song(title=title, author=author, year=year, album=album, lyrics=lyrics)
new_song = Song(title=title, author=author, album_id=album_id, lyrics=lyrics)
db.session.add(new_song) db.session.add(new_song)
db.session.commit() db.session.commit()
return redirect(url_for('index')) return redirect(url_for('index'))
return render_template('add_song.html')
albums = Album.query.all()
return render_template('add_song.html', albums=albums)
@app.route('/add_album', methods=['GET', 'POST'])
def add_album():
if request.method == 'POST':
name = request.form['name']
artist = request.form['artist']
year = request.form['year']
new_album = Album(name=name, artist=artist, year=year)
db.session.add(new_album)
db.session.commit()
return redirect(url_for('index'))
return render_template('add_album.html')
if __name__ == '__main__': if __name__ == '__main__':
with app.app_context(): with app.app_context():


BIN
LyricsPy/instance/songs.db View File


+ 11
- 2
LyricsPy/models.py View File

@ -2,12 +2,21 @@ from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() db = SQLAlchemy()
class Album(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
artist = db.Column(db.String(100), nullable=False)
year = db.Column(db.Integer, nullable=False)
songs = db.relationship('Song', backref='album', lazy=True)
def __repr__(self):
return f'<Album {self.name} by {self.artist}>'
class Song(db.Model): class Song(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False) title = db.Column(db.String(100), nullable=False)
author = 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)
album_id = db.Column(db.Integer, db.ForeignKey('album.id'), nullable=False)
lyrics = db.Column(db.Text, nullable=False) lyrics = db.Column(db.Text, nullable=False)
def __repr__(self): def __repr__(self):


+ 85
- 0
LyricsPy/static/style.css View File

@ -0,0 +1,85 @@
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
color: #343a40;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1, h2 {
color: #007bff;
}
nav {
margin-bottom: 20px;
}
nav a {
margin-right: 15px;
text-decoration: none;
color: #007bff;
}
nav a:hover {
text-decoration: underline;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-top: 10px;
margin-bottom: 5px;
}
input[type="text"],
input[type="number"],
textarea,
select {
padding: 10px;
border: 1px solid #ced4da;
border-radius: 4px;
font-size: 16px;
}
button {
margin-top: 20px;
padding: 10px 15px;
background-color: #007bff;
border: none;
color: white;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
pre {
background-color: #f8f9fa;
padding: 10px;
border: 1px solid #ced4da;
border-radius: 4px;
}

+ 17
- 0
LyricsPy/templates/add_album.html View File

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block content %}
<h2>Añadir nuevo Álbum</h2>
<form method="POST">
<label for="name">Nombre:</label>
<input type="text" id="name" name="name" required>
<label for="artist">Artista:</label>
<input type="text" id="artist" name="artist" required>
<label for="year">Año:</label>
<input type="number" id="year" name="year" required>
<button type="submit">Añadir álbum</button>
</form>
{% endblock %}

+ 10
- 9
LyricsPy/templates/add_song.html View File

@ -1,23 +1,24 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<h2>Add New Song</h2>
<h2>Añadir nueva canción</h2>
<form method="POST"> <form method="POST">
<label for="title">Title:</label>
<label for="title">Título:</label>
<input type="text" id="title" name="title" required> <input type="text" id="title" name="title" required>
<label for="author">Author:</label>
<label for="author">Autor:</label>
<input type="text" id="author" name="author" required> <input type="text" id="author" name="author" required>
<label for="year">Year:</label>
<input type="number" id="year" name="year" required>
<label for="album">Album:</label>
<input type="text" id="album" name="album" required>
<label for="album_id">Álbum:</label>
<select id="album_id" name="album_id" required>
{% for album in albums %}
<option value="{{ album.id }}">{{ album.name }} by {{ album.artist }}</option>
{% endfor %}
</select>
<label for="lyrics">Lyrics:</label> <label for="lyrics">Lyrics:</label>
<textarea id="lyrics" name="lyrics" required></textarea> <textarea id="lyrics" name="lyrics" required></textarea>
<button type="submit">Add Song</button>
<button type="submit">Añadir canción</button>
</form> </form>
{% endblock %} {% endblock %}

+ 5
- 4
LyricsPy/templates/base.html View File

@ -2,15 +2,16 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>My Song Lyrics</title>
<title>Mis Letras de Canciones</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>My Song Lyrics</h1>
<h1>Mis Letras de Canciones</h1>
<nav> <nav>
<a href="{{ url_for('index') }}">Home</a>
<a href="{{ url_for('add_song') }}">Add Song</a>
<a href="{{ url_for('index') }}">Inicio</a>
<a href="{{ url_for('add_song') }}">Añadir canción</a>
<a href="{{ url_for('add_album') }}">Añadir Álbum</a>
</nav> </nav>
<hr> <hr>
{% block content %}{% endblock %} {% block content %}{% endblock %}


+ 10
- 1
LyricsPy/templates/index.html View File

@ -1,7 +1,7 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<h2>Song List</h2>
<h2>Lista de canciones</h2>
<ul> <ul>
{% for song in songs %} {% for song in songs %}
<li> <li>
@ -9,4 +9,13 @@
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<hr>
<h2>Álbumes</h2>
<ul>
{% for album in albums %}
<li>{{ album.name }} by {{ album.artist }} ({{ album.year }})</li>
{% endfor %}
</ul>
<hr>
<a href="{{ url_for('add_album') }}" class="button">Añadir nuevo álbum</a>
{% endblock %} {% endblock %}

+ 2
- 2
LyricsPy/templates/song.html View File

@ -3,7 +3,7 @@
{% block content %} {% block content %}
<h2>{{ song.title }}</h2> <h2>{{ song.title }}</h2>
<p><strong>Author:</strong> {{ song.author }}</p> <p><strong>Author:</strong> {{ song.author }}</p>
<p><strong>Year:</strong> {{ song.year }}</p>
<p><strong>Album:</strong> {{ song.album }}</p>
<p><strong>Year:</strong> {{ song.album.year }}</p>
<p><strong>Album:</strong> {{ song.album.name }}</p>
<pre>{{ song.lyrics }}</pre> <pre>{{ song.lyrics }}</pre>
{% endblock %} {% endblock %}

Loading…
Cancel
Save