Ajouter des données fictives aux aperçus
Some checks are pending
CI - Tests et Syntax / lint-and-test (push) Waiting to run

This commit is contained in:
root 2026-08-21 12:15:10 +00:00
parent c4db4a1e12
commit b1f1e8b604
4 changed files with 48 additions and 3 deletions

View file

@ -452,6 +452,7 @@ def template_debug_preview():
from jinja2 import ChoiceLoader, FileSystemLoader, TemplateNotFound, ChainableUndefined
relative_path = (request.args.get('path') or '').strip()
demo_mode = request.args.get('demo', '1') != '0'
files, _ = _template_inventory()
item = next((candidate for candidate in files if candidate['path'] == relative_path), None)
if not item:
@ -481,12 +482,44 @@ def template_debug_preview():
# Reproduire les variables globales Flask (current_user, config,
# request...) sans injecter d'objet métier réel dans la prévisualisation.
current_app.update_template_context(preview_context)
if demo_mode:
from types import SimpleNamespace
class DemoObject(SimpleNamespace):
def __getattr__(self, name):
return ''
demo_room = DemoObject(id=12, name='B102', code='B102', full_name='Bâtiment principal — Zone Anglais — B102')
demo_equipment = DemoObject(
id=42, name='Luminaire LED central', code='ECL-0042',
full_path='Bâtiment principal / Zone Anglais / B102 / Éclairage',
room=demo_room, status='en_service', status_label='En service',
)
preview_context.update({
'current_user': DemoObject(id=1, username='demo.admin', full_name='Administrateur démo', role='admin', role_label='Administrateur système', is_authenticated=True),
'stats': DemoObject(alerts_unread=3, interventions_pending=4, equipments=128),
'unread': 3,
'alerts': [DemoObject(id=1, title='Luminaire défaillant', message='Une intervention est à traiter.', level='warning')],
'interventions': [DemoObject(id=39, title='Remplacement du luminaire', status='en_cours', status_label='En cours', room=demo_room, equipment=demo_equipment)],
'intervention': DemoObject(id=39, title='Remplacement du luminaire', type='curatif', status='en_cours', status_label='En cours', room=demo_room, equipment=demo_equipment),
'rooms': [demo_room, DemoObject(id=13, name='B103', code='B103', full_name='Bâtiment principal — Zone Anglais — B103')],
'equipments': [demo_equipment, DemoObject(id=43, name='Prise murale fond de salle', code='ELEC-0043', room=demo_room)],
'categories': [DemoObject(id=1, name='Éclairage'), DemoObject(id=2, name='Prises')],
'buildings': [DemoObject(id=1, name='Bâtiment principal')],
'zones': [DemoObject(id=1, name='Zone Anglais')],
'messages': [DemoObject(subject='Demande de réparation', sender='Vie scolaire', body_preview='Une chaise est cassée en B102.')],
'mails': [DemoObject(subject='Maintenance urgente', sender='service@exemple.fr', received_at='21/08/2026 08:15')],
'tasks': [DemoObject(id=7, name='Contrôle mensuel des luminaires', periodicity='Mensuelle', duration_minutes=30)],
'lots': [DemoObject(id=3, name='Éclairage intérieur', category=DemoObject(name='Éclairage'))],
'companies': [DemoObject(id=2, name='Entreprise Démo Maintenance')],
})
rendered = template.render(**preview_context)
except (TemplateNotFound, Exception) as exc:
render_error = f'{type(exc).__name__}: {exc}'
return render_template('admin/template_preview.html', template=item,
source=source, rendered=rendered, render_error=render_error)
source=source, rendered=rendered, render_error=render_error,
demo_mode=demo_mode)
@admin_bp.route('/debug/templates/mark', methods=['POST'])

View file

@ -81,7 +81,7 @@
{% else %}<span class="badge bg-warning text-dark">Masqué probablement</span>{% endif %}
</td>
<td><code title="{{ template.hash }}">{{ template.hash[:10] }}</code></td>
<td><a class="btn btn-outline-primary btn-sm" href="{{ url_for('admin.template_debug_preview', path=template.path) }}" target="_blank" rel="noopener"><i class="bi bi-eye"></i> Prévisualiser</a></td>
<td><a class="btn btn-outline-primary btn-sm" href="{{ url_for('admin.template_debug_preview', path=template.path, demo=1) }}" target="_blank" rel="noopener"><i class="bi bi-eye"></i> Prévisualiser</a></td>
<td>
<form method="post" action="{{ url_for('admin.template_debug_mark') }}" class="d-flex gap-1">
<input type="hidden" name="path" value="{{ template.path }}">

View file

@ -10,6 +10,17 @@
<a href="{{ url_for('admin.template_debug', filter='all') }}" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> Retour à laudit</a>
</div>
<div class="alert {% if demo_mode %}alert-success{% else %}alert-secondary{% endif %} py-2 d-flex flex-wrap gap-2 justify-content-between align-items-center">
<span><i class="bi bi-{% if demo_mode %}magic{% else %}braces{% endif %}"></i>
{% if demo_mode %}<strong>Données fictives actives.</strong> Le rendu utilise un contexte GMAO de démonstration, sans écriture en base.{% else %}<strong>Contexte vide.</strong> Les variables métier absentes sont tolérées.{% endif %}
</span>
{% if demo_mode %}
<a class="btn btn-sm btn-outline-secondary" href="{{ url_for('admin.template_debug_preview', path=template.path, demo=0) }}">Tester sans données</a>
{% else %}
<a class="btn btn-sm btn-outline-success" href="{{ url_for('admin.template_debug_preview', path=template.path, demo=1) }}">Charger les données fictives</a>
{% endif %}
</div>
{% if render_error %}
<div class="alert alert-warning">
<strong><i class="bi bi-exclamation-triangle"></i> Rendu incomplet</strong>

View file

@ -10,10 +10,11 @@ def test_template_audit_can_mark_and_restore_without_deleting(authenticated_clie
assert 'Identiques' in response.get_data(as_text=True)
assert 'Divergents' in response.get_data(as_text=True)
response = authenticated_client.get('/admin/debug/templates/preview?path=templates%2Fadmin%2Ftemplate_debug.html')
response = authenticated_client.get('/admin/debug/templates/preview?path=templates%2Fadmin%2Ftemplate_debug.html&demo=1')
assert response.status_code == 200
assert 'Prévisualisation du template' in response.get_data(as_text=True)
assert 'sandbox' in response.get_data(as_text=True)
assert 'Données fictives actives' in response.get_data(as_text=True)
response = authenticated_client.post('/admin/debug/templates/mark', data={
'path': template_path,