Skip to content

Commit 8e6215c

Browse files
authored
Update IntegrityError catch (#18906)
* Catch a UniqueViolation instead * Update stale comment * Update translations
1 parent ed1fd3e commit 8e6215c

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

tests/unit/accounts/test_views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pretend
99
import pytest
1010

11+
from psycopg.errors import UniqueViolation
1112
from pyramid.httpexceptions import (
1213
HTTPBadRequest,
1314
HTTPMovedPermanently,
@@ -16,7 +17,7 @@
1617
HTTPTooManyRequests,
1718
HTTPUnauthorized,
1819
)
19-
from sqlalchemy.exc import IntegrityError, NoResultFound
20+
from sqlalchemy.exc import NoResultFound
2021
from webauthn.authentication.verify_authentication_response import (
2122
VerifiedAuthentication,
2223
)
@@ -4569,10 +4570,10 @@ def test_add_pending_oidc_publisher_already_exists(
45694570
)
45704571
]
45714572

4572-
def test_add_pending_oidc_publisher_integrityerror(self, monkeypatch, db_request):
4573+
def test_add_pending_oidc_publisher_uniqueviolation(self, monkeypatch, db_request):
45734574
db_request.user = UserFactory.create()
45744575
EmailFactory(user=db_request.user, verified=True, primary=True)
4575-
db_request.db.add = pretend.raiser(IntegrityError("foo", "bar", "baz"))
4576+
db_request.db.add = pretend.raiser(UniqueViolation("foo", "bar", "baz"))
45764577

45774578
db_request.registry = pretend.stub(
45784579
settings={

tests/unit/organizations/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def test_unique_constraint(self, db_session):
723723
created_by=admin_user,
724724
)
725725

726-
# Attempt to create duplicate - should raise IntegrityError
726+
# Attempt to create duplicate - should raise UniqueViolation
727727
with pytest.raises(psycopg.errors.UniqueViolation):
728728
DBOrganizationOIDCIssuerFactory.create(
729729
organization=organization,

warehouse/accounts/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytz
1010

1111
from more_itertools import first_true
12+
from psycopg.errors import UniqueViolation
1213
from pyramid.httpexceptions import (
1314
HTTPBadRequest,
1415
HTTPMovedPermanently,
@@ -21,7 +22,7 @@
2122
from pyramid.security import forget, remember
2223
from pyramid.view import view_config, view_defaults
2324
from sqlalchemy import and_, func, select
24-
from sqlalchemy.exc import IntegrityError, NoResultFound
25+
from sqlalchemy.exc import NoResultFound
2526
from webauthn.helpers import bytes_to_base64url
2627
from webob.multidict import MultiDict
2728

@@ -1772,7 +1773,7 @@ def _add_pending_oidc_publisher(
17721773
try:
17731774
self.request.db.add(pending_publisher)
17741775
self.request.db.flush() # To get the new ID
1775-
except IntegrityError:
1776+
except UniqueViolation:
17761777
# The user has probably double-posted and a new publisher was
17771778
# created after our check for duplicates ran. The success message
17781779
# is probably already in the flash queue, so just redirect to the

warehouse/locale/messages.pot

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -122,211 +122,211 @@ msgstr ""
122122
msgid "The username isn't valid. Try again."
123123
msgstr ""
124124

125-
#: warehouse/accounts/views.py:111
125+
#: warehouse/accounts/views.py:112
126126
#, python-brace-format
127127
msgid ""
128128
"There have been too many unsuccessful login attempts. You have been "
129129
"locked out for {}. Please try again later."
130130
msgstr ""
131131

132-
#: warehouse/accounts/views.py:132
132+
#: warehouse/accounts/views.py:133
133133
#, python-brace-format
134134
msgid ""
135135
"Too many emails have been added to this account without verifying them. "
136136
"Check your inbox and follow the verification links. (IP: ${ip})"
137137
msgstr ""
138138

139-
#: warehouse/accounts/views.py:144
139+
#: warehouse/accounts/views.py:145
140140
#, python-brace-format
141141
msgid ""
142142
"Too many password resets have been requested for this account without "
143143
"completing them. Check your inbox and follow the verification links. (IP:"
144144
" ${ip})"
145145
msgstr ""
146146

147-
#: warehouse/accounts/views.py:376 warehouse/accounts/views.py:440
148-
#: warehouse/accounts/views.py:442 warehouse/accounts/views.py:471
149-
#: warehouse/accounts/views.py:473 warehouse/accounts/views.py:589
147+
#: warehouse/accounts/views.py:377 warehouse/accounts/views.py:441
148+
#: warehouse/accounts/views.py:443 warehouse/accounts/views.py:472
149+
#: warehouse/accounts/views.py:474 warehouse/accounts/views.py:590
150150
msgid "Invalid or expired two factor login."
151151
msgstr ""
152152

153-
#: warehouse/accounts/views.py:434
153+
#: warehouse/accounts/views.py:435
154154
msgid "Already authenticated"
155155
msgstr ""
156156

157-
#: warehouse/accounts/views.py:508
157+
#: warehouse/accounts/views.py:509
158158
msgid "Successful WebAuthn assertion"
159159
msgstr ""
160160

161-
#: warehouse/accounts/views.py:616 warehouse/manage/views/__init__.py:851
161+
#: warehouse/accounts/views.py:617 warehouse/manage/views/__init__.py:851
162162
msgid "Recovery code accepted. The supplied code cannot be used again."
163163
msgstr ""
164164

165-
#: warehouse/accounts/views.py:708
165+
#: warehouse/accounts/views.py:709
166166
msgid ""
167167
"New user registration temporarily disabled. See https://pypi.org/help"
168168
"#admin-intervention for details."
169169
msgstr ""
170170

171-
#: warehouse/accounts/views.py:877
171+
#: warehouse/accounts/views.py:878
172172
msgid "Expired token: request a new password reset link"
173173
msgstr ""
174174

175-
#: warehouse/accounts/views.py:879
175+
#: warehouse/accounts/views.py:880
176176
msgid "Invalid token: request a new password reset link"
177177
msgstr ""
178178

179-
#: warehouse/accounts/views.py:881 warehouse/accounts/views.py:982
180-
#: warehouse/accounts/views.py:1088 warehouse/accounts/views.py:1257
179+
#: warehouse/accounts/views.py:882 warehouse/accounts/views.py:983
180+
#: warehouse/accounts/views.py:1089 warehouse/accounts/views.py:1258
181181
msgid "Invalid token: no token supplied"
182182
msgstr ""
183183

184-
#: warehouse/accounts/views.py:885
184+
#: warehouse/accounts/views.py:886
185185
msgid "Invalid token: not a password reset token"
186186
msgstr ""
187187

188-
#: warehouse/accounts/views.py:890
188+
#: warehouse/accounts/views.py:891
189189
msgid "Invalid token: user not found"
190190
msgstr ""
191191

192-
#: warehouse/accounts/views.py:901
192+
#: warehouse/accounts/views.py:902
193193
msgid "Invalid token: user has logged in since this token was requested"
194194
msgstr ""
195195

196-
#: warehouse/accounts/views.py:919
196+
#: warehouse/accounts/views.py:920
197197
msgid ""
198198
"Invalid token: password has already been changed since this token was "
199199
"requested"
200200
msgstr ""
201201

202-
#: warehouse/accounts/views.py:950
202+
#: warehouse/accounts/views.py:951
203203
msgid "You have reset your password"
204204
msgstr ""
205205

206-
#: warehouse/accounts/views.py:978
206+
#: warehouse/accounts/views.py:979
207207
msgid "Expired token: request a new email verification link"
208208
msgstr ""
209209

210-
#: warehouse/accounts/views.py:980
210+
#: warehouse/accounts/views.py:981
211211
msgid "Invalid token: request a new email verification link"
212212
msgstr ""
213213

214-
#: warehouse/accounts/views.py:986
214+
#: warehouse/accounts/views.py:987
215215
msgid "Invalid token: not an email verification token"
216216
msgstr ""
217217

218-
#: warehouse/accounts/views.py:995
218+
#: warehouse/accounts/views.py:996
219219
msgid "Email not found"
220220
msgstr ""
221221

222-
#: warehouse/accounts/views.py:998
222+
#: warehouse/accounts/views.py:999
223223
msgid "Email already verified"
224224
msgstr ""
225225

226-
#: warehouse/accounts/views.py:1018
226+
#: warehouse/accounts/views.py:1019
227227
msgid "You can now set this email as your primary address"
228228
msgstr ""
229229

230-
#: warehouse/accounts/views.py:1021
230+
#: warehouse/accounts/views.py:1022
231231
msgid "This is your primary address"
232232
msgstr ""
233233

234-
#: warehouse/accounts/views.py:1027
234+
#: warehouse/accounts/views.py:1028
235235
#, python-brace-format
236236
msgid "Email address ${email_address} verified. ${confirm_message}."
237237
msgstr ""
238238

239-
#: warehouse/accounts/views.py:1084
239+
#: warehouse/accounts/views.py:1085
240240
msgid "Expired token: request a new organization invitation"
241241
msgstr ""
242242

243-
#: warehouse/accounts/views.py:1086
243+
#: warehouse/accounts/views.py:1087
244244
msgid "Invalid token: request a new organization invitation"
245245
msgstr ""
246246

247-
#: warehouse/accounts/views.py:1092
247+
#: warehouse/accounts/views.py:1093
248248
msgid "Invalid token: not an organization invitation token"
249249
msgstr ""
250250

251-
#: warehouse/accounts/views.py:1096
251+
#: warehouse/accounts/views.py:1097
252252
msgid "Organization invitation is not valid."
253253
msgstr ""
254254

255-
#: warehouse/accounts/views.py:1105
255+
#: warehouse/accounts/views.py:1106
256256
msgid "Organization invitation no longer exists."
257257
msgstr ""
258258

259-
#: warehouse/accounts/views.py:1157
259+
#: warehouse/accounts/views.py:1158
260260
#, python-brace-format
261261
msgid "Invitation for '${organization_name}' is declined."
262262
msgstr ""
263263

264-
#: warehouse/accounts/views.py:1220
264+
#: warehouse/accounts/views.py:1221
265265
#, python-brace-format
266266
msgid "You are now ${role} of the '${organization_name}' organization."
267267
msgstr ""
268268

269-
#: warehouse/accounts/views.py:1253
269+
#: warehouse/accounts/views.py:1254
270270
msgid "Expired token: request a new project role invitation"
271271
msgstr ""
272272

273-
#: warehouse/accounts/views.py:1255
273+
#: warehouse/accounts/views.py:1256
274274
msgid "Invalid token: request a new project role invitation"
275275
msgstr ""
276276

277-
#: warehouse/accounts/views.py:1261
277+
#: warehouse/accounts/views.py:1262
278278
msgid "Invalid token: not a collaboration invitation token"
279279
msgstr ""
280280

281-
#: warehouse/accounts/views.py:1265
281+
#: warehouse/accounts/views.py:1266
282282
msgid "Role invitation is not valid."
283283
msgstr ""
284284

285-
#: warehouse/accounts/views.py:1272
285+
#: warehouse/accounts/views.py:1273
286286
msgid "Invalid token: project does not exist"
287287
msgstr ""
288288

289-
#: warehouse/accounts/views.py:1283
289+
#: warehouse/accounts/views.py:1284
290290
msgid "Role invitation no longer exists."
291291
msgstr ""
292292

293-
#: warehouse/accounts/views.py:1315
293+
#: warehouse/accounts/views.py:1316
294294
#, python-brace-format
295295
msgid "Invitation for '${project_name}' is declined."
296296
msgstr ""
297297

298-
#: warehouse/accounts/views.py:1381
298+
#: warehouse/accounts/views.py:1382
299299
#, python-brace-format
300300
msgid "You are now ${role} of the '${project_name}' project."
301301
msgstr ""
302302

303-
#: warehouse/accounts/views.py:1461
303+
#: warehouse/accounts/views.py:1462
304304
#, python-brace-format
305305
msgid "Please review our updated <a href=\"${tos_url}\">Terms of Service</a>."
306306
msgstr ""
307307

308-
#: warehouse/accounts/views.py:1673 warehouse/accounts/views.py:1927
308+
#: warehouse/accounts/views.py:1674 warehouse/accounts/views.py:1928
309309
#: warehouse/manage/views/oidc_publishers.py:121
310310
msgid ""
311311
"Trusted publishing is temporarily disabled. See https://pypi.org/help"
312312
"#admin-intervention for details."
313313
msgstr ""
314314

315-
#: warehouse/accounts/views.py:1694
315+
#: warehouse/accounts/views.py:1695
316316
msgid "disabled. See https://pypi.org/help#admin-intervention for details."
317317
msgstr ""
318318

319-
#: warehouse/accounts/views.py:1710
319+
#: warehouse/accounts/views.py:1711
320320
msgid ""
321321
"You must have a verified email in order to register a pending trusted "
322322
"publisher. See https://pypi.org/help#openid-connect for details."
323323
msgstr ""
324324

325-
#: warehouse/accounts/views.py:1723
325+
#: warehouse/accounts/views.py:1724
326326
msgid "You can't register more than 3 pending trusted publishers at once."
327327
msgstr ""
328328

329-
#: warehouse/accounts/views.py:1738
329+
#: warehouse/accounts/views.py:1739
330330
#: warehouse/manage/views/oidc_publishers.py:303
331331
#: warehouse/manage/views/oidc_publishers.py:418
332332
#: warehouse/manage/views/oidc_publishers.py:534
@@ -336,30 +336,30 @@ msgid ""
336336
"again later."
337337
msgstr ""
338338

339-
#: warehouse/accounts/views.py:1748
339+
#: warehouse/accounts/views.py:1749
340340
#: warehouse/manage/views/oidc_publishers.py:316
341341
#: warehouse/manage/views/oidc_publishers.py:431
342342
#: warehouse/manage/views/oidc_publishers.py:547
343343
#: warehouse/manage/views/oidc_publishers.py:659
344344
msgid "The trusted publisher could not be registered"
345345
msgstr ""
346346

347-
#: warehouse/accounts/views.py:1763
347+
#: warehouse/accounts/views.py:1764
348348
msgid ""
349349
"This trusted publisher has already been registered. Please contact PyPI's"
350350
" admins if this wasn't intentional."
351351
msgstr ""
352352

353-
#: warehouse/accounts/views.py:1797
353+
#: warehouse/accounts/views.py:1798
354354
msgid "Registered a new pending publisher to create "
355355
msgstr ""
356356

357-
#: warehouse/accounts/views.py:1940 warehouse/accounts/views.py:1953
358-
#: warehouse/accounts/views.py:1960
357+
#: warehouse/accounts/views.py:1941 warehouse/accounts/views.py:1954
358+
#: warehouse/accounts/views.py:1961
359359
msgid "Invalid publisher ID"
360360
msgstr ""
361361

362-
#: warehouse/accounts/views.py:1967
362+
#: warehouse/accounts/views.py:1968
363363
msgid "Removed trusted publisher for project "
364364
msgstr ""
365365

0 commit comments

Comments
 (0)