changes
This commit is contained in:
1
plugins/plant/__init__.py
Normal file
1
plugins/plant/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# plant plugin init
|
10
plugins/plant/forms.py
Normal file
10
plugins/plant/forms.py
Normal file
@ -0,0 +1,10 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, TextAreaField, BooleanField, SubmitField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
class PlantForm(FlaskForm):
|
||||
name = StringField('Name', validators=[DataRequired()])
|
||||
type = StringField('Type')
|
||||
notes = TextAreaField('Notes')
|
||||
is_active = BooleanField('Active', default=True)
|
||||
submit = SubmitField('Save')
|
11
plugins/plant/models.py
Normal file
11
plugins/plant/models.py
Normal file
@ -0,0 +1,11 @@
|
||||
from datetime import datetime
|
||||
from app import db
|
||||
|
||||
class Plant(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(128), nullable=False)
|
||||
type = db.Column(db.String(64))
|
||||
notes = db.Column(db.Text)
|
||||
is_active = db.Column(db.Boolean, default=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
43
plugins/plant/routes.py
Normal file
43
plugins/plant/routes.py
Normal file
@ -0,0 +1,43 @@
|
||||
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)
|
9
plugins/plant/templates/plant/detail.html
Normal file
9
plugins/plant/templates/plant/detail.html
Normal file
@ -0,0 +1,9 @@
|
||||
{% extends 'core_ui/base.html' %}
|
||||
{% block content %}
|
||||
<h1>{{ plant.name }}</h1>
|
||||
<p>Type: {{ plant.type }}</p>
|
||||
<p>{{ plant.notes }}</p>
|
||||
<p>Status: {% if plant.is_active %}Active{% else %}Inactive{% endif %}</p>
|
||||
<a href="{{ url_for('plant.edit', plant_id=plant.id) }}">Edit</a>
|
||||
<a href="{{ url_for('plant.index') }}">Back to list</a>
|
||||
{% endblock %}
|
12
plugins/plant/templates/plant/form.html
Normal file
12
plugins/plant/templates/plant/form.html
Normal file
@ -0,0 +1,12 @@
|
||||
{% extends 'core_ui/base.html' %}
|
||||
{% block content %}
|
||||
<h1>{% if plant %}Edit{% else %}New{% endif %} Plant</h1>
|
||||
<form method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
<p>{{ form.name.label }}<br>{{ form.name(size=40) }}</p>
|
||||
<p>{{ form.type.label }}<br>{{ form.type(size=40) }}</p>
|
||||
<p>{{ form.notes.label }}<br>{{ form.notes(rows=5, cols=40) }}</p>
|
||||
<p>{{ form.is_active() }} {{ form.is_active.label }}</p>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</form>
|
||||
{% endblock %}
|
10
plugins/plant/templates/plant/index.html
Normal file
10
plugins/plant/templates/plant/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends 'core_ui/base.html' %}
|
||||
{% block content %}
|
||||
<h1>Plant List</h1>
|
||||
<ul>
|
||||
{% for plant in plants %}
|
||||
<li><a href="{{ url_for('plant.detail', plant_id=plant.id) }}">{{ plant.name }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<a href="{{ url_for('plant.create') }}">Add New Plant</a>
|
||||
{% endblock %}
|
Reference in New Issue
Block a user