changes
This commit is contained in:
1
plugins/growlog/__init__.py
Normal file
1
plugins/growlog/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# growlog plugin init
|
15
plugins/growlog/forms.py
Normal file
15
plugins/growlog/forms.py
Normal file
@ -0,0 +1,15 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import TextAreaField, SelectField, SubmitField
|
||||
from wtforms.validators import DataRequired, Length
|
||||
|
||||
class GrowLogForm(FlaskForm):
|
||||
event_type = SelectField('Event Type', choices=[
|
||||
('water', 'Watered'),
|
||||
('fertilizer', 'Fertilized'),
|
||||
('repot', 'Repotted'),
|
||||
('note', 'Note'),
|
||||
('pest', 'Pest Observed')
|
||||
], validators=[DataRequired()])
|
||||
|
||||
note = TextAreaField('Notes', validators=[Length(max=1000)])
|
||||
submit = SubmitField('Add Log')
|
15
plugins/growlog/models.py
Normal file
15
plugins/growlog/models.py
Normal file
@ -0,0 +1,15 @@
|
||||
from app import db
|
||||
from datetime import datetime
|
||||
|
||||
class GrowLog(db.Model):
|
||||
__tablename__ = 'grow_logs'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
event_type = db.Column(db.String(64), nullable=False)
|
||||
note = db.Column(db.Text)
|
||||
|
||||
media = db.relationship("Media", backref="growlog", lazy=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<GrowLog {self.event_type} @ {self.timestamp}>"
|
1
plugins/growlog/plugin.json
Normal file
1
plugins/growlog/plugin.json
Normal file
@ -0,0 +1 @@
|
||||
{ "name": "growlog", "version": "1.0", "description": "Tracks time-based plant care logs" }
|
31
plugins/growlog/routes.py
Normal file
31
plugins/growlog/routes.py
Normal file
@ -0,0 +1,31 @@
|
||||
from flask import Blueprint, render_template, redirect, url_for, request
|
||||
from flask_login import login_required
|
||||
from app import db
|
||||
from .models import GrowLog
|
||||
from .forms import GrowLogForm
|
||||
from plugins.plant.models import Plant
|
||||
|
||||
bp = Blueprint('growlog', __name__, template_folder='templates')
|
||||
|
||||
@bp.route('/plants/<int:plant_id>/logs')
|
||||
@login_required
|
||||
def view_logs(plant_id):
|
||||
plant = Plant.query.get_or_404(plant_id)
|
||||
logs = GrowLog.query.filter_by(plant_id=plant.id).order_by(GrowLog.timestamp.desc()).all()
|
||||
return render_template('growlog/log_list.html', plant=plant, logs=logs)
|
||||
|
||||
@bp.route('/plants/<int:plant_id>/logs/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_log(plant_id):
|
||||
plant = Plant.query.get_or_404(plant_id)
|
||||
form = GrowLogForm()
|
||||
if form.validate_on_submit():
|
||||
log = GrowLog(
|
||||
plant_id=plant.id,
|
||||
event_type=form.event_type.data,
|
||||
note=form.note.data
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
return redirect(url_for('growlog.view_logs', plant_id=plant.id))
|
||||
return render_template('growlog/log_form.html', form=form, plant=plant)
|
10
plugins/growlog/templates/growlog/log_form.html
Normal file
10
plugins/growlog/templates/growlog/log_form.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<h2>Add Log for Plant #{{ plant.id }}</h2>
|
||||
<form method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
<p>{{ form.event_type.label }}<br>{{ form.event_type() }}</p>
|
||||
<p>{{ form.note.label }}<br>{{ form.note(rows=4) }}</p>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</form>
|
||||
{% endblock %}
|
25
plugins/growlog/templates/growlog/log_list.html
Normal file
25
plugins/growlog/templates/growlog/log_list.html
Normal file
@ -0,0 +1,25 @@
|
||||
{% import 'core_ui/_media_macros.html' as media %}
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<h2>Logs for Plant #{{ plant.id }}</h2>
|
||||
<a href="{{ url_for('growlog.add_log', plant_id=plant.id) }}">Add New Log</a>
|
||||
<ul>
|
||||
{% for log in logs %}
|
||||
<li>
|
||||
<strong>{{ log.timestamp.strftime('%Y-%m-%d') }}:</strong> {{ log.event_type }} - {{ log.note }}
|
||||
{% if log.media %}
|
||||
<br><em>Images:</em>
|
||||
<ul>
|
||||
{% for image in log.media %}
|
||||
<li>
|
||||
<img src="{{ url_for('media.media_file', filename=image.file_url) }}" width="150"><br>
|
||||
{{ image.caption or "No caption" }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{{ media.render_media_list(log.media) }}
|
||||
{% endblock %}
|
Reference in New Issue
Block a user