changes
This commit is contained in:
1
plugins/media/__init__.py
Normal file
1
plugins/media/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# media plugin init
|
14
plugins/media/forms.py
Normal file
14
plugins/media/forms.py
Normal file
@ -0,0 +1,14 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, SelectField, SubmitField, IntegerField
|
||||
from flask_wtf.file import FileField, FileAllowed, FileRequired
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
class MediaUploadForm(FlaskForm):
|
||||
image = FileField('Image', validators=[
|
||||
FileRequired(),
|
||||
FileAllowed(['jpg', 'jpeg', 'png', 'gif'], 'Images only!')
|
||||
])
|
||||
caption = StringField('Caption')
|
||||
plant_id = IntegerField('Plant ID')
|
||||
growlog_id = IntegerField('GrowLog ID')
|
||||
submit = SubmitField('Upload')
|
12
plugins/media/models.py
Normal file
12
plugins/media/models.py
Normal file
@ -0,0 +1,12 @@
|
||||
from app import db
|
||||
from datetime import datetime
|
||||
|
||||
class Media(db.Model):
|
||||
__tablename__ = 'media'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
file_url = db.Column(db.String(256), nullable=False)
|
||||
uploaded_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=True)
|
||||
growlog_id = db.Column(db.Integer, db.ForeignKey('grow_logs.id'), nullable=True)
|
||||
caption = db.Column(db.String(255), nullable=True)
|
1
plugins/media/plugin.json
Normal file
1
plugins/media/plugin.json
Normal file
@ -0,0 +1 @@
|
||||
{ "name": "media", "version": "1.0", "description": "Upload and attach media to plants and grow logs" }
|
42
plugins/media/routes.py
Normal file
42
plugins/media/routes.py
Normal file
@ -0,0 +1,42 @@
|
||||
import os
|
||||
import uuid
|
||||
from flask import Blueprint, render_template, redirect, url_for, request, current_app, flash
|
||||
from flask_login import login_required
|
||||
from werkzeug.utils import secure_filename
|
||||
from app import db
|
||||
from .models import Media
|
||||
from .forms import MediaUploadForm
|
||||
|
||||
bp = Blueprint('media', __name__, template_folder='templates')
|
||||
|
||||
@bp.route('/media/upload', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def upload_media():
|
||||
form = MediaUploadForm()
|
||||
if form.validate_on_submit():
|
||||
file = form.image.data
|
||||
filename = f"{uuid.uuid4().hex}_{secure_filename(file.filename)}"
|
||||
upload_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
|
||||
file.save(upload_path)
|
||||
media = Media(
|
||||
file_url=filename,
|
||||
caption=form.caption.data,
|
||||
plant_id=form.plant_id.data or None,
|
||||
growlog_id=form.growlog_id.data or None
|
||||
)
|
||||
db.session.add(media)
|
||||
db.session.commit()
|
||||
flash("Image uploaded successfully.", "success")
|
||||
return redirect(url_for('media.upload_media'))
|
||||
return render_template('media/upload.html', form=form)
|
||||
|
||||
@bp.route('/media')
|
||||
@login_required
|
||||
def list_media():
|
||||
images = Media.query.order_by(Media.uploaded_at.desc()).all()
|
||||
return render_template('media/list.html', images=images)
|
||||
|
||||
@bp.route('/media/files/<filename>')
|
||||
def media_file(filename):
|
||||
from flask import send_from_directory
|
||||
return send_from_directory(current_app.config['UPLOAD_FOLDER'], filename)
|
14
plugins/media/templates/media/list.html
Normal file
14
plugins/media/templates/media/list.html
Normal file
@ -0,0 +1,14 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<h2>All Uploaded Media</h2>
|
||||
<ul>
|
||||
{% for image in images %}
|
||||
<li>
|
||||
<img src="{{ url_for('media.media_file', filename=image.file_url) }}" alt="{{ image.caption }}" width="200"><br>
|
||||
{{ image.caption or "No caption" }}
|
||||
{% if image.plant_id %}<br>Plant ID: {{ image.plant_id }}{% endif %}
|
||||
{% if image.growlog_id %}<br>GrowLog ID: {{ image.growlog_id }}{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
12
plugins/media/templates/media/upload.html
Normal file
12
plugins/media/templates/media/upload.html
Normal file
@ -0,0 +1,12 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<h2>Upload Media</h2>
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
{{ form.hidden_tag() }}
|
||||
<p>{{ form.image.label }}<br>{{ form.image() }}</p>
|
||||
<p>{{ form.caption.label }}<br>{{ form.caption(size=40) }}</p>
|
||||
<p>{{ form.plant_id.label }}<br>{{ form.plant_id() }}</p>
|
||||
<p>{{ form.growlog_id.label }}<br>{{ form.growlog_id() }}</p>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</form>
|
||||
{% endblock %}
|
Reference in New Issue
Block a user