44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
from flask import Blueprint, render_template, redirect, url_for, request, flash
|
|
from app import db
|
|
from .models import Plant
|
|
from .forms import PlantForm
|
|
|
|
bp = Blueprint('plant', __name__, template_folder='templates')
|
|
|
|
@bp.route('/plants')
|
|
def index():
|
|
plants = Plant.query.order_by(Plant.created_at.desc()).all()
|
|
return render_template('plant/index.html', plants=plants)
|
|
|
|
@bp.route('/plants/<int:plant_id>')
|
|
def detail(plant_id):
|
|
plant = Plant.query.get_or_404(plant_id)
|
|
return render_template('plant/detail.html', plant=plant)
|
|
|
|
@bp.route('/plants/new', methods=['GET', 'POST'])
|
|
def create():
|
|
form = PlantForm()
|
|
if form.validate_on_submit():
|
|
plant = Plant(
|
|
name=form.name.data,
|
|
type=form.type.data,
|
|
notes=form.notes.data,
|
|
is_active=form.is_active.data
|
|
)
|
|
db.session.add(plant)
|
|
db.session.commit()
|
|
flash('Plant created successfully.', 'success')
|
|
return redirect(url_for('plant.index'))
|
|
return render_template('plant/form.html', form=form)
|
|
|
|
@bp.route('/plants/<int:plant_id>/edit', methods=['GET', 'POST'])
|
|
def edit(plant_id):
|
|
plant = Plant.query.get_or_404(plant_id)
|
|
form = PlantForm(obj=plant)
|
|
if form.validate_on_submit():
|
|
form.populate_obj(plant)
|
|
db.session.commit()
|
|
flash('Plant updated successfully.', 'success')
|
|
return redirect(url_for('plant.detail', plant_id=plant.id))
|
|
return render_template('plant/form.html', form=form, plant=plant)
|