Add the posibility to change the username

This commit is contained in:
zelazna
2019-10-01 15:36:00 +02:00
parent 0c40e40dc3
commit 3764c33a3a
4 changed files with 37 additions and 4 deletions
+18 -1
View File
@@ -38,7 +38,8 @@ from flask import render_template, request, redirect, send_from_directory, make_
from flask_babel import gettext as _
from flask_login import login_user, logout_user, login_required, current_user
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.expression import text, func, true, false, not_, and_
from sqlalchemy.sql.expression import text, func, true, false, not_, and_, \
exists
from werkzeug.exceptions import default_exceptions
from werkzeug.datastructures import Headers
from werkzeug.security import generate_password_hash, check_password_hash
@@ -1252,6 +1253,22 @@ def profile():
return render_title_template("user_edit.html", content=current_user, downloads=downloads,
title=_(u"%(name)s's profile", name=current_user.nickname), page="me",
registered_oauth=oauth_check, oauth_status=oauth_status)
if "nickname" in to_save and to_save["nickname"] != current_user.nickname:
existing_nickname = ub.session.query(exists().where(
ub.User.nickname == to_save["nickname"])).scalar()
if not existing_nickname:
current_user.nickname = to_save["nickname"]
else:
flash(_(u"This username is already taken."), category="error")
return render_title_template("user_edit.html",
translations=translations,
languages=languages,
new_user=0, content=current_user,
downloads=downloads,
registered_oauth=oauth_check,
title=_(u"Edit User %(nick)s",
nick=current_user.nickname),
page="edituser")
current_user.email = to_save["email"]
if "show_random" in to_save and to_save["show_random"] == "on":
current_user.random_books = 1