[damned-lies] Avoid crashing with double team join submission



commit cb606f53377e41f0a747318b770f60473e9b6144
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Aug 23 11:31:48 2021 +0200

    Avoid crashing with double team join submission

 people/views.py | 5 +++--
 teams/tests.py  | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)
---
diff --git a/people/views.py b/people/views.py
index 52d08b7f..aeb4f46d 100644
--- a/people/views.py
+++ b/people/views.py
@@ -5,7 +5,7 @@ from django.conf.locale import LANG_INFO
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.forms import PasswordChangeForm
 from django.contrib import messages
-from django.db import IntegrityError
+from django.db import IntegrityError, transaction
 from django.http import HttpResponseRedirect
 from django.shortcuts import render, get_object_or_404
 from django.urls import reverse
@@ -82,7 +82,8 @@ def person_team_join(request):
             # Role default to 'translator'
             new_role = Role(team=team, person=person)
             try:
-                new_role.save()
+                with transaction.atomic():
+                    new_role.save()
                 messages.success(request, _("You have successfully joined the team ā€œ%sā€.") % 
team.get_description())
                 team.send_mail_to_coordinator(
                     subject=gettext_lazy("A new person joined your team"),
diff --git a/teams/tests.py b/teams/tests.py
index f95409a4..c1e6fc1e 100644
--- a/teams/tests.py
+++ b/teams/tests.py
@@ -162,6 +162,8 @@ class TeamTests(TeamsAndRolesMixin, TestCase):
         self.assertEqual(mail.outbox[0].recipients()[0], self.pcoo.email)
         # Mail should be sent in the target team's language (i.e. French here)
         self.assertIn("rejoindre", mail.outbox[0].body)
+        # Double submission should not crash
+        self.client.post(team_join_url, {'teams': [str(self.t.pk)]})
 
     def test_leave_team(self):
         Role.objects.create(team=self.t, person=self.pn, role='translator')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]