Commit a49732b54352c411881769940013e4aa89713c92

Authored by hitier
1 parent 0d4479a4

Add current user to base page

app/auth/routes.py
@@ -16,6 +16,7 @@ def login_post(): @@ -16,6 +16,7 @@ def login_post():
16 user_password = request.form.get('password') 16 user_password = request.form.get('password')
17 user_remember = request.form.get('remember') 17 user_remember = request.form.get('remember')
18 user = User.query.filter_by(login=user_login).one_or_none() 18 user = User.query.filter_by(login=user_login).one_or_none()
  19 + print("user login: {}, user password: {}".format(user.login, user.password))
19 if user and user.password == user_password: 20 if user and user.password == user_password:
20 # login code goes here 21 # login code goes here
21 login_user(user) 22 login_user(user)
app/auth/templates/login.html
@@ -5,18 +5,19 @@ @@ -5,18 +5,19 @@
5 <link href="{{ url_for('auth.static', filename='signin.css') }}" rel="stylesheet" type="text/css"/> 5 <link href="{{ url_for('auth.static', filename='signin.css') }}" rel="stylesheet" type="text/css"/>
6 </head> 6 </head>
7 <body class="text-center"> 7 <body class="text-center">
8 - <form class="form-signin" method="POST" action="{{ url_for('auth.login')}}"> 8 + <form action="{{ url_for('auth.login')}}" class="form-signin" method="POST">
  9 + {% include 'flash-messages.html' %}
9 10
10 <img class="mb-4" height="72" src="{{ url_for('static', filename='img/pdc.png') }}" width="72"> 11 <img class="mb-4" height="72" src="{{ url_for('static', filename='img/pdc.png') }}" width="72">
11 <h1 class="h3 font-weight-normal mb-3">Connectez vous</h1> 12 <h1 class="h3 font-weight-normal mb-3">Connectez vous</h1>
12 <label class="sr-only" for="login">Email Adress</label> 13 <label class="sr-only" for="login">Email Adress</label>
13 - <input autofocus class="form-control" name="login" id="login" placeholder="identifiant" required > 14 + <input autofocus class="form-control" id="login" name="login" placeholder="identifiant" required>
14 <label class="sr-only" for="password">Password</label> 15 <label class="sr-only" for="password">Password</label>
15 - <input class="form-control" name="password" id="password" placeholder="mot de passe" required type="password"> 16 + <input class="form-control" id="password" name="password" placeholder="mot de passe" required type="password">
16 17
17 <div class="checkbox my-3"> 18 <div class="checkbox my-3">
18 - <label class="text-muted" data-toggle="tooltip" data-placement="top" title="désactivé">  
19 - <input type="checkbox" id="remember" name="remember" value="true" disabled > Se souvenir de moi 19 + <label class="text-muted" data-placement="top" data-toggle="tooltip" title="désactivé">
  20 + <input disabled id="remember" name="remember" type="checkbox" value="true"> Se souvenir de moi
20 </label> 21 </label>
21 </div> 22 </div>
22 23
@@ -26,6 +27,6 @@ @@ -26,6 +27,6 @@
26 {% include 'copy.html' %} 27 {% include 'copy.html' %}
27 </div> 28 </div>
28 </form> 29 </form>
29 -{% include 'bs4-scripts.html' %} 30 + {% include 'bs4-scripts.html' %}
30 </body> 31 </body>
31 </html> 32 </html>
app/cli/__init__.py
@@ -16,7 +16,7 @@ def delete(user_id): @@ -16,7 +16,7 @@ def delete(user_id):
16 @bp.cli.command('create_db') 16 @bp.cli.command('create_db')
17 def create(): 17 def create():
18 db.create_all() 18 db.create_all()
19 - admin = User(email='groz@free.fr', name='richard', password='1champ1') 19 + admin = User(email='groz@free.fr', name='richard', login='richard', password='1champ1')
20 db.session.add(admin) 20 db.session.add(admin)
21 db.session.commit() 21 db.session.commit()
22 22
@@ -34,20 +34,15 @@ def add(email, name, password): @@ -34,20 +34,15 @@ def add(email, name, password):
34 34
35 @bp.cli.command('show_all') 35 @bp.cli.command('show_all')
36 def show_all(): 36 def show_all():
37 - print("{:<5} {:<15} {:<15} {:<15}".format('id', 'name', 'passwd', 'email'))  
38 - print("{:<5} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15)) 37 + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email'))
  38 + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15))
39 for user in User.query.all(): 39 for user in User.query.all():
40 - print("{:<5} {:<15} {:<15} {:<15}".format( 40 + print(user.login)
  41 + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format(
41 user.id, 42 user.id,
42 user.name, 43 user.name,
  44 + user.login,
43 user.password, 45 user.password,
44 user.email 46 user.email
45 )) 47 ))
46 48
47 -  
48 -@bp.cli.command('create')  
49 -def create():  
50 - db.create_all()  
51 - admin = User(email='groz@free.fr', name='richard', password='1champ1')  
52 - db.session.add(admin)  
53 - db.session.commit()  
app/templates/base_page.html
@@ -8,9 +8,16 @@ @@ -8,9 +8,16 @@
8 8
9 <nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0"> 9 <nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
10 <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Plan de Charges</a> 10 <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Plan de Charges</a>
  11 + {% if not current_user.is_anonymous %}
11 <ul class="navbar-nav px-3"> 12 <ul class="navbar-nav px-3">
12 <li class="nav-item text-nowrap"> 13 <li class="nav-item text-nowrap">
13 - <a class="nav-link" href="{{ url_for('auth.login') }}">Sign out</a> 14 + <a class="nav-link" >Bienvenue {{current_user.name}}</a>
  15 + </li>
  16 + </ul>
  17 + {% endif %}
  18 + <ul class="navbar-nav px-3">
  19 + <li class="nav-item text-nowrap">
  20 + <a class="nav-link" href="{{ url_for('auth.login') }}">Déconnecter</a>
14 </li> 21 </li>
15 </ul> 22 </ul>
16 </nav> 23 </nav>
@@ -105,18 +112,9 @@ @@ -105,18 +112,9 @@
105 </nav> 112 </nav>
106 113
107 <main class="container" role="main"> 114 <main class="container" role="main">
108 - {% with messages = get_flashed_messages(with_categories=true) %}  
109 - {% if messages %}  
110 - <div id="messages">  
111 - {% for level, message in messages %}  
112 - <div class="alert alert-{{level}}" role="alert">{{ message }}</div>  
113 - {% endfor %}  
114 - </div>  
115 - {% endif %}  
116 - {% endwith %}  
117 -  
118 - {%block content%}  
119 - {%endblock%} 115 + {% include 'flash-messages.html' %}
  116 + {% block content %}
  117 + {% endblock %}
120 </main> 118 </main>
121 119
122 </div> 120 </div>
app/templates/flash-messages.html 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 + {% with messages = get_flashed_messages(with_categories=true) %}
  2 + {% if messages %}
  3 + <div id="messages">
  4 + {% for level, message in messages %}
  5 + <div class="alert alert-{{level}}" role="alert">{{ message }}</div>
  6 + {% endfor %}
  7 + </div>
  8 + {% endif %}
  9 + {% endwith %}