check that id is int pk before str username/email
@@ -113,24 +113,29 @@ def user_from_id(user, raise404=True):
if isinstance(user, User):
return user
+ tgt = None
+ if user.isnumeric():
try:
- tgt = User.objects.get(username__iexact=user)
+ tgt = User.objects.get(pk=int(user))
+ except ValueError:
+ pass
except ObjectDoesNotExist:
+ pass
+ else:
try:
- tgt = User.objects.get(email__iexact=user)
+ tgt = User.objects.get(username__iexact=user)
except ObjectDoesNotExist:
try:
- tgt = User.objects.get(pk=int(user))
- except ValueError:
- if raise404:
- raise Http404('User not found')
- else:
- raise User.DoesNotExist
+ tgt = User.objects.get(email__iexact=user)
except ObjectDoesNotExist:
+ pass
+
+ if tgt is None:
if raise404:
raise Http404('User not found')
else:
raise
+
return tgt
