things
This commit is contained in:
@ -40,44 +40,74 @@ def inject_image_helper():
|
||||
@bp.route('/', methods=['GET'])
|
||||
@login_required
|
||||
def index():
|
||||
plants = (
|
||||
# ── 1) Read query-params ───────────────────────────────────────────
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = request.args.get(
|
||||
'per_page',
|
||||
current_app.config.get('PLANTS_PER_PAGE', 12),
|
||||
type=int
|
||||
)
|
||||
view_mode = request.args.get('view', 'grid', type=str) # 'grid' or 'list'
|
||||
q = request.args.get('q', '', type=str).strip()
|
||||
type_filter= request.args.get('type', '', type=str).strip().lower()
|
||||
|
||||
# ── 2) Build base SQLAlchemy query ────────────────────────────────
|
||||
qry = (
|
||||
Plant.query
|
||||
.options(joinedload(Plant.media_items))
|
||||
.filter_by(owner_id=current_user.id)
|
||||
.order_by(Plant.id.desc())
|
||||
.all()
|
||||
.options(joinedload(Plant.media_items))
|
||||
.filter_by(owner_id=current_user.id)
|
||||
)
|
||||
|
||||
user_plants_count = Plant.query.filter_by(owner_id=current_user.id).count()
|
||||
user_images_count = Media.query.filter_by(uploader_id=current_user.id).count()
|
||||
total_plants_count = Plant.query.count()
|
||||
total_images_count = Media.query.count()
|
||||
# ── 3) Optional name search ───────────────────────────────────────
|
||||
if q:
|
||||
qry = qry.join(PlantCommonName).filter(
|
||||
PlantCommonName.name.ilike(f'%{q}%')
|
||||
)
|
||||
|
||||
# ── 4) Optional type filter ───────────────────────────────────────
|
||||
if type_filter:
|
||||
qry = qry.filter(Plant.plant_type.ilike(type_filter))
|
||||
|
||||
# ── 5) Apply ordering + paginate ─────────────────────────────────
|
||||
pagination = (
|
||||
qry.order_by(Plant.id.desc())
|
||||
.paginate(page=page, per_page=per_page, error_out=False)
|
||||
)
|
||||
plants = pagination.items
|
||||
|
||||
# ── 6) Gather stats and distinct types as before ─────────────────
|
||||
stats = {
|
||||
'user_plants': Plant.query.filter_by(owner_id=current_user.id).count(),
|
||||
'user_images': Media.query.filter_by(uploader_id=current_user.id).count(),
|
||||
'total_plants': Plant.query.count(),
|
||||
'total_images': Media.query.count(),
|
||||
}
|
||||
|
||||
plant_types = [
|
||||
pt[0]
|
||||
for pt in (
|
||||
db.session.query(Plant.plant_type)
|
||||
.filter_by(owner_id=current_user.id)
|
||||
.distinct()
|
||||
.all()
|
||||
row[0]
|
||||
for row in (
|
||||
db.session
|
||||
.query(Plant.plant_type)
|
||||
.filter_by(owner_id=current_user.id)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
]
|
||||
|
||||
stats = {
|
||||
'user_plants': user_plants_count,
|
||||
'user_images': user_images_count,
|
||||
'total_plants': total_plants_count,
|
||||
'total_images': total_images_count,
|
||||
}
|
||||
|
||||
# ── 7) Render, passing both pagination AND per-page items ─────────
|
||||
return render_template(
|
||||
'plant/index.html',
|
||||
plants=plants,
|
||||
plant_types=plant_types,
|
||||
stats=stats,
|
||||
plants = plants,
|
||||
pagination = pagination,
|
||||
view_mode = view_mode,
|
||||
q = q,
|
||||
type_filter = type_filter,
|
||||
per_page = per_page,
|
||||
plant_types = plant_types,
|
||||
stats = stats
|
||||
)
|
||||
|
||||
@bp.route('/', methods=['GET', 'POST'])
|
||||
@bp.route('/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create():
|
||||
form = PlantForm()
|
||||
|
Reference in New Issue
Block a user