示例#1
0
    def edit_user(user_id):
        user = Journalist.query.get(user_id)

        if request.method == 'POST':
            if request.form.get('username', None):
                new_username = request.form['username']

                try:
                    Journalist.check_username_acceptable(new_username)
                except InvalidUsernameException as e:
                    flash('Invalid username: '******'error')
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))

                if new_username == user.username:
                    pass
                elif Journalist.query.filter_by(
                        username=new_username).one_or_none():
                    flash(gettext(
                        'Username "{user}" already taken.').format(
                            user=new_username),
                        "error")
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))
                else:
                    user.username = new_username

            user.is_admin = bool(request.form.get('is_admin'))

            commit_account_changes(user)

        password = make_password(config)
        return render_template("edit_account.html", user=user,
                               password=password)
示例#2
0
    def edit_user(user_id: int) -> Union[str, werkzeug.Response]:
        user = Journalist.query.get(user_id)

        if request.method == "POST":
            if request.form.get("username", None):
                new_username = request.form["username"]

                try:
                    Journalist.check_username_acceptable(new_username)
                except InvalidUsernameException as e:
                    flash(
                        gettext("Invalid username: {message}").format(
                            message=e), "error")
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))

                if new_username == user.username:
                    pass
                elif Journalist.query.filter_by(
                        username=new_username).one_or_none():
                    flash(
                        gettext('Username "{username}" already taken.').format(
                            username=new_username),
                        "error",
                    )
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))
                else:
                    user.username = new_username

            try:
                first_name = request.form["first_name"]
                Journalist.check_name_acceptable(first_name)
                user.first_name = first_name
            except FirstOrLastNameError as e:
                # Translators: Here, "{message}" explains the problem with the name.
                flash(
                    gettext("Name not updated: {message}").format(message=e),
                    "error")
                return redirect(url_for("admin.edit_user", user_id=user_id))

            try:
                last_name = request.form["last_name"]
                Journalist.check_name_acceptable(last_name)
                user.last_name = last_name
            except FirstOrLastNameError as e:
                flash(
                    gettext("Name not updated: {message}").format(message=e),
                    "error")
                return redirect(url_for("admin.edit_user", user_id=user_id))

            user.is_admin = bool(request.form.get("is_admin"))

            commit_account_changes(user)

        password = PassphraseGenerator.get_default().generate_passphrase(
            preferred_language=g.localeinfo.language)
        return render_template("edit_account.html",
                               user=user,
                               password=password)
示例#3
0
    def edit_user(user_id: int) -> Union[str, werkzeug.Response]:
        user = Journalist.query.get(user_id)

        if request.method == 'POST':
            if request.form.get('username', None):
                new_username = request.form['username']

                try:
                    Journalist.check_username_acceptable(new_username)
                except InvalidUsernameException as e:
                    flash('Invalid username: '******'error')
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))

                if new_username == user.username:
                    pass
                elif Journalist.query.filter_by(
                        username=new_username).one_or_none():
                    flash(
                        gettext('Username "{user}" already taken.').format(
                            user=new_username), "error")
                    return redirect(url_for("admin.edit_user",
                                            user_id=user_id))
                else:
                    user.username = new_username

            try:
                first_name = request.form['first_name']
                Journalist.check_name_acceptable(first_name)
                user.first_name = first_name
            except FirstOrLastNameError as e:
                flash(gettext('Name not updated: {}'.format(e)), "error")
                return redirect(url_for("admin.edit_user", user_id=user_id))

            try:
                last_name = request.form['last_name']
                Journalist.check_name_acceptable(last_name)
                user.last_name = last_name
            except FirstOrLastNameError as e:
                flash(gettext('Name not updated: {}'.format(e)), "error")
                return redirect(url_for("admin.edit_user", user_id=user_id))

            user.is_admin = bool(request.form.get('is_admin'))

            commit_account_changes(user)

        password = PassphraseGenerator.get_default().generate_passphrase(
            preferred_language=g.localeinfo.language)
        return render_template("edit_account.html",
                               user=user,
                               password=password)