|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
-
-
-
-
-
-
-
-
-
- from flask import Blueprint, render_template, request, flash, redirect, url_for
- from flask_login import login_required, current_user
- from .models import Practice
- from werkzeug.exceptions import abort
- from . import db
-
- practice = Blueprint('practice', __name__)
-
-
- def get_practice(practice_id):
- practice = Practice.query.filter_by(id=practice_id).first()
- if practice is None:
- abort(404)
- return practice
-
-
- @practice.route('/practices')
- def get_practices():
- practices = Practice.query
- return render_template('practices.html', practices=practices)
-
-
- @practice.route('/practices/<int:practice_id>')
- def show_practice(practice_id):
- practice = get_practice(practice_id)
- return render_template('practice.html', practice=practice)
-
-
- @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
- @login_required
- def edit_practice(practice_id):
- practice = get_practice(practice_id)
-
- if request.method == 'POST':
- name = request.form['name']
- description = request.form['description']
-
- if not name:
- flash('Name is required!')
- else:
- practice = Practice.query.get(practice_id)
- practice.name = name
- practice.description = description
- db.session.commit()
- return redirect(url_for('practice.get_practices'))
-
- return render_template('edit.html', practice=practice)
-
-
- @practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
- @login_required
- def delete_practice(practice_id):
- practice = get_practice(practice_id)
- deletion = Practice.query.get(practice_id)
- db.session.delete(deletion)
- db.session.commit()
- flash('Successfully deleted!')
- return redirect(url_for('practice.get_practices'))
|