05/27 Update template toggle function
This commit is contained in:
@@ -13,6 +13,7 @@ class InspectionTemplate(db.Model):
|
||||
created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
|
||||
created_at = db.Column(db.DateTime, default=now_eastern)
|
||||
form_schema = db.Column(db.JSON, nullable=True)
|
||||
active = db.Column(db.Boolean, default=True, nullable=False)
|
||||
|
||||
checklist_items = db.relationship('ChecklistItem', backref='template', lazy='dynamic', cascade='all, delete-orphan')
|
||||
inspections = db.relationship('Inspection', backref='template', lazy='dynamic')
|
||||
|
||||
@@ -270,7 +270,7 @@ def index():
|
||||
def start():
|
||||
form = StartInspectionForm()
|
||||
|
||||
templates = InspectionTemplate.query.order_by(InspectionTemplate.name).all()
|
||||
templates = InspectionTemplate.query.filter_by(active=True).order_by(InspectionTemplate.name).all()
|
||||
projects = Project.query.filter_by(active=True).order_by(Project.name).all()
|
||||
|
||||
# Scope projects to inspector's assigned contracts
|
||||
|
||||
@@ -153,6 +153,23 @@ def delete_template(template_id):
|
||||
return redirect(url_for('templates.index'))
|
||||
|
||||
|
||||
@bp.route('/<int:template_id>/toggle-active', methods=['POST'])
|
||||
@login_required
|
||||
@supervisor_required
|
||||
def toggle_active(template_id):
|
||||
template = db.session.get(InspectionTemplate, template_id)
|
||||
if template is None:
|
||||
abort(404)
|
||||
template.active = not template.active
|
||||
db.session.commit()
|
||||
state = 'activated' if template.active else 'deactivated'
|
||||
logger.info('TEMPLATES | toggle_active | user=%s | template_id=%s active=%s',
|
||||
current_user.username, template.id, template.active)
|
||||
log_action(ACTION_UPDATE, 'Template', template.id, template.name, f'active={template.active}')
|
||||
flash(f'Template "{template.name}" {state}.', 'success')
|
||||
return redirect(url_for('templates.index'))
|
||||
|
||||
|
||||
@bp.route('/<int:template_id>/duplicate', methods=['POST'])
|
||||
@login_required
|
||||
@supervisor_required
|
||||
|
||||
@@ -19,12 +19,17 @@
|
||||
<div class="row">
|
||||
{% for template in templates %}
|
||||
<div class="col-md-6 col-lg-4 mb-4">
|
||||
<div class="card shadow-sm h-100">
|
||||
<div class="card shadow-sm h-100 {% if not template.active %}opacity-75 border-secondary{% endif %}">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">
|
||||
<a href="{{ url_for('templates.view_template', template_id=template.id) }}" class="text-decoration-none">
|
||||
<h5 class="card-title d-flex align-items-start gap-2">
|
||||
<a href="{{ url_for('templates.view_template', template_id=template.id) }}" class="text-decoration-none flex-grow-1">
|
||||
{{ template.name }}
|
||||
</a>
|
||||
{% if template.active %}
|
||||
<span class="badge bg-success flex-shrink-0">Active</span>
|
||||
{% else %}
|
||||
<span class="badge bg-secondary flex-shrink-0">Inactive</span>
|
||||
{% endif %}
|
||||
</h5>
|
||||
<p class="card-text text-muted small">{{ template.description or 'No description' }}</p>
|
||||
<div class="mt-3">
|
||||
@@ -66,6 +71,24 @@
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<!-- Toggle active/inactive -->
|
||||
<form method="POST"
|
||||
action="{{ url_for('templates.toggle_active', template_id=template.id) }}"
|
||||
class="d-inline">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
{% if template.active %}
|
||||
<button type="submit" class="btn btn-sm btn-outline-warning"
|
||||
title="Deactivate template">
|
||||
<i class="bi bi-pause-circle"></i> Deactivate
|
||||
</button>
|
||||
{% else %}
|
||||
<button type="submit" class="btn btn-sm btn-outline-success"
|
||||
title="Activate template">
|
||||
<i class="bi bi-play-circle"></i> Activate
|
||||
</button>
|
||||
{% endif %}
|
||||
</form>
|
||||
|
||||
<!-- Delete -->
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-danger ms-auto"
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
"""phase21 — template active flag
|
||||
|
||||
Adds `active` boolean column to `inspection_templates` so templates can be
|
||||
deactivated without deletion. Inactive templates are hidden from the
|
||||
inspection-start form but remain accessible in the template management UI.
|
||||
|
||||
Safe to re-run — uses INFORMATION_SCHEMA column existence check.
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = 'phase21_template_active'
|
||||
down_revision = 'phase20_inspector_assignments'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _column_exists(bind, table, column):
|
||||
result = bind.execute(sa.text(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
|
||||
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t AND COLUMN_NAME = :c"
|
||||
), {"t": table, "c": column})
|
||||
return result.scalar() > 0
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
if not _column_exists(bind, 'inspection_templates', 'active'):
|
||||
op.add_column(
|
||||
'inspection_templates',
|
||||
sa.Column('active', sa.Boolean(), nullable=False, server_default=sa.true())
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
if _column_exists(bind, 'inspection_templates', 'active'):
|
||||
op.drop_column('inspection_templates', 'active')
|
||||
Reference in New Issue
Block a user