From e648d5e4ee38f80128f3c7b705d17f227e70b190 Mon Sep 17 00:00:00 2001
From: Yoann Schneider <yschneider@teklia.com>
Date: Fri, 12 Apr 2024 19:10:23 +0200
Subject: [PATCH] Comply with RET506

---
 arkindex/images/managers.py             | 2 +-
 arkindex/images/models.py               | 2 +-
 arkindex/process/api.py                 | 4 ++--
 arkindex/process/serializers/imports.py | 4 ++--
 arkindex/training/api.py                | 5 ++---
 ruff.toml                               | 2 ++
 6 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/arkindex/images/managers.py b/arkindex/images/managers.py
index d3d9769256..61ffb364bb 100644
--- a/arkindex/images/managers.py
+++ b/arkindex/images/managers.py
@@ -47,7 +47,7 @@ class ImageServerManager(models.Manager):
                 f'The URL "{url}" does not match any existing image server. '
                 "Please ask an instance administrator to register the IIIF server for this image."
             )
-        elif servers_count > 1:
+        if servers_count > 1:
             raise self.model.MultipleObjectsReturned(
                 f'The URL "{url}" matched multiple existing image servers'
             )
diff --git a/arkindex/images/models.py b/arkindex/images/models.py
index aecc25fca6..8b904acaf9 100644
--- a/arkindex/images/models.py
+++ b/arkindex/images/models.py
@@ -108,7 +108,7 @@ class ImageServer(models.Model):
         src_url, dest_url = map(urllib.parse.urlsplit, (self.url, other.url))
         if src_url.scheme != dest_url.scheme:
             raise ValueError("Cannot merge into an image server of a different protocol")
-        elif src_url.netloc != dest_url.netloc:
+        if src_url.netloc != dest_url.netloc:
             raise ValueError("Cannot merge into an image server on a different domain")
 
         # Check paths
diff --git a/arkindex/process/api.py b/arkindex/process/api.py
index f31c615adb..8d478b2aa9 100644
--- a/arkindex/process/api.py
+++ b/arkindex/process/api.py
@@ -427,9 +427,9 @@ class ProcessRetry(ProcessACLMixin, ProcessQuerysetMixin, GenericAPIView):
         # Allow 'retrying' a process that has no Ponos tasks (that has never been started)
         if len(process.tasks.all()) and state in (State.Unscheduled, State.Pending):
             raise ValidationError({"__all__": ["This process is already pending"]})
-        elif state == State.Running:
+        if state == State.Running:
             raise ValidationError({"__all__": ["This process is already running"]})
-        elif state == State.Stopping:
+        if state == State.Stopping:
             raise ValidationError({"__all__": ["This process is stopping"]})
 
     @extend_schema(
diff --git a/arkindex/process/serializers/imports.py b/arkindex/process/serializers/imports.py
index 2e01179db6..23e6017546 100644
--- a/arkindex/process/serializers/imports.py
+++ b/arkindex/process/serializers/imports.py
@@ -545,7 +545,7 @@ class ApplyProcessTemplateSerializer(ProcessACLMixin, serializers.Serializer):
         access_level = self.process_access_level(process)
         if not access_level:
             raise ValidationError(detail="Process with this ID does not exist.")
-        elif access_level < Role.Contributor.value:
+        if access_level < Role.Contributor.value:
             raise PermissionDenied(detail="You do not have a contributor access to this process.")
 
         errors = []
@@ -627,7 +627,7 @@ class CorpusProcessSerializer(serializers.Serializer):
         access_level = get_max_level(self.context["request"].user, corpus)
         if not access_level:
             raise ValidationError(["Corpus with this ID does not exist."])
-        elif access_level < Role.Admin.value:
+        if access_level < Role.Admin.value:
             raise ValidationError(["You do not have an admin access to this corpus."])
         return corpus
 
diff --git a/arkindex/training/api.py b/arkindex/training/api.py
index 66b6d00a37..c7d7220ae9 100644
--- a/arkindex/training/api.py
+++ b/arkindex/training/api.py
@@ -417,8 +417,7 @@ class ModelsList(TrainingModelMixin, ListCreateAPIView):
                     "id": str(existing_model.id),
                     "name": "A model with this name already exists",
                 })
-            else:
-                raise PermissionDenied()
+            raise PermissionDenied()
         return serializer.save()
 
 
@@ -753,7 +752,7 @@ class DatasetSetBase():
         )
         if self.request.method == "DELETE" and not Corpus.objects.admin(self.request.user).filter(pk=dataset.corpus_id).exists():
             raise PermissionDenied(detail="You do not have admin access to this dataset.")
-        elif self.request.method != "DELETE" and not Corpus.objects.writable(self.request.user).filter(pk=dataset.corpus_id).exists():
+        if self.request.method != "DELETE" and not Corpus.objects.writable(self.request.user).filter(pk=dataset.corpus_id).exists():
             raise PermissionDenied(detail="You do not have contributor access to this dataset.")
         if dataset.state != DatasetState.Open:
             raise ValidationError(detail="You can only add or update sets from a dataset in an open state.")
diff --git a/ruff.toml b/ruff.toml
index 5899ec4ebe..40fbb5b3b5 100644
--- a/ruff.toml
+++ b/ruff.toml
@@ -14,5 +14,7 @@ select = ["Q0", "F", "W", "E",
     "RET504",
     # flake8-return: superfluous-else-return
     "RET505",
+    # flake8-return: superfluous-else-raise
+    "RET506",
 ]
 ignore = ["E501"]
-- 
GitLab