diff --git a/src/postorius/forms.py b/src/postorius/forms.py index 8da59c8..35425ae 100644 --- a/src/postorius/forms.py +++ b/src/postorius/forms.py @@ -828,6 +828,19 @@ return email +class ChangeDisplayNameForm(forms.Form): + """ + Change display name. + """ + + display_name = forms.CharField( + label=_('Display name'), + error_messages={ + 'required': _('Please enter a display name')}, + required=True + ) + + class ChangeSubscriptionForm(forms.Form): email = forms.ChoiceField() diff --git a/src/postorius/templates/postorius/user/profile.html b/src/postorius/templates/postorius/user/profile.html index 667f323..45f9c7a 100644 --- a/src/postorius/templates/postorius/user/profile.html +++ b/src/postorius/templates/postorius/user/profile.html @@ -36,6 +36,10 @@
+
+ {% render_form_horizontal name_form 2 5 'Change display name' %} + +

{% blocktrans %} You can add other addresses to your profile, diff --git a/src/postorius/views/user.py b/src/postorius/views/user.py index d5f3513..c76668c 100644 --- a/src/postorius/views/user.py +++ b/src/postorius/views/user.py @@ -39,7 +39,7 @@ AddressConfirmationProfile, MailmanUser, Mailman404Error) from postorius.forms import (UserPreferences, AddressActivationForm, - ChangeSubscriptionForm) + ChangeSubscriptionForm, ChangeDisplayNameForm) from postorius.views.generic import MailmanUserView from smtplib import SMTPException from socket import error as socket_error @@ -247,29 +247,53 @@ except MailmanApiError: return utils.render_api_error(request) if request.method == 'POST': - form = AddressActivationForm(request.POST) - if form.is_valid(): - profile, c = AddressConfirmationProfile.objects.update_or_create( - email=form.cleaned_data['email'], user=request.user, - defaults={'activation_key': uuid.uuid4().hex}) - try: - profile.send_confirmation_link(request) - messages.success(request, - _('Please follow the instructions sent via' - ' email to confirm the address')) + if request.POST.get('formname') == 'displayname': + display_name_form = ChangeDisplayNameForm(request.POST) + form = AddressActivationForm( + initial={'user_email': request.user.email}) + if display_name_form.is_valid(): + name = display_name_form.cleaned_data['display_name'] + try: + mm_user.display_name = name + mm_user.save() + except MailmanApiError: + return utils.render_api_error(request) + except HTTPError as e: + messages.error(request, e) + else: + messages.success(request, _('Display name changed')) return redirect('user_profile') - except (SMTPException, socket_error) as e: - if (not isinstance(e, SMTPException) and - e.errno != errno.ECONNREFUSED): - raise e - profile.delete() - messages.error(request, _('Currently emails can not be added,' - ' please try again later')) + else: + display_name_form = ChangeDisplayNameForm( + initial={'display_name': mm_user.display_name}) + form = AddressActivationForm(request.POST) + if form.is_valid(): + profile, c = ( + AddressConfirmationProfile.objects.update_or_create( + email=form.cleaned_data['email'], user=request.user, + defaults={'activation_key': uuid.uuid4().hex})) + try: + profile.send_confirmation_link(request) + messages.success(request, _( + 'Please follow the instructions sent via' + ' email to confirm the address')) + return redirect('user_profile') + except (SMTPException, socket_error) as e: + if (not isinstance(e, SMTPException) and + e.errno != errno.ECONNREFUSED): + raise e + profile.delete() + messages.error(request, + _('Currently emails can not be added,' + ' please try again later')) else: form = AddressActivationForm( initial={'user_email': request.user.email}) + display_name_form = ChangeDisplayNameForm( + initial={'display_name': mm_user.display_name}) return render(request, 'postorius/user/profile.html', - {'mm_user': mm_user, 'form': form}) + {'mm_user': mm_user, 'form': form, + 'name_form': display_name_form}) @login_required()