Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix failure on preferences page when email validation fails
bigjimmy33336fa7306be09
diff --git a/wger/core/forms.py b/wger/core/forms.pyindex a06f52ddc..f5237f13b 100644--- a/wger/core/forms.py+++ b/wger/core/forms.py@@ -171,6 +171,10 @@ class UserPreferencesForm(forms.ModelForm):self.helper = FormHelper()self.helper.form_class = 'wger-form'+ # Disable browser-side HTML5 validation so invalid emails and empty+ # required fields submit through to Django, which surfaces a proper+ # error message inline instead of the browser's own popover.+ self.helper.attrs = {'novalidate': True}self.helper.layout = Layout(Fieldset(_('Personal data'),diff --git a/wger/core/templates/base.html b/wger/core/templates/base.htmlindex 6ecec451e..60ad90a8e 100644--- a/wger/core/templates/base.html+++ b/wger/core/templates/base.html@@ -45,7 +45,7 @@{% if messages and not no_messages %}{% for message in messages %}<div- class="alert {% if message.tags == 'warning' %}alert-error{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}">+ class="alert {% if message.tags == 'error' %}alert-danger{% elif message.tags == 'warning' %}alert-warning{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}"><a href="#" class="close extra-bold" data-bs-dismiss="alert">×</a>{{ message }}</div>diff --git a/wger/core/templates/base_wide.html b/wger/core/templates/base_wide.htmlindex 30e6595d0..7de7d2ebd 100644--- a/wger/core/templates/base_wide.html+++ b/wger/core/templates/base_wide.html@@ -39,7 +39,7 @@{% if messages and not no_messages %}{% for message in messages %}<div- class="alert {% if message.tags == 'warning' %}alert-error{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}">+ class="alert {% if message.tags == 'error' %}alert-danger{% elif message.tags == 'warning' %}alert-warning{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}"><a href="#" class="close extra-bold" data-bs-dismiss="alert">×</a>{{ message }}</div>diff --git a/wger/core/views/user.py b/wger/core/views/user.pyindex e46fc37df..ff4056ba7 100644--- a/wger/core/views/user.py+++ b/wger/core/views/user.py@@ -320,59 +320,62 @@ def preferences(request):"""context = {}context.update(csrf(request))- redirect = Falseemail_verified = request.user.userprofile.is_verified- # Process the preferences formif request.method == 'POST':form = UserPreferencesForm(data=request.POST, instance=request.user.userprofile)form.user = request.user-- # Save the data if it validates- if form.is_valid():- form.save()- redirect = True- else:- data = {- 'first_name': request.user.first_name,- 'last_name': request.user.last_name,- 'email': request.user.email,- }-- form = UserPreferencesForm(initial=data, instance=request.user.userprofile)-- # Process the email form- if request.method == 'POST':- user_email = request.user.emailemail_form = UserPersonalInformationForm(data=request.POST, instance=request.user)- if email_form.is_valid() and redirect:+ # Validate both forms before saving anything, so a single bad field+ # (e.g. a duplicate email) never half-persists the submission.+ prefs_valid = form.is_valid()+ email_valid = email_form.is_valid()++ if prefs_valid and email_valid:# If the user changes the email, it is no longer verified- if user_email != email_form.instance.email:+ if request.user.email != email_form.cleaned_data.get('email'):logger.debug('adding email with verified flag and also, sends email confirmation')EmailAddress.objects.add_email(request,request.user,- request.user.email,+ email_form.cleaned_data['email'],confirm=True,)- request.user.userprofile.save()- # Save as normalemail_form.save()- redirect = True- else:- redirect = False+ form.save()++ messages.success(request, _('Settings successfully updated'))+ return HttpResponseRedirect(reverse('core:user:preferences'))++ # The template only renders `form`, so copy any errors from+ # `email_form` onto the rendered form so they show up inline next+ # to the offending field. Skip duplicates that already exist on+ # `form` (the email field's format validator runs on both forms).+ for field, errors in email_form.errors.items():+ target = field if field in form.fields else None+ existing = set(form.errors.get(target or '__all__', []))+ for err in errors:+ if err in existing:+ continue+ form.add_error(target, err)+ existing.add(err)++ messages.error(request, _('Please correct the errors below.'))+ else:+ data = {+ 'first_name': request.user.first_name,+ 'last_name': request.user.last_name,+ 'email': request.user.email,+ }+ form = UserPreferencesForm(initial=data, instance=request.user.userprofile)context['form'] = formcontext['email_verified'] = email_verified- if redirect:- messages.success(request, _('Settings successfully updated'))- return HttpResponseRedirect(reverse('core:user:preferences'))- else:- return render(request, 'user/preferences.html', context)+ return render(request, 'user/preferences.html', context)class UserDeactivateView(