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.
This commit is contained in:
Sebastian Lohff 2020-01-20 22:52:23 +01:00 committed by root
parent 2b42f6cea6
commit 1f2d34c8b4
1 changed files with 8 additions and 2 deletions

View File

@ -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: