Walter Mantovani commited on
Commit ·
ef362ff
1
Parent(s): fbf131f
UPDATE: nuova versione
Browse files- app.py +172 -116
- models.py +5 -5
- static/scripts/load_lotti.js +1 -1
- static/scripts/load_prenotazioni.js +2 -2
- templates/lotto.html +72 -0
- templates/prenotazione.html +25 -49
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from flask import Flask, render_template, jsonify, request, redirect, url_for, session, flash
|
| 2 |
-
from models import User, Lotto, Prenotazione, db
|
| 3 |
from populate_db import init_db
|
| 4 |
from settings import DATABASE_PATH
|
| 5 |
|
|
@@ -14,16 +14,20 @@ app.config.update(
|
|
| 14 |
|
| 15 |
db.init_app(app)
|
| 16 |
|
|
|
|
| 17 |
@app.route('/')
|
| 18 |
def home():
|
| 19 |
return render_template('index.html')
|
| 20 |
|
|
|
|
| 21 |
@app.route('/prenotazioni')
|
| 22 |
def prenotazioni():
|
| 23 |
if 'user_id' not in session:
|
| 24 |
return redirect(url_for('login'))
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
| 27 |
@app.route('/api/dati_lotti')
|
| 28 |
def get_dati_lotti():
|
| 29 |
# Esegui la query per ottenere tutti i lotti in ordine di data
|
|
@@ -31,142 +35,195 @@ def get_dati_lotti():
|
|
| 31 |
lotti_data = []
|
| 32 |
for lotto in lotti: # Model objects
|
| 33 |
lotti_data.append(lotto.to_dict())
|
|
|
|
| 34 |
return jsonify(lotti_data)
|
| 35 |
|
| 36 |
|
|
|
|
| 37 |
@app.route('/api/dati_prenotazioni')
|
| 38 |
def get_dati_prenotazioni():
|
| 39 |
if 'user_id' not in session:
|
| 40 |
return redirect(url_for('login'))
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
prenotazioni_data = []
|
| 51 |
for prenot in prenotazioni:
|
| 52 |
-
# prenotazione_data = {
|
| 53 |
-
# 'prenotazione_id': prenot.id,
|
| 54 |
-
# 'qta': prenot.qta,
|
| 55 |
-
# 'prezzoTotale': prenot.get_prezzo_totale_str(),
|
| 56 |
-
# 'lotto': {
|
| 57 |
-
# 'lotto_id': prenot.lotto.id,
|
| 58 |
-
# 'dataConsegna': prenot.lotto.get_date(),
|
| 59 |
-
# 'qtaUnitaMisura': prenot.lotto.qta_unita_misura,
|
| 60 |
-
# 'qtaLotto': prenot.lotto.qta_lotto,
|
| 61 |
-
# 'qtaDisponibile': prenot.lotto.get_qta_disponibile(),
|
| 62 |
-
# 'prezzoUnitario': prenot.lotto.get_prezzo_str(),
|
| 63 |
-
# 'sospeso': prenot.lotto.sospeso,
|
| 64 |
-
# 'prodotto': {
|
| 65 |
-
# 'prodotto_id': prenot.lotto.prodotto.id,
|
| 66 |
-
# 'nome': prenot.lotto.prodotto.nome,
|
| 67 |
-
# 'produttore': {
|
| 68 |
-
# 'produttore_id': prenot.lotto.prodotto.produttore.id,
|
| 69 |
-
# 'nome': prenot.lotto.prodotto.produttore.nome,
|
| 70 |
-
# 'descrizione': prenot.lotto.prodotto.produttore.descrizione,
|
| 71 |
-
# 'indirizzo': prenot.lotto.prodotto.produttore.indirizzo,
|
| 72 |
-
# 'telefono': prenot.lotto.prodotto.produttore.telefono,
|
| 73 |
-
# 'email': prenot.lotto.prodotto.produttore.email,
|
| 74 |
-
# }
|
| 75 |
-
# }
|
| 76 |
-
# }
|
| 77 |
-
# }
|
| 78 |
-
# prenotazioni_data.append(prenotazione_data)
|
| 79 |
prenotazioni_data.append(prenot.to_dict())
|
| 80 |
|
| 81 |
return jsonify(prenotazioni_data)
|
| 82 |
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
if 'user_id' not in session:
|
| 87 |
return redirect(url_for('login'))
|
|
|
|
|
|
|
| 88 |
|
| 89 |
lotto = db.session.get(Lotto, lotto_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
azione = request.form.get('azione')
|
| 97 |
-
quantita = int(request.form.get('quantita'), 0)
|
| 98 |
-
qta_disponibile = lotto.get_qta_disponibile()
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
if azione == 'aggiornaPrenotazione':
|
| 106 |
-
if not prenotazione_esistente:
|
| 107 |
-
raise ValueError('Prenotazione non esistente.')
|
| 108 |
-
if quantita > qta_disponibile + prenotazione_esistente.qta:
|
| 109 |
-
flash('La quantità richiesta supera quella disponibile.', 'danger')
|
| 110 |
-
return render_template('prenotazione.html', azione=azione, lotto=lotto, quantita=quantita,
|
| 111 |
-
prenotazione_esistente=prenotazione_esistente)
|
| 112 |
-
|
| 113 |
-
prenotazione_esistente.qta = quantita
|
| 114 |
-
db.session.commit()
|
| 115 |
-
flash(f'Prenotazione di "{lotto.prodotto.nome}" aggiornata a {quantita} {lotto.qta_unita_misura}.', 'success')
|
| 116 |
-
|
| 117 |
-
elif azione == 'nuovaPrenotazione':
|
| 118 |
-
if quantita > qta_disponibile:
|
| 119 |
-
flash('La quantità richiesta supera quella disponibile.', 'danger')
|
| 120 |
-
return render_template('prenotazione.html', azione=azione, lotto=lotto, quantita=quantita,
|
| 121 |
-
prenotazione_esistente=prenotazione_esistente)
|
| 122 |
-
|
| 123 |
-
if prenotazione_esistente:
|
| 124 |
-
prenotazione_esistente.qta += quantita
|
| 125 |
-
db.session.commit()
|
| 126 |
-
flash(f'Aggiunti {quantita} {lotto.qta_unita_misura} alla prenotazione preesistente di "{lotto.prodotto.nome}".', 'success')
|
| 127 |
-
else:
|
| 128 |
-
prenotazione = Prenotazione(
|
| 129 |
-
user_id=session['user_id'],
|
| 130 |
-
lotto_id=lotto_id,
|
| 131 |
-
qta=quantita
|
| 132 |
-
)
|
| 133 |
-
db.session.add(prenotazione)
|
| 134 |
-
db.session.commit()
|
| 135 |
-
flash(f'Prenotazione di {quantita} {lotto.qta_unita_misura} di "{lotto.prodotto.nome}" effettuata con successo!', 'success')
|
| 136 |
-
|
| 137 |
-
elif azione == 'eliminaPrenotazione':
|
| 138 |
-
if not prenotazione_esistente:
|
| 139 |
-
raise ValueError('Prenotazione non esistente.')
|
| 140 |
-
db.session.delete(prenotazione_esistente)
|
| 141 |
-
db.session.commit()
|
| 142 |
-
flash('Prenotazione eliminata con successo.', 'warning')
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
return redirect(url_for('prenotazioni'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
@app.route('/login', methods=['GET', 'POST'])
|
|
@@ -191,9 +248,8 @@ def logout():
|
|
| 191 |
session.pop('user_id', None)
|
| 192 |
return redirect(url_for('login'))
|
| 193 |
|
| 194 |
-
with app.app_context():
|
| 195 |
-
init_db()
|
| 196 |
|
| 197 |
if __name__ == '__main__':
|
| 198 |
-
app.
|
| 199 |
-
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, jsonify, request, redirect, url_for, session, flash
|
| 2 |
+
from models import User, Lotto, Prenotazione, Prodotto, Produttore, db
|
| 3 |
from populate_db import init_db
|
| 4 |
from settings import DATABASE_PATH
|
| 5 |
|
|
|
|
| 14 |
|
| 15 |
db.init_app(app)
|
| 16 |
|
| 17 |
+
# Mostra la pagina che deve elencare i lotti disponibili
|
| 18 |
@app.route('/')
|
| 19 |
def home():
|
| 20 |
return render_template('index.html')
|
| 21 |
|
| 22 |
+
# Mostra la pagina che deve elencare le prenotazioni dell'utente
|
| 23 |
@app.route('/prenotazioni')
|
| 24 |
def prenotazioni():
|
| 25 |
if 'user_id' not in session:
|
| 26 |
return redirect(url_for('login'))
|
| 27 |
+
else:
|
| 28 |
+
return render_template('prenotazioni.html')
|
| 29 |
|
| 30 |
+
# Restituisce i dati dei lotti
|
| 31 |
@app.route('/api/dati_lotti')
|
| 32 |
def get_dati_lotti():
|
| 33 |
# Esegui la query per ottenere tutti i lotti in ordine di data
|
|
|
|
| 35 |
lotti_data = []
|
| 36 |
for lotto in lotti: # Model objects
|
| 37 |
lotti_data.append(lotto.to_dict())
|
| 38 |
+
|
| 39 |
return jsonify(lotti_data)
|
| 40 |
|
| 41 |
|
| 42 |
+
# Restituisce i dati delle prenotazioni degli utenti
|
| 43 |
@app.route('/api/dati_prenotazioni')
|
| 44 |
def get_dati_prenotazioni():
|
| 45 |
if 'user_id' not in session:
|
| 46 |
return redirect(url_for('login'))
|
| 47 |
+
else:
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
# Così è semplice, ma non sono ordinati per data del lotto:
|
| 51 |
+
# prenot_user = Prenotazione.query.filter_by(user_id=session['user_id']).all()
|
| 52 |
+
|
| 53 |
+
# Se facciamo una query semplice e diretta, non possiamo ordinare per
|
| 54 |
+
# data_consegna del lotto, perché non abbiamo il campo 'data_consegna'
|
| 55 |
+
# del lotto!
|
| 56 |
+
# prenot_user_ord = Prenotazione.query \
|
| 57 |
+
# .filter_by(user_id=session['user_id']) \
|
| 58 |
+
# .order_by('???') \
|
| 59 |
+
# .all()
|
| 60 |
+
|
| 61 |
+
# Dobbiamo quindi fare un join con la tabella Lotto per poter
|
| 62 |
+
# ordinare per data_consegna:
|
| 63 |
+
prenot_user_ord = (Prenotazione.query
|
| 64 |
+
.filter_by(user_id=session['user_id'])
|
| 65 |
+
.join(Lotto, Prenotazione.lotto_id == Lotto.id)
|
| 66 |
+
.order_by(Lotto.data_consegna)
|
| 67 |
+
.all()
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Se preferite, potete dividere la query in due passaggi:
|
| 71 |
+
# prenotazioni_user = Prenotazione.query.filter_by(user_id=session['user_id'])
|
| 72 |
+
# prenot_user_ord = prenotazioni_user.join(Lotto).order_by(Lotto.data_consegna).all()
|
| 73 |
+
|
| 74 |
+
# Oppure, possiamo evitare applicare il filtraro, sfruttando la relationship
|
| 75 |
+
# tra Utente e Prenotazione a cui però va impostato l'argomento lazy='dynamic':
|
| 76 |
+
# utente = db.session.get(User, session['user_id'])
|
| 77 |
+
# prenot_user_ord = utente.prenotazioni.join(Lotto).order_by(Lotto.data_consegna).all()
|
| 78 |
+
|
| 79 |
+
print(prenot_user_ord)
|
| 80 |
|
| 81 |
prenotazioni_data = []
|
| 82 |
for prenot in prenotazioni:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
prenotazioni_data.append(prenot.to_dict())
|
| 84 |
|
| 85 |
return jsonify(prenotazioni_data)
|
| 86 |
|
| 87 |
|
| 88 |
+
# Mostra il lotto
|
| 89 |
+
@app.route('/lotto/<int:lotto_id>', methods=['GET'])
|
| 90 |
+
def mostra_lotto(lotto_id):
|
| 91 |
if 'user_id' not in session:
|
| 92 |
return redirect(url_for('login'))
|
| 93 |
+
else:
|
| 94 |
+
pass
|
| 95 |
|
| 96 |
lotto = db.session.get(Lotto, lotto_id)
|
| 97 |
+
prenotazione_esistente = Prenotazione.query.filter_by(lotto_id=lotto_id, user_id=session['user_id']).first()
|
| 98 |
+
if prenotazione_esistente:
|
| 99 |
+
flash('Hai già una prenotazione per questo lotto. Se vuoi, puoi modificare '
|
| 100 |
+
'la prenotazione esistente.', 'primary'
|
| 101 |
+
)
|
| 102 |
+
return redirect(url_for('mostra_prenotazione', prenotazione_id=prenotazione_esistente.id))
|
| 103 |
+
else:
|
| 104 |
+
return render_template('lotto.html', lotto=lotto)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# Crea una nuova prenotazione a partire da un lotto
|
| 108 |
+
@app.route('/lotto/<int:lotto_id>', methods=['POST'])
|
| 109 |
+
def prenota_lotto(lotto_id):
|
| 110 |
+
if 'user_id' not in session:
|
| 111 |
+
flash('Devi fare il login per effettuare una prenotazione.', 'warning')
|
| 112 |
+
return redirect(url_for('login'))
|
| 113 |
+
else:
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
# Controllo di sicurezza per evitare di duplicare prenotazioni il che solleverebbe
|
| 117 |
+
# un errore dato che abbiamo impostato un constraint univoco su (lotto_id, user_id)
|
| 118 |
+
# a livello di DBMS.
|
| 119 |
+
prenotazione_esistente = Prenotazione.query.filter_by(lotto_id=lotto_id, user_id=session['user_id']).first()
|
| 120 |
+
if prenotazione_esistente:
|
| 121 |
+
flash('Hai già una prenotazione per questo lotto. Se vuoi, puoi modificare '
|
| 122 |
+
'la prenotazione esistente.', 'primary'
|
| 123 |
+
)
|
| 124 |
+
return redirect(url_for('mostra_prenotazione', prenotazione_id=prenotazione_esistente.id))
|
| 125 |
+
else:
|
| 126 |
+
pass
|
| 127 |
+
|
| 128 |
+
quantita = int(request.form.get('quantita')) # ATTENZIONE: sarebbe da gestire meglio
|
| 129 |
+
# con un try/except per evitare errori
|
| 130 |
+
lotto = db.session.get(Lotto, lotto_id)
|
| 131 |
|
| 132 |
+
if quantita <= 0:
|
| 133 |
+
flash('Quantità non valida. Inserire un numero maggiore di 0.', 'danger')
|
| 134 |
+
return redirect(url_for('mostra_lotto', lotto_id=lotto_id))
|
| 135 |
+
else:
|
| 136 |
+
pass
|
| 137 |
|
| 138 |
+
qta_disponibile = lotto.get_qta_disponibile()
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
if quantita > qta_disponibile:
|
| 141 |
+
flash('La quantità richiesta supera quella disponibile.', 'danger')
|
| 142 |
+
return redirect(url_for('mostra_lotto', lotto_id=lotto_id))
|
| 143 |
+
else:
|
| 144 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
prenotazione = Prenotazione(user_id=session['user_id'], lotto_id=lotto_id, qta=quantita)
|
| 147 |
+
db.session.add(prenotazione)
|
| 148 |
+
db.session.commit()
|
| 149 |
+
flash(f'Prenotazione di {quantita} {lotto.qta_unita_misura} di '
|
| 150 |
+
f'"{lotto.prodotto.nome}" effettuata con successo!', 'success')
|
| 151 |
+
|
| 152 |
+
return redirect(url_for('prenotazioni', lotto_id=lotto_id))
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# Mostra prenotazione esistente
|
| 156 |
+
@app.route('/prenotazione/<int:prenotazione_id>', methods=['GET'])
|
| 157 |
+
def mostra_prenotazione(prenotazione_id):
|
| 158 |
+
if 'user_id' not in session:
|
| 159 |
+
return redirect(url_for('login'))
|
| 160 |
+
else:
|
| 161 |
+
pass
|
| 162 |
+
|
| 163 |
+
prenotazione = db.session.get(Prenotazione, prenotazione_id)
|
| 164 |
+
|
| 165 |
+
if not prenotazione:
|
| 166 |
+
flash('Prenotazione non trovata.', 'danger')
|
| 167 |
return redirect(url_for('prenotazioni'))
|
| 168 |
+
else:
|
| 169 |
+
pass
|
| 170 |
+
|
| 171 |
+
if prenotazione.user_id != session['user_id']:
|
| 172 |
+
flash('Non sei autorizzato a visualizzare questa prenotazione.', 'danger')
|
| 173 |
+
return redirect(url_for('prenotazioni'))
|
| 174 |
+
|
| 175 |
+
return render_template('prenotazione.html', prenotazione=prenotazione)
|
| 176 |
+
|
| 177 |
+
# Modifica prenotazione esistente
|
| 178 |
+
@app.route('/prenotazione/<int:prenotazione_id>', methods=['POST'])
|
| 179 |
+
def modifica_prenotazione(prenotazione_id):
|
| 180 |
+
if 'user_id' not in session:
|
| 181 |
+
return redirect(url_for('login'))
|
| 182 |
|
| 183 |
+
prenotazione = db.session.get(Prenotazione, prenotazione_id)
|
| 184 |
+
|
| 185 |
+
if not prenotazione:
|
| 186 |
+
flash('Prenotazione non trovata.', 'danger')
|
| 187 |
+
return redirect(url_for('prenotazioni'))
|
| 188 |
+
else:
|
| 189 |
+
pass
|
| 190 |
+
|
| 191 |
+
if prenotazione.user_id != session['user_id']:
|
| 192 |
+
flash('Non sei autorizzato a modificare questa prenotazione.', 'danger')
|
| 193 |
+
return redirect(url_for('prenotazioni'))
|
| 194 |
+
else:
|
| 195 |
+
pass
|
| 196 |
+
|
| 197 |
+
azione = request.form.get('azione')
|
| 198 |
+
|
| 199 |
+
if azione == 'aggiorna':
|
| 200 |
+
quantita = int(request.form.get('quantita')) # ATTENZIONE: sarebbe da gestire meglio
|
| 201 |
+
# con un try/except per evitare errori
|
| 202 |
+
lotto = prenotazione.lotto
|
| 203 |
|
| 204 |
+
if quantita <= 0:
|
| 205 |
+
flash('Quantità non valida. Inserire un numero maggiore di 0.', 'danger')
|
| 206 |
+
return redirect(url_for('mostra_prenotazione', prenotazione_id=prenotazione_id))
|
| 207 |
+
|
| 208 |
+
qta_disponibile = lotto.get_qta_disponibile()
|
| 209 |
+
|
| 210 |
+
if quantita > qta_disponibile + prenotazione.qta:
|
| 211 |
+
flash('La quantità richiesta supera quella disponibile.', 'danger')
|
| 212 |
+
return redirect(url_for('mostra_prenotazione', prenotazione_id=prenotazione_id))
|
| 213 |
+
|
| 214 |
+
prenotazione.qta = quantita
|
| 215 |
+
db.session.commit()
|
| 216 |
+
flash(f'Prenotazione di "{lotto.prodotto.nome}" aggiornata a {quantita} {lotto.qta_unita_misura}.', 'success')
|
| 217 |
+
|
| 218 |
+
elif azione == 'elimina':
|
| 219 |
+
db.session.delete(prenotazione)
|
| 220 |
+
db.session.commit()
|
| 221 |
+
flash('Prenotazione eliminata con successo.', 'warning')
|
| 222 |
+
|
| 223 |
+
else:
|
| 224 |
+
flash('Azione non implementata.', 'danger')
|
| 225 |
+
|
| 226 |
+
return redirect(url_for('prenotazioni'))
|
| 227 |
|
| 228 |
|
| 229 |
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
| 248 |
session.pop('user_id', None)
|
| 249 |
return redirect(url_for('login'))
|
| 250 |
|
|
|
|
|
|
|
| 251 |
|
| 252 |
if __name__ == '__main__':
|
| 253 |
+
with app.app_context():
|
| 254 |
+
init_db()
|
| 255 |
+
app.run(debug=True)
|
models.py
CHANGED
|
@@ -2,7 +2,7 @@ import locale
|
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from sqlalchemy_serializer import SerializerMixin
|
| 4 |
|
| 5 |
-
locale.setlocale(locale.LC_TIME, 'it_IT
|
| 6 |
|
| 7 |
db = SQLAlchemy()
|
| 8 |
|
|
@@ -17,7 +17,7 @@ class User(db.Model, SerializerMixin):
|
|
| 17 |
# -- RELATIONSHIPS --
|
| 18 |
prenotazioni = db.relationship('Prenotazione', back_populates='user', lazy='dynamic')
|
| 19 |
|
| 20 |
-
serialize_rules = ('-prenotazioni.user',)
|
| 21 |
|
| 22 |
|
| 23 |
class Produttore(db.Model, SerializerMixin):
|
|
@@ -31,7 +31,7 @@ class Produttore(db.Model, SerializerMixin):
|
|
| 31 |
# -- RELATIONSHIPS --
|
| 32 |
prodotti = db.relationship('Prodotto', back_populates='produttore')
|
| 33 |
|
| 34 |
-
serialize_rules = ('-prodotti.produttore',)
|
| 35 |
|
| 36 |
|
| 37 |
class Prodotto(db.Model, SerializerMixin):
|
|
@@ -43,7 +43,7 @@ class Prodotto(db.Model, SerializerMixin):
|
|
| 43 |
produttore = db.relationship('Produttore', back_populates='prodotti')
|
| 44 |
lotti = db.relationship('Lotto', back_populates='prodotto')
|
| 45 |
|
| 46 |
-
serialize_rules = ('-produttore.prodotti', '-lotti.prodotto')
|
| 47 |
|
| 48 |
|
| 49 |
class Lotto(db.Model, SerializerMixin):
|
|
@@ -59,7 +59,7 @@ class Lotto(db.Model, SerializerMixin):
|
|
| 59 |
prodotto = db.relationship('Prodotto', back_populates='lotti')
|
| 60 |
prenotazioni = db.relationship('Prenotazione', back_populates='lotto')
|
| 61 |
|
| 62 |
-
serialize_rules = ('-prodotto.lotti', '-prenotazioni
|
| 63 |
|
| 64 |
def get_qta_disponibile(self):
|
| 65 |
qta_prenotata = sum(prenotazione.qta for prenotazione in self.prenotazioni)
|
|
|
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from sqlalchemy_serializer import SerializerMixin
|
| 4 |
|
| 5 |
+
locale.setlocale(locale.LC_TIME, 'it_IT')
|
| 6 |
|
| 7 |
db = SQLAlchemy()
|
| 8 |
|
|
|
|
| 17 |
# -- RELATIONSHIPS --
|
| 18 |
prenotazioni = db.relationship('Prenotazione', back_populates='user', lazy='dynamic')
|
| 19 |
|
| 20 |
+
serialize_rules = ('-prenotazioni.user', '-password')
|
| 21 |
|
| 22 |
|
| 23 |
class Produttore(db.Model, SerializerMixin):
|
|
|
|
| 31 |
# -- RELATIONSHIPS --
|
| 32 |
prodotti = db.relationship('Prodotto', back_populates='produttore')
|
| 33 |
|
| 34 |
+
serialize_rules = ('-prodotti.produttore', '-prodotti.lotti.prenotazioni')
|
| 35 |
|
| 36 |
|
| 37 |
class Prodotto(db.Model, SerializerMixin):
|
|
|
|
| 43 |
produttore = db.relationship('Produttore', back_populates='prodotti')
|
| 44 |
lotti = db.relationship('Lotto', back_populates='prodotto')
|
| 45 |
|
| 46 |
+
serialize_rules = ('-produttore.prodotti', '-lotti.prodotto', '-lotti.prenotazioni')
|
| 47 |
|
| 48 |
|
| 49 |
class Lotto(db.Model, SerializerMixin):
|
|
|
|
| 59 |
prodotto = db.relationship('Prodotto', back_populates='lotti')
|
| 60 |
prenotazioni = db.relationship('Prenotazione', back_populates='lotto')
|
| 61 |
|
| 62 |
+
serialize_rules = ('-prodotto.lotti', '-prenotazioni', 'get_qta_disponibile', 'get_date', 'get_prezzo_str')
|
| 63 |
|
| 64 |
def get_qta_disponibile(self):
|
| 65 |
qta_prenotata = sum(prenotazione.qta for prenotazione in self.prenotazioni)
|
static/scripts/load_lotti.js
CHANGED
|
@@ -21,7 +21,7 @@ function onLoad() {
|
|
| 21 |
} else if (lotto.get_qta_disponibile == 0) {
|
| 22 |
renderPrenota = '<a href="#" class="btn btn-danger disabled w-100">Esaurito</a>';
|
| 23 |
} else {
|
| 24 |
-
renderPrenota = `<a href="/
|
| 25 |
}
|
| 26 |
|
| 27 |
rowLotti.innerHTML += `
|
|
|
|
| 21 |
} else if (lotto.get_qta_disponibile == 0) {
|
| 22 |
renderPrenota = '<a href="#" class="btn btn-danger disabled w-100">Esaurito</a>';
|
| 23 |
} else {
|
| 24 |
+
renderPrenota = `<a href="/lotto/${lotto.id}" class="btn btn-primary w-100">Prenota</a>`;
|
| 25 |
}
|
| 26 |
|
| 27 |
rowLotti.innerHTML += `
|
static/scripts/load_prenotazioni.js
CHANGED
|
@@ -21,7 +21,7 @@ function onLoad() {
|
|
| 21 |
|
| 22 |
let renderModifica = '';
|
| 23 |
if(!prenot.lotto.sospeso) {
|
| 24 |
-
renderModifica = `<a href="/prenotazione/${prenot.
|
| 25 |
} else {
|
| 26 |
renderModifica = '<a href="#" class="btn btn-danger disabled">Annullato</a>';
|
| 27 |
}
|
|
@@ -33,7 +33,7 @@ function onLoad() {
|
|
| 33 |
<h4 class="card-title">
|
| 34 |
${prenot.lotto.prodotto.nome}
|
| 35 |
</h4>
|
| 36 |
-
<div class="text-end"><small>(cod. lotto ${prenot.lotto.id})</small></div>
|
| 37 |
</div>
|
| 38 |
<div class="card-body d-flex flex-column">
|
| 39 |
<p class="card-text">
|
|
|
|
| 21 |
|
| 22 |
let renderModifica = '';
|
| 23 |
if(!prenot.lotto.sospeso) {
|
| 24 |
+
renderModifica = `<a href="/prenotazione/${prenot.id}" class="btn btn-primary w-100">Modifica prenotazione</a>`;
|
| 25 |
} else {
|
| 26 |
renderModifica = '<a href="#" class="btn btn-danger disabled">Annullato</a>';
|
| 27 |
}
|
|
|
|
| 33 |
<h4 class="card-title">
|
| 34 |
${prenot.lotto.prodotto.nome}
|
| 35 |
</h4>
|
| 36 |
+
<div class="text-end"><small>(cod. lotto ${prenot.lotto.id} / prenot. ${prenot.id})</small></div>
|
| 37 |
</div>
|
| 38 |
<div class="card-body d-flex flex-column">
|
| 39 |
<p class="card-text">
|
templates/lotto.html
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% extends "_layout_base.html" %}
|
| 2 |
+
|
| 3 |
+
{% block title %}Prenotazione lotto {{ super() }}{% endblock %}
|
| 4 |
+
|
| 5 |
+
{% block h1 %}
|
| 6 |
+
{% if prenotazione_esistente %}
|
| 7 |
+
Aggiungi alla tua prenotazione
|
| 8 |
+
{% else %}
|
| 9 |
+
Nuova prenotazione
|
| 10 |
+
{% endif %}
|
| 11 |
+
{% endblock %}
|
| 12 |
+
|
| 13 |
+
{% block content %}
|
| 14 |
+
<div class="row mb-4 justify-content-center">
|
| 15 |
+
<!-- lotto infos -->
|
| 16 |
+
<div class="col-md-6">
|
| 17 |
+
<div class="card">
|
| 18 |
+
<div class="card-header">
|
| 19 |
+
<h4 class="card-title">
|
| 20 |
+
{{ lotto.prodotto.nome }}
|
| 21 |
+
</h4>
|
| 22 |
+
<div class="text-end"><small>(cod. lotto {{ lotto.id }})</small></div>
|
| 23 |
+
</div>
|
| 24 |
+
<div class="card-body">
|
| 25 |
+
<p class="card-text">
|
| 26 |
+
<small>Disponibile da:</small> <b>{{ lotto.get_date() }}</b>
|
| 27 |
+
</p>
|
| 28 |
+
<p class="card-text">
|
| 29 |
+
<small>Prodotto da:</small> <b>{{ lotto.prodotto.produttore.nome }}</b>
|
| 30 |
+
</p>
|
| 31 |
+
<p class="card-text">
|
| 32 |
+
<small>Q.tà totale lotto:</small> <b>{{ lotto.qta_lotto }} {{ lotto.qta_unita_misura }}</b>
|
| 33 |
+
</p>
|
| 34 |
+
<p class="card-text">
|
| 35 |
+
<small>Q.tà disponibile:</small> <b>{{ lotto.get_qta_disponibile() }} {{ lotto.qta_unita_misura }}</b>
|
| 36 |
+
</p>
|
| 37 |
+
<p class="card-text">
|
| 38 |
+
<small>Prezzo:</small> <b>{{ lotto.get_prezzo_str() }}</b>
|
| 39 |
+
</p>
|
| 40 |
+
{% if prenotazione_esistente %}
|
| 41 |
+
<div class="row alert alert-primary text-center">
|
| 42 |
+
<div class="col card-text">
|
| 43 |
+
<small>Q.tà già prenotata:</small> <b>{{ prenotazione_esistente.qta }} {{ lotto.qta_unita_misura }}</b>
|
| 44 |
+
</div>
|
| 45 |
+
<div class="col card-text">
|
| 46 |
+
<small>Prezzo totale:</small> <b>{{ prenotazione_esistente.get_prezzo_totale_str() }}</b>
|
| 47 |
+
</div>
|
| 48 |
+
</div>
|
| 49 |
+
{% endif %}
|
| 50 |
+
</div>
|
| 51 |
+
<!-- Form per la creazione o la modifica di una prenotazione -->
|
| 52 |
+
<div class="card-footer">
|
| 53 |
+
<form method="POST" action="{{ url_for('prenota_lotto', lotto_id=lotto.id) }}">
|
| 54 |
+
<div class="form-group mb-3">
|
| 55 |
+
<label for="quantita" class="form-label">Quantità</label>
|
| 56 |
+
<input type="number" class="form-control" id="quantita" name="quantita"
|
| 57 |
+
required min="1" max="{{ lotto.get_qta_disponibile() }}"
|
| 58 |
+
value="1">
|
| 59 |
+
</div>
|
| 60 |
+
<button type="submit" class="btn btn-primary w-100" name="azione" value="nuovaPrenotazione">
|
| 61 |
+
{% if prenotazione_esistente %} Aggiungi quantità {% else %} Prenota quantità {% endif %}
|
| 62 |
+
</button>
|
| 63 |
+
</form>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
</div>
|
| 67 |
+
</div>
|
| 68 |
+
{% endblock %}
|
| 69 |
+
|
| 70 |
+
{% block scripts %}
|
| 71 |
+
<script src="{{ url_for('static', filename='scripts/load_prenotazione.js') }}"></script>
|
| 72 |
+
{% endblock %}
|
templates/prenotazione.html
CHANGED
|
@@ -2,18 +2,7 @@
|
|
| 2 |
|
| 3 |
{% block title %}Prenotazione {{ super() }}{% endblock %}
|
| 4 |
|
| 5 |
-
{% block h1 %}
|
| 6 |
-
{% if prenotazione_esistente %}
|
| 7 |
-
{% if azione == 'nuovaPrenotazione' %}
|
| 8 |
-
Aggiungi alla tua prenotazione
|
| 9 |
-
{% elif azione == 'aggiornaPrenotazione' %}
|
| 10 |
-
Aggiorna la tua prenotazione
|
| 11 |
-
{% endif %}
|
| 12 |
-
{% else %}
|
| 13 |
-
Nuova prenotazione
|
| 14 |
-
{% endif %}
|
| 15 |
-
|
| 16 |
-
{% endblock %}
|
| 17 |
|
| 18 |
{% block content %}
|
| 19 |
<div class="row mb-4 justify-content-center">
|
|
@@ -22,64 +11,51 @@
|
|
| 22 |
<div class="card">
|
| 23 |
<div class="card-header">
|
| 24 |
<h4 class="card-title">
|
| 25 |
-
{{ lotto.prodotto.nome }}
|
| 26 |
</h4>
|
| 27 |
-
<div class="text-end"><small>(cod. lotto {{ lotto.id }})</small></div>
|
| 28 |
</div>
|
| 29 |
<div class="card-body">
|
| 30 |
<p class="card-text">
|
| 31 |
-
<small>Disponibile da:</small> <b>{{ lotto.get_date() }}</b>
|
| 32 |
</p>
|
| 33 |
<p class="card-text">
|
| 34 |
-
<small>Prodotto da:</small> <b>{{ lotto.prodotto.produttore.nome }}</b>
|
| 35 |
</p>
|
| 36 |
<p class="card-text">
|
| 37 |
-
<small>Q.tà totale lotto:</small> <b>{{ lotto.qta_lotto }} {{ lotto.qta_unita_misura }}</b>
|
| 38 |
</p>
|
| 39 |
<p class="card-text">
|
| 40 |
-
<small>Q.tà disponibile:</small> <b>{{ lotto.get_qta_disponibile() }} {{ lotto.qta_unita_misura }}</b>
|
| 41 |
</p>
|
| 42 |
<p class="card-text">
|
| 43 |
-
<small>Prezzo:</small> <b>{{ lotto.get_prezzo_str() }}</b>
|
| 44 |
</p>
|
| 45 |
-
{% if prenotazione_esistente %}
|
| 46 |
<div class="row alert alert-primary text-center">
|
| 47 |
<div class="col card-text">
|
| 48 |
-
<small>Q.tà già prenotata:</small> <b>{{
|
| 49 |
</div>
|
| 50 |
<div class="col card-text">
|
| 51 |
-
<small>Prezzo totale:</small> <b>{{
|
| 52 |
</div>
|
| 53 |
</div>
|
| 54 |
-
{% endif %}
|
| 55 |
</div>
|
| 56 |
-
<!-- Form per la
|
| 57 |
<div class="card-footer">
|
| 58 |
-
<form method="POST">
|
| 59 |
-
<
|
| 60 |
-
|
| 61 |
-
<
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
<label for="quantita" class="form-label">Quantità</label>
|
| 73 |
-
<input type="number" class="form-control" id="quantita" name="quantita"
|
| 74 |
-
required min="0" max="{{ lotto.get_qta_disponibile() + prenotazione_esistente.qta}}"
|
| 75 |
-
value="{{ quantita }}">
|
| 76 |
-
</div>
|
| 77 |
-
<button type="submit" class="btn btn-primary" name="azione" value="aggiornaPrenotazione">Modifica quantità</button>
|
| 78 |
-
<button type="submit" class="btn btn-danger float-end" name="azione" value="eliminaPrenotazione"
|
| 79 |
-
onclick="return confirm('Sei sicuro di voler eliminare la prenotazione?')">
|
| 80 |
-
Elimina prenotazione
|
| 81 |
-
</button>
|
| 82 |
-
{% endif %}
|
| 83 |
</form>
|
| 84 |
</div>
|
| 85 |
</div>
|
|
|
|
| 2 |
|
| 3 |
{% block title %}Prenotazione {{ super() }}{% endblock %}
|
| 4 |
|
| 5 |
+
{% block h1 %}Modifica la tua prenotazione{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
{% block content %}
|
| 8 |
<div class="row mb-4 justify-content-center">
|
|
|
|
| 11 |
<div class="card">
|
| 12 |
<div class="card-header">
|
| 13 |
<h4 class="card-title">
|
| 14 |
+
{{ prenotazione.lotto.prodotto.nome }}
|
| 15 |
</h4>
|
| 16 |
+
<div class="text-end"><small>(cod. lotto {{ prenotazione.lotto.id }} / prenot. {{ prenotazione.id }})</small></div>
|
| 17 |
</div>
|
| 18 |
<div class="card-body">
|
| 19 |
<p class="card-text">
|
| 20 |
+
<small>Disponibile da:</small> <b>{{ prenotazione.lotto.get_date() }}</b>
|
| 21 |
</p>
|
| 22 |
<p class="card-text">
|
| 23 |
+
<small>Prodotto da:</small> <b>{{ prenotazione.lotto.prodotto.produttore.nome }}</b>
|
| 24 |
</p>
|
| 25 |
<p class="card-text">
|
| 26 |
+
<small>Q.tà totale lotto:</small> <b>{{ prenotazione.lotto.qta_lotto }} {{ prenotazione.lotto.qta_unita_misura }}</b>
|
| 27 |
</p>
|
| 28 |
<p class="card-text">
|
| 29 |
+
<small>Q.tà disponibile:</small> <b>{{ prenotazione.lotto.get_qta_disponibile() }} {{ prenotazione.lotto.qta_unita_misura }}</b>
|
| 30 |
</p>
|
| 31 |
<p class="card-text">
|
| 32 |
+
<small>Prezzo:</small> <b>{{ prenotazione.lotto.get_prezzo_str() }}</b>
|
| 33 |
</p>
|
|
|
|
| 34 |
<div class="row alert alert-primary text-center">
|
| 35 |
<div class="col card-text">
|
| 36 |
+
<small>Q.tà già prenotata:</small> <b>{{ prenotazione.qta }} {{ prenotazione.lotto.qta_unita_misura }}</b>
|
| 37 |
</div>
|
| 38 |
<div class="col card-text">
|
| 39 |
+
<small>Prezzo totale:</small> <b>{{ prenotazione.get_prezzo_totale_str() }}</b>
|
| 40 |
</div>
|
| 41 |
</div>
|
|
|
|
| 42 |
</div>
|
| 43 |
+
<!-- Form per la modifica di una prenotazione -->
|
| 44 |
<div class="card-footer">
|
| 45 |
+
<form method="POST" action="{{ url_for('modifica_prenotazione', prenotazione_id=prenotazione.id) }}">
|
| 46 |
+
<div class="form-group mb-3">
|
| 47 |
+
<label for="quantita" class="form-label">Quantità</label>
|
| 48 |
+
<input type="number" class="form-control" id="quantita" name="quantita"
|
| 49 |
+
required min="0" max="{{ prenotazione.lotto.get_qta_disponibile() + prenotazione.qta}}"
|
| 50 |
+
value="{{ prenotazione.qta }}">
|
| 51 |
+
</div>
|
| 52 |
+
<button type="submit" class="btn btn-primary" name="azione" value="aggiorna">
|
| 53 |
+
Modifica quantità
|
| 54 |
+
</button>
|
| 55 |
+
<button type="submit" class="btn btn-danger float-end" name="azione" value="elimina"
|
| 56 |
+
onclick="return confirm('Sei sicuro di voler eliminare la prenotazione?')">
|
| 57 |
+
Elimina prenotazione
|
| 58 |
+
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
</form>
|
| 60 |
</div>
|
| 61 |
</div>
|