[Patchew-devel] [PATCH] rest: ignore IntegrityErrors on message import

Paolo Bonzini posted 1 patch 2 years, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20220227061327.37066-1-pbonzini@redhat.com
api/rest.py        | 13 ++++++++-----
tests/test_rest.py |  2 +-
2 files changed, 9 insertions(+), 6 deletions(-)
[Patchew-devel] [PATCH] rest: ignore IntegrityErrors on message import
Posted by Paolo Bonzini 2 years, 1 month ago
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/rest.py        | 13 ++++++++-----
 tests/test_rest.py |  2 +-
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/api/rest.py b/api/rest.py
index 43ec79d..8117ddc 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -690,14 +690,17 @@ class MessagesViewSet(BaseMessageViewSet):
             projects = (p for p in projects if p.maintained_by(self.request.user))
         results = []
         for project in projects:
-            serializer = self.get_serializer(data=request.data)
-            serializer.is_valid(raise_exception=True)
-            serializer.save(project=project)
-            results.append(serializer.data)
+            try:
+                serializer = self.get_serializer(data=request.data)
+                serializer.is_valid(raise_exception=True)
+                serializer.save(project=project)
+                results.append(serializer.data)
+            except django.db.utils.IntegrityError:
+                pass
         # Fake paginator response.  Note that there is no Location header.
         return Response(
             OrderedDict([("count", len(results)), ("results", results)]),
-            status=status.HTTP_201_CREATED,
+            status=status.HTTP_201_CREATED if results else status.HTTP_200_OK,
         )
 
 
diff --git a/tests/test_rest.py b/tests/test_rest.py
index aaa9dd0..9d96fa4 100755
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -638,7 +638,7 @@ class RestTest(PatchewTestCase):
         resp = self.api_client.post(
             self.REST_BASE + "messages/", data, content_type="message/rfc822"
         )
-        self.assertEqual(resp.status_code, 201)
+        self.assertEqual(resp.status_code, 200)
         self.assertEqual(resp.data["count"], 0)
         resp_get = self.api_client.get(
             self.PROJECT_BASE
-- 
2.34.1

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel