Skip to content

Commit d04f16d

Browse files
committed
Change error message
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 06cbbb6 commit d04f16d

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

vulnerabilities/forms.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,24 @@ class ApiUserCreationForm(forms.ModelForm):
4141
class Meta:
4242
model = ApiUser
4343
fields = (
44-
"username",
44+
"email",
4545
"first_name",
4646
"last_name",
4747
)
48-
48+
4949
def __init__(self, *args, **kwargs):
5050
super(ApiUserCreationForm, self).__init__(*args, **kwargs)
51-
self.fields["username"].help_text = f"<ul><li>{self.fields['username'].help_text}</li></ul>"
52-
self.fields["username"].label = "Email"
51+
self.fields["email"].required = True
5352

5453
def save(self, commit=True):
5554
return ApiUser.objects.create_api_user(
56-
username=self.cleaned_data["username"],
55+
username=self.cleaned_data["email"],
5756
first_name=self.cleaned_data["first_name"],
5857
last_name=self.cleaned_data["last_name"],
5958
)
6059

6160
def clean_username(self):
62-
username = self.cleaned_data["username"]
61+
username = self.cleaned_data["email"]
6362
validate_email(username)
6463
return username
6564

vulnerabilities/views.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
#
99

1010
from django.contrib import messages
11+
from django.core.exceptions import ValidationError
1112
from django.core.mail import send_mail
1213
from django.db.models import Count
1314
from django.db.models import Q
1415
from django.http.response import Http404
16+
from django.shortcuts import redirect
1517
from django.shortcuts import render
1618
from django.urls import reverse_lazy
1719
from django.views import View
@@ -235,7 +237,12 @@ class ApiUserCreateView(generic.CreateView):
235237
template_name = "api_user_creation_form.html"
236238

237239
def form_valid(self, form):
238-
response = super().form_valid(form)
240+
241+
try:
242+
response = super().form_valid(form)
243+
except ValidationError as e:
244+
messages.error(self.request, "Email is invalid or already taken")
245+
return redirect(self.get_success_url())
239246

240247
send_mail(
241248
subject="VulnerableCode.io API key token",
@@ -251,12 +258,5 @@ def form_valid(self, form):
251258

252259
return response
253260

254-
def form_invalid(self, form):
255-
response = super().form_invalid(form)
256-
257-
messages.error(self.request, str(form.errors))
258-
259-
return response
260-
261261
def get_success_url(self):
262262
return reverse_lazy("api_user_request")

0 commit comments

Comments
 (0)