Skip to content

Commit b2e47b0

Browse files
committed
Creato Blueprint tags
1 parent cd63d8e commit b2e47b0

File tree

18 files changed

+112
-103
lines changed

18 files changed

+112
-103
lines changed

Flask/Flask04/burlesco70/webapp/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Aggiunto "required" nelle form html ove necessario
2626
- Pagina Dettaglio Corso: aggiunta in lista visualizzazione serata con registrazione non presente
2727
- Pagina Dettaglio Corso: possibilità di aggiungere Serata al corso
28+
- Creato Blueprint "tags" con relativi spostamenti di pagine e metodi
2829

2930
# TO DO App
3031
- Pagina dettaglio corso: aggiunta / cancellazione serata

Flask/Flask04/burlesco70/webapp/project/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
# get errors in your models.py files.
3333
## Grab the blueprints from the other views.py files for each "app"
3434
from project.corsi.views import corsi_blueprint
35+
from project.tags.views import tags_blueprint
3536
from project.error_pages.handlers import error_pages
3637

3738
app.register_blueprint(corsi_blueprint, url_prefix="/corsi")
39+
app.register_blueprint(tags_blueprint, url_prefix="/tags")
40+
# app.register_blueprint(serate_blueprint, url_prefix="/serate")
3841
app.register_blueprint(error_pages)
3942

40-
# app.register_blueprint(serate_blueprint, url_prefix="/serate")
43+
4144

Binary file not shown.
Binary file not shown.
Binary file not shown.

Flask/Flask04/burlesco70/webapp/project/corsi/forms.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from time import strftime
22
from flask_wtf import FlaskForm
33
from project.models.corsi import Corso, Serata
4-
from project import db
54
from wtforms import (
65
Form,
76
validators,
@@ -78,33 +77,6 @@ def write_to_disk(name, surname, email):
7877
data.close()
7978

8079

81-
def write_db(name, teacher, level, description):
82-
83-
print(f"Writing new data to db: {name}")
84-
85-
new_course = Corso(name, teacher, level, description)
86-
87-
# try:
88-
db.session.add(new_course)
89-
db.session.commit()
90-
# get the id of the object
91-
# db.session.refresh(new_course)
92-
course_inserted = Corso.query.filter_by(nome=name).first()
93-
print(f"Course: {name} added to db, with id: {course_inserted.id}")
94-
95-
return course_inserted.id
96-
97-
# except Exception as message:
98-
# print(f"Impossibile to write on db the new course: {name}, because: {message}")
99-
# db.session.rollback()
100-
# return 0
101-
102-
'''
103-
Tag Form
104-
'''
105-
class TagForm(FlaskForm):
106-
name = StringField(u'Titolo tag', validators=[Length(min=-1, max=255, message='Massimo 255 caratteri')])
107-
10880
'''
10981
Serata Form
11082
'''

Flask/Flask04/burlesco70/webapp/project/corsi/views.py

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
session,
99
current_app,
1010
)
11-
from project.corsi.forms import CorsiForm, write_to_disk, write_db, TagForm, SerataForm
11+
from project.corsi.forms import CorsiForm, write_to_disk, SerataForm
1212
from project.models.corsi import Corso, Tag, Serata
1313
from project import db
1414

1515
from sqlalchemy import desc,asc
1616

1717
# Define blueprint
18-
corsi_blueprint = Blueprint("corsi", __name__, template_folder="templates/corsi")
18+
corsi_blueprint = Blueprint("corsi", __name__, template_folder="templates")
1919

2020
'''
2121
Lista dei corsi
@@ -118,71 +118,3 @@ def corso_delete(id):
118118
flash("Errore durante la cancellazione del corso: %s" % str(e), 'danger')
119119
return redirect(url_for('corsi.lista'))
120120

121-
'''
122-
Lista dei tags con la possibilità di creazione di nuovo tag
123-
'''
124-
@corsi_blueprint.route("/tags", methods=('GET', 'POST'))
125-
def tags():
126-
# Ordinamento alfabetico ascendente per titolo
127-
lista_tags = Tag.query.order_by(asc(Tag.name)).all()
128-
'''
129-
Crea nuovo tag
130-
'''
131-
form = TagForm()
132-
if form.validate_on_submit():
133-
tag_name = form.name.data
134-
n_tag = Tag(tag_name)
135-
db.session.add(n_tag)
136-
form.name.data = ""
137-
try:
138-
db.session.commit()
139-
flash('Tag creato correttamente', 'success')
140-
return redirect(url_for('corsi.tags'))
141-
except Exception as e:
142-
db.session.rollback()
143-
flash("Errore durante la creazione del tag: %s" % str(e), 'danger')
144-
145-
return render_template('tags_lista.html', form=form, lista_tags=lista_tags)
146-
147-
'''
148-
Cancellazione tag
149-
'''
150-
@corsi_blueprint.route("/tags/delete/<int:id>", methods=('GET', 'POST'))
151-
def tag_delete(id):
152-
'''
153-
Delete tag
154-
'''
155-
try:
156-
my_tag = Tag.query.filter_by(id=id).first()
157-
db.session.delete(my_tag)
158-
db.session.commit()
159-
flash('Cancellazione avvenuta con successo.', 'success')
160-
except Exception as e:
161-
db.session.rollback()
162-
flash("Errore durante la cancellazione del tag: %s" % str(e), 'danger')
163-
return redirect(url_for('corsi.tags'))
164-
165-
'''
166-
Modifica tag
167-
'''
168-
@corsi_blueprint.route("/tags/<id>", methods=('GET', 'POST'))
169-
def edit_tag(id):
170-
'''
171-
Edit tag
172-
:param id: Id from tag
173-
'''
174-
my_tag = Tag.query.filter_by(id=id).first()
175-
form = TagForm(obj=my_tag)
176-
if form.validate_on_submit():
177-
try:
178-
# Update tag
179-
form.populate_obj(my_tag)
180-
db.session.add(my_tag)
181-
db.session.commit()
182-
flash('Aggiornamento avvenuto con successo', 'success')
183-
except Exception as e:
184-
db.session.rollback()
185-
flash("Errore durante l'aggiornamento del tag: %s" % str(e), 'danger')
186-
return render_template(
187-
'/tags_edit.html',
188-
form=form,tag=my_tag)

0 commit comments

Comments
 (0)