From 1f2d34c8b47432b90754b13d74776492c2f6c49a Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 20 Jan 2020 22:52:23 +0100 Subject: [PATCH] Don't error out when newRef already exist, use it instead Before this commit when a newRef was entered on user registration and that ref already existed we errored out. Now we use exactly that reference when it already exists. --- contest/forms.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/contest/forms.py b/contest/forms.py index 9fab138..e8a68fb 100644 --- a/contest/forms.py +++ b/contest/forms.py @@ -27,8 +27,6 @@ class UpdateRefForm(forms.Form): def clean_newRefName(self): data = self.cleaned_data["newRefName"].strip().upper() - if Reference.objects.filter(name=data).count() > 0: - raise forms.ValidationError("Reference already exists") return data @@ -38,6 +36,14 @@ class UpdateRefForm(forms.Form): existingRef = cleaned_data.get("existingRef") newRefName = cleaned_data.get("newRefName") + if newRefName: + try: + ref = Reference.objects.get(name=newRefName) + self.cleaned_data['newRefName'] = None + self.cleaned_data['existingRef'] = ref + except Reference.DoesNotExist: + pass + if existingRef and newRefName: raise forms.ValidationError("Select an existing exchange or create a new one, not both!") if not existingRef and not newRefName: