From 4488a72483be53fbdac8f1c59c8535d17d0447b4 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@ess.eu>
Date: Wed, 21 Oct 2020 17:11:52 +0200
Subject: [PATCH] Fix some code smells

Collapsible "if" statements should be merged

JIRA INFRA-2729 #action In Progress
---
 app/models.py        | 15 ++++++---------
 app/network/views.py | 15 +++++++--------
 app/utils.py         |  9 ++++-----
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/app/models.py b/app/models.py
index 0b56458..3b4005d 100644
--- a/app/models.py
+++ b/app/models.py
@@ -703,9 +703,8 @@ class CreatedMixin:
         # Automatically convert created_at/updated_at strings
         # to datetime object
         for key in ("created_at", "updated_at"):
-            if key in kwargs:
-                if isinstance(kwargs[key], str):
-                    kwargs[key] = utils.parse_to_utc(kwargs[key])
+            if key in kwargs and isinstance(kwargs[key], str):
+                kwargs[key] = utils.parse_to_utc(kwargs[key])
         super().__init__(**kwargs)
 
     def to_dict(self):
@@ -802,9 +801,8 @@ class Item(CreatedMixin, SearchableMixin, db.Model):
     @validates("ics_id")
     def validate_ics_id(self, key, string):
         """Ensure the ICS id field matches the required format"""
-        if string is not None:
-            if ICS_ID_RE.fullmatch(string) is None:
-                raise ValidationError("ICS id shall match [A-Z]{3}[0-9]{3}")
+        if string is not None and ICS_ID_RE.fullmatch(string) is None:
+            raise ValidationError("ICS id shall match [A-Z]{3}[0-9]{3}")
         return string
 
     def to_dict(self, recursive=False):
@@ -1042,9 +1040,8 @@ class DeviceType(db.Model):
     @validates("name")
     def validate_name(self, key, string):
         """Ensure the name field matches the required format"""
-        if string is not None:
-            if DEVICE_TYPE_RE.fullmatch(string) is None:
-                raise ValidationError(f"'{string}' is an invalid device type name")
+        if string is not None and DEVICE_TYPE_RE.fullmatch(string) is None:
+            raise ValidationError(f"'{string}' is an invalid device type name")
         return string
 
     def __str__(self):
diff --git a/app/network/views.py b/app/network/views.py
index 38b5e31..d36e2b7 100644
--- a/app/network/views.py
+++ b/app/network/views.py
@@ -193,14 +193,13 @@ def view_host(name):
                 task = utils.trigger_set_network_boot_profile(
                     host, boot_profile=boot_profile
                 )
-                if boot_profile != "localboot":
-                    # For localboot, there is no need to update the variable
-                    # csentry_autoinstall_boot_profile is used for DHCP options
-                    if utils.update_ansible_vars(
-                        host, {"csentry_autoinstall_boot_profile": boot_profile}
-                    ):
-                        # If a change occured, force DHCP update
-                        utils.trigger_core_services_update()
+                # For localboot, there is no need to update the variable
+                # csentry_autoinstall_boot_profile is used for DHCP options
+                if boot_profile != "localboot" and utils.update_ansible_vars(
+                    host, {"csentry_autoinstall_boot_profile": boot_profile}
+                ):
+                    # If a change occured, force DHCP update
+                    utils.trigger_core_services_update()
                 db.session.commit()
                 current_app.logger.info(
                     f"Set network boot profile to {boot_profile} for {name} requested: task {task.id}"
diff --git a/app/utils.py b/app/utils.py
index f109ce2..7a7c17b 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -555,11 +555,10 @@ def validate_ip(ip, network):
         is_admin = current_user.is_admin
     except AttributeError:
         is_admin = False
-    if not is_admin:
-        if addr < network.first or addr > network.last:
-            raise ValidationError(
-                f"IP address {ip} is not in range {network.first} - {network.last}"
-            )
+    if (not is_admin) and (addr < network.first or addr > network.last):
+        raise ValidationError(
+            f"IP address {ip} is not in range {network.first} - {network.last}"
+        )
 
 
 def overlaps(subnet, subnets):
-- 
GitLab