Browse Source

Se añade libro desde autor

politica
Celestino Rey 1 year ago
parent
commit
f6d6e70fcc
5 changed files with 99 additions and 8 deletions
  1. +43
    -1
      LibrosPy/servicios/libros/paginas.py
  2. +48
    -0
      LibrosPy/servicios/libros/templates/add_libro2autor.html
  3. +4
    -0
      LibrosPy/servicios/libros/templates/autor.html
  4. +2
    -4
      LibrosPy/servicios/libros/templates/index.html
  5. +2
    -3
      LibrosPy/servicios/libros/templates/libroscard.html

+ 43
- 1
LibrosPy/servicios/libros/paginas.py View File

@ -96,6 +96,48 @@ def add_libro():
return render_template('nohayautores.html')
@bp.route('/add_libro2autor/<int:autor_id>', methods=['GET', 'POST'])
@login_required
def add_libro2autor(autor_id):
autor = Autores.query.filter_by(id=autor_id).first() # obtiene el autor cuyo id hemos recibido
if request.method == 'POST':
anno = request.form['anno']
titulo = request.form['titulo']
portada = request.files['portada']
# Verificar que se ha seleccionado un archivo
if portada.filename == '':
return "No selected file", 400
if portada:
image_filename = secure_filename(portada.filename)
portada.save(os.path.join(current_app.config['UPLOAD_FOLDER'], image_filename))
else:
image_filename = None
epub = request.files['epub']
# Verificar que se ha seleccionado un archivo
if epub.filename == '':
return "No selected file", 400
if epub:
epub_filename = secure_filename(epub.filename)
epub.save(os.path.join(current_app.config['UPLOAD_FOLDER'], epub_filename))
else:
epub_filename = None
new_libro = Libros(anno=anno, autor_id=autor_id, titulo=titulo, portada=image_filename, epub=epub_filename)
db.session.add(new_libro)
db.session.commit()
return redirect(url_for('paginas.autor', autor_id=autor_id))
return render_template('add_libro2autor.html', autor=autor, autor_id=autor_id)
@bp.route('/edit_libro/<int:libro_id>', methods=['GET', 'POST'])
@login_required
def edit_libro(libro_id):
@ -200,7 +242,7 @@ def searchautor():
autores = Autores.query.filter(Autores.apellido.contains(query)).all()
else:
autores = []
return render_template('searchautor.html', query=query, autores=autores)
@bp.route('/searchlibro')


+ 48
- 0
LibrosPy/servicios/libros/templates/add_libro2autor.html View File

@ -0,0 +1,48 @@
{% extends 'base.html' %}
{% block content %}
<div class="column is-4 is-offset-4">
<h3>Añadir nuevo libro para autor '{{ autor.apellido }}, {{ autor.nombre}}'</h3>
<div class="box">
<form method="POST" enctype="multipart/form-data" action=" {{ url_for('paginas.add_libro2autor', autor_id=autor_id) }}">
<div class="text mb-3">
<label for="anno">Año:</label>
<textarea class="form-control" id="anno" name="anno" required></textarea>
</div>
<div class="text mb-3">
<label for="titulo">Título:</label>
<input class="form-control" type="text" id="titulo" name="titulo" required>
</div>
<div class="text mb-3">
<label for="portada">Portada</label>
<input class="form-control" type="file" id="portada" name="portada" required>
</div>
<div class="text mb-3">
<label for="epub">Fichero epub</label>
<input class="form-control" type="file" id="epub" name="epub" required>
</div>
<div class="text mb-3">
<button type="submit" class="btn app-btn-primary w-100 theme-btn mx-auto">Añadir canción</button>
</div>
</form>
</div>
<script>
function updateAuthor() {
var autorSelect = document.getElementById("autor_id");
var selectedautor = autorSelect.options[autorSelect.selectedIndex];
var artist = selectedautor.getAttribute("data-artist");
document.getElementById("author").value = artist;
}
// Initialize the author field with the artist of the first autor
document.addEventListener('DOMContentLoaded', function() {
updateAuthor();
});
</script>
</div>
{% endblock %}

+ 4
- 0
LibrosPy/servicios/libros/templates/autor.html View File

@ -60,6 +60,10 @@
<div class="col-auto">
<a class="btn app-btn-secondary" href="{{ url_for('paginas.index') }}">Volver al inicio</a>
</div>
<div class="col-auto">
<a class="btn app-btn-primary" href="{{ url_for('paginas.add_libro2autor', autor_id=autor.id) }}">Añadir libro</a>
</div>
</div>
</div>
{% endblock %}

+ 2
- 4
LibrosPy/servicios/libros/templates/index.html View File

@ -56,8 +56,7 @@
<thead>
<tr>
<th class="cell">#</th>
<th class="cell">Nombre</th>
<th class="cell">Apellido</th>
<th class="cell">Apellido y nombre</th>
<th class="cell">Foto</th>
</tr>
</thead>
@ -65,8 +64,7 @@
{% for row in autores %}
<tr>
<td class="cell">{{ row.id }}</td>
<td class="cell">{{ row.nombre }}</td>
<td class="cell">{{ row.apellido }}</td>
<td class="cell"><a href="{{ url_for('paginas.autor', autor_id=row.id) }}">{{ row.apellido }}, {{ row.nombre }}</a></td>
{% if row.foto %}
<td class="cell"><img src="{{ url_for('paginas.uploaded_file', filename=row.foto) }}" alt="{{ row.apellido }}, {{ row.nombre}}" style="width:200px;height:200px;"></td>
{% else %}


+ 2
- 3
LibrosPy/servicios/libros/templates/libroscard.html View File

@ -39,11 +39,10 @@
{% else %}
Sin imágen
{% endif %}
<h4 class="app-doc-title truncate mb-0"><a>{{ libro.titulo }}</a></h4>
<h4 class="app-doc-title truncate mb-0"><a href="{{ url_for('paginas.libro', libro_id=libro.id) }}">{{ libro.titulo }}</a></h4>
<div class="app-doc-meta">
<ul class="list-unstyled mb-0">
<li><span class="text-muted">Título: </span>{{ libro.titulo }}</li>
<li><span class="text-muted">Autor: </span>{{ libro.autores.apellidos }}, {{ libro.autores.nombre }}</li>
<li><span class="text-muted">Autor: </span><a href="{{ url_for('paginas.autor', autor_id=libro.autor_id) }}">{{ libro.autores.apellido }}, {{ libro.autores.nombre }}</a></li>
</ul>
</div><!--//app-doc-meta-->


Loading…
Cancel
Save