~homeworkprod/byceps

34d6524128c43f5c353515382f18a5c483fbf99c — Jochen Kupperschmidt 1 year, 4 months ago f967c41
Allow current user to change own user/screen name
M byceps/blueprints/user/current/forms.py => byceps/blueprints/user/current/forms.py +21 -0
@@ 6,12 6,33 @@ byceps.blueprints.user.current.forms
:License: Modified BSD, see LICENSE for details.
"""

from flask import g
from wtforms import DateField, StringField
from wtforms.fields.html5 import TelField
from wtforms.validators import InputRequired, Length, Optional

from ....services.user import screen_name_validator, service as user_service
from ....util.l10n import LocalizedForm

from ..creation.forms import ScreenNameValidator


class ChangeScreenNameForm(LocalizedForm):
    screen_name = StringField('Neuer Benutzername', [
        InputRequired(),
        Length(min=screen_name_validator.MIN_LENGTH,
               max=screen_name_validator.MAX_LENGTH),
        ScreenNameValidator(),
    ])

    @staticmethod
    def validate_screen_name(form, field):
        if g.current_user.screen_name == field.data:
            raise ValueError('Dies ist bereits der aktuelle Benutzername.')

        if user_service.is_screen_name_already_assigned(field.data):
            raise ValueError('Dieser Benutzername kann nicht verwendet werden.')


class DetailsForm(LocalizedForm):
    first_names = StringField('Vorname(n)', [InputRequired(), Length(min=2)])

M byceps/blueprints/user/current/templates/user/current/_account.html => byceps/blueprints/user/current/templates/user/current/_account.html +1 -1
@@ 6,7 6,7 @@
  <table class="index">
    <tr>
      <th>Benutzername</th>
      <td>{{ render_user_screen_name(user) }}</td>
      <td>{{ render_user_screen_name(user) }}<br><a class="button button--compact" href="{{ url_for('.change_screen_name_form') }}">{{ render_icon('edit') }} <span>ändern</span></a></td>
    </tr>
    <tr>
      <th>E-Mail-Adresse</th>

A byceps/blueprints/user/current/templates/user/current/change_screen_name_form.html => byceps/blueprints/user/current/templates/user/current/change_screen_name_form.html +19 -0
@@ 0,0 1,19 @@
{% extends 'layout/base_auto.html' %}
{% from 'macros/forms.html' import form_buttons, form_field, form_fieldset %}
{% from 'macros/icons.html' import render_icon %}
{% set current_page = 'user' %}
{% set title = 'Benutzerkonto umbenennen' %}

{% block body %}

  <h1>{{ title }}</h1>

  <form action="{{ url_for('.change_screen_name') }}" method="post">
    {% call form_fieldset() %}
      {{ form_field(form.screen_name, maxlength=24, autofocus='autofocus') }}
    {% endcall %}

    {{ form_buttons('Umbenennen', cancel_url=url_for('.view')) }}
  </form>

{%- endblock %}

M byceps/blueprints/user/current/views.py => byceps/blueprints/user/current/views.py +38 -1
@@ 23,7 23,7 @@ from ...authentication.decorators import login_required
from ..creation.views import _find_newsletter_list_for_brand
from .. import signals

from .forms import DetailsForm
from .forms import DetailsForm, ChangeScreenNameForm


blueprint = create_blueprint('user_current', __name__)


@@ 79,6 79,43 @@ def view_as_json():
    })


@blueprint.route('/me/screen_name')
@templated
def change_screen_name_form(erroneous_form=None):
    """Show a form to change the current user's screen name."""
    _get_current_user_or_404()

    form = erroneous_form if erroneous_form else ChangeScreenNameForm()

    return {
        'form': form,
    }


@blueprint.route('/me/screen_name', methods=['POST'])
def change_screen_name():
    """Change the current user's screen name."""
    current_user = _get_current_user_or_404()

    form = ChangeScreenNameForm(request.form)
    if not form.validate():
        return change_screen_name_form(form)

    old_screen_name = current_user.screen_name
    new_screen_name = form.screen_name.data.strip()
    initiator_id = current_user.id

    event = user_command_service.change_screen_name(
        current_user.id, new_screen_name, initiator_id
    )

    signals.screen_name_changed.send(None, event=event)

    flash_success(f'Dein Benutzername wurde zu "{new_screen_name}" geändert.')

    return redirect_to('.view')


@blueprint.route('/me/details')
@templated
def details_update_form(erroneous_form=None):