Commit 65b2833eb77d0da5760c1aa1996940735f3e41d3

Authored by hitier
1 parent 3262868b

Fix some sonar code smells

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