Jul 8 - Implement Regenerate token and buik print QR codes
This commit is contained in:
@@ -151,6 +151,66 @@ def facility_qr_page(facility_id):
|
||||
return render_template('facilities/qr.html',
|
||||
facility=facility, public_url=public_url)
|
||||
|
||||
|
||||
@bp.route('/<int:facility_id>/qr/regenerate', methods=['POST'])
|
||||
@login_required
|
||||
@supervisor_required
|
||||
def facility_qr_regenerate(facility_id):
|
||||
"""Mint a NEW public token, invalidating any previously printed QR code."""
|
||||
facility = db.session.get(Facility, facility_id)
|
||||
if facility is None:
|
||||
abort(404)
|
||||
|
||||
facility.public_token = Facility.generate_public_token()
|
||||
db.session.commit()
|
||||
|
||||
logger.info('FACILITIES | qr_regenerate | user=%s | facility_id=%s',
|
||||
current_user.username, facility.id)
|
||||
log_action(ACTION_UPDATE, 'Facility', facility.id, facility.name,
|
||||
'regenerated public QR token (old code invalidated)')
|
||||
flash('QR code regenerated. Any previously printed codes for this facility no '
|
||||
'longer work — reprint and repost.', 'warning')
|
||||
return redirect(url_for('facilities.facility_qr_page', facility_id=facility.id))
|
||||
|
||||
|
||||
@bp.route('/qr/print-all')
|
||||
@login_required
|
||||
def facility_qr_print_all():
|
||||
"""Printable sheet of QR codes for all facilities the user can see.
|
||||
|
||||
Optional ?contract_id=<id> limits the sheet to one contract. Customers have
|
||||
no QR access (403); inspectors are scoped to their contracted facilities.
|
||||
"""
|
||||
if current_user.role == 'customer':
|
||||
abort(403)
|
||||
|
||||
contract_id = request.args.get('contract_id', type=int)
|
||||
|
||||
if current_user.role == 'inspector':
|
||||
fids = get_inspector_scope(current_user) or []
|
||||
query = Facility.query.filter(Facility.id.in_(fids), Facility.active == True)
|
||||
else:
|
||||
query = Facility.query.filter(Facility.active == True)
|
||||
|
||||
if contract_id:
|
||||
query = query.filter(Facility.project_id == contract_id)
|
||||
|
||||
facilities = query.order_by(Facility.name).all()
|
||||
|
||||
# Ensure every facility on the sheet has a token so its qr.png renders.
|
||||
changed = False
|
||||
for f in facilities:
|
||||
if not f.public_token:
|
||||
f.ensure_public_token()
|
||||
changed = True
|
||||
if changed:
|
||||
db.session.commit()
|
||||
|
||||
selected_contract = db.session.get(Project, contract_id) if contract_id else None
|
||||
return render_template('facilities/qr_print_all.html',
|
||||
facilities=facilities,
|
||||
selected_contract=selected_contract)
|
||||
|
||||
@bp.route('/<int:facility_id>/edit', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@supervisor_required
|
||||
|
||||
Reference in New Issue
Block a user