Commit 65b2833eb77d0da5760c1aa1996940735f3e41d3

Authored by hitier
1 parent 3262868b

Fix some sonar code smells

app/auth/models.py
... ... @@ -30,7 +30,7 @@ _nameToRole = {
30 30 }
31 31  
32 32  
33   -def _checkRole(role):
  33 +def _check_role(role):
34 34 if isinstance(role, int):
35 35 rv = role
36 36 elif str(role) == role:
... ... @@ -65,14 +65,14 @@ class User(UserMixin, db.Model):
65 65 self.password = None
66 66  
67 67 def set_role(self, role):
68   - self.role = _checkRole(role)
  68 + self.role = _check_role(role)
69 69  
70 70 def has_role(self, role):
71   - role = _checkRole(role)
  71 + role = _check_role(role)
72 72 return self.role == role
73 73  
74 74 def has_role_or_higher(self, role):
75   - role = _checkRole(role)
  75 + role = _check_role(role)
76 76 return self.role and (self.role >= role)
77 77  
78 78 def set_password(self, password):
... ...
app/auth/routes.py
... ... @@ -7,6 +7,8 @@ from flask_login import login_user, logout_user
7 7 from app.auth.models import User
8 8 from app.auth import bp
9 9  
  10 +main_index = 'main.index'
  11 +
10 12  
11 13 #
12 14 # Decorator used to protect routes by role
... ... @@ -30,10 +32,10 @@ def role_required(role):
30 32 try:
31 33 is_authorised = current_user.has_role_or_higher(role)
32 34 except ValueError:
33   - raise Exception("Unknowk role provided %s" % role)
  35 + raise ValueError("Unknown role provided %s" % role)
34 36 if not is_authorised:
35 37 flash("Vous n'avez pas les autorisations pour accéder à cette page", 'dark')
36   - return redirect(url_for('main.index'))
  38 + return redirect(url_for(main_index))
37 39 return f(*args, **kwargs)
38 40  
39 41 return decorated_function
... ... @@ -50,12 +52,11 @@ def login():
50 52 def login_post():
51 53 user_login = request.form.get('login')
52 54 user_password = request.form.get('password')
53   - # user_remember = request.form.get('remember')
54 55 user = User.query.filter_by(login=user_login).one_or_none()
55 56 if user and user.check_password(user_password):
56 57 login_user(user)
57 58 flash("Connection Réussie !", 'success')
58   - return redirect(url_for('main.index'))
  59 + return redirect(url_for(main_index))
59 60 else:
60 61 flash("Mauvais login ou mot de passe.", 'warning')
61 62 return redirect(url_for('auth.login'))
... ... @@ -65,4 +66,4 @@ def login_post():
65 66 def logout():
66 67 logout_user()
67 68 flash("Vous êtes maintenant déconnecté", 'info')
68   - return redirect(url_for('main.index'))
  69 + return redirect(url_for(main_index))
... ...
app/commands/commands.py
... ... @@ -433,7 +433,7 @@ def feed_periods(begin_year, end_year):
433 433 @click.option('--agent', '-a', 'agent', default=None, help="the agent id you want to charge")
434 434 def feed_random_charges(agent):
435 435 """ Randomly fill in the agents charges. """
436   - for i in range(0, 100):
  436 + for _ in range(0, 100):
437 437 if agent is None:
438 438 agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all()])
439 439 else:
... ...
app/errors/handlers.py
... ... @@ -2,30 +2,32 @@ from flask import render_template
2 2 from app import db
3 3 from . import bp
4 4  
5   -
6 5 # Inspired by:
7 6 # https://flask.palletsprojects.com/en/master/patterns/errorpages/
8 7  
  8 +error_page = 'error.html'
  9 +
  10 +
9 11 @bp.app_errorhandler(403)
10 12 def forbidden_error(error):
11 13 error_title = "Page Interdite"
12   - return render_template('error.html', error_title=error_title, error_msg=error), 403
  14 + return render_template(error_page, error_title=error_title, error_msg=error), 403
13 15  
14 16  
15 17 @bp.app_errorhandler(404)
16 18 def not_found_error(error):
17 19 error_title = "Page Introuvable."
18   - return render_template('error.html', error_title=error_title, error_msg=error), 404
  20 + return render_template(error_page, error_title=error_title, error_msg=error), 404
19 21  
20 22  
21 23 @bp.app_errorhandler(405)
22 24 def method_error(error):
23 25 error_title = "Erreur de Méthode."
24   - return render_template('error.html', error_title=error_title, error_msg=error), 405
  26 + return render_template(error_page, error_title=error_title, error_msg=error), 405
25 27  
26 28  
27 29 @bp.app_errorhandler(500)
28 30 def internal_error(error):
29 31 db.session.rollback()
30 32 error_title = "Erreur Interne. Administrateur Prévenu."
31   - return render_template('error.html', error_title=error_title, error_msg=error), 500
  33 + return render_template(error_page, error_title=error_title, error_msg=error), 500
... ...