Add the posibility to change the username

pull/1052/head
zelazna 5 years ago
parent 0c40e40dc3
commit 3764c33a3a

@ -35,7 +35,7 @@ from flask_login import login_required, current_user, logout_user
from flask_babel import gettext as _ from flask_babel import gettext as _
from sqlalchemy import and_ from sqlalchemy import and_
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.expression import func from sqlalchemy.sql.expression import func, exists
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
from . import constants, logger, helper, services from . import constants, logger, helper, services
@ -563,7 +563,6 @@ def edit_user(user_id):
else: else:
if "password" in to_save and to_save["password"]: if "password" in to_save and to_save["password"]:
content.password = generate_password_hash(to_save["password"]) content.password = generate_password_hash(to_save["password"])
anonymous = content.is_anonymous anonymous = content.is_anonymous
content.role = constants.selected_roles(to_save) content.role = constants.selected_roles(to_save)
if anonymous: if anonymous:
@ -601,6 +600,22 @@ def edit_user(user_id):
return render_title_template("user_edit.html", translations=translations, languages=languages, return render_title_template("user_edit.html", translations=translations, languages=languages,
new_user=0, content=content, downloads=downloads, registered_oauth=oauth_check, new_user=0, content=content, downloads=downloads, registered_oauth=oauth_check,
title=_(u"Edit User %(nick)s", nick=content.nickname), page="edituser") title=_(u"Edit User %(nick)s", nick=content.nickname), page="edituser")
if "nickname" in to_save and to_save["nickname"] != content.nickname:
existing_nickname = ub.session.query(exists().where(
ub.User.nickname == to_save["nickname"])).scalar()
if not existing_nickname:
content.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=content,
downloads=downloads,
registered_oauth=oauth_check,
title=_(u"Edit User %(nick)s",
nick=content.nickname),
page="edituser")
if "kindle_mail" in to_save and to_save["kindle_mail"] != content.kindle_mail: if "kindle_mail" in to_save and to_save["kindle_mail"] != content.kindle_mail:
content.kindle_mail = to_save["kindle_mail"] content.kindle_mail = to_save["kindle_mail"]

@ -3,7 +3,7 @@
<div class="discover"> <div class="discover">
<h1>{{title}}</h1> <h1>{{title}}</h1>
<form role="form" method="POST" autocomplete="off"> <form role="form" method="POST" autocomplete="off">
{% if g.user and g.user.role_admin() and new_user %} {% if g.user or g.user.role_admin() or new_user %}
<div class="form-group required"> <div class="form-group required">
<label for="nickname">{{_('Username')}}</label> <label for="nickname">{{_('Username')}}</label>
<input type="text" class="form-control" name="nickname" id="nickname" value="{{ content.nickname if content.nickname != None }}" autocomplete="off"> <input type="text" class="form-control" name="nickname" id="nickname" value="{{ content.nickname if content.nickname != None }}" autocomplete="off">

@ -27,6 +27,7 @@ from flask import g
from flask_babel import gettext as _ from flask_babel import gettext as _
from flask_login import AnonymousUserMixin from flask_login import AnonymousUserMixin
from werkzeug.local import LocalProxy from werkzeug.local import LocalProxy
try: try:
from flask_dance.consumer.backend.sqla import OAuthConsumerMixin from flask_dance.consumer.backend.sqla import OAuthConsumerMixin
oauth_support = True oauth_support = True

@ -38,7 +38,8 @@ from flask import render_template, request, redirect, send_from_directory, make_
from flask_babel import gettext as _ from flask_babel import gettext as _
from flask_login import login_user, logout_user, login_required, current_user from flask_login import login_user, logout_user, login_required, current_user
from sqlalchemy.exc import IntegrityError 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.exceptions import default_exceptions
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
from werkzeug.security import generate_password_hash, check_password_hash 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, return render_title_template("user_edit.html", content=current_user, downloads=downloads,
title=_(u"%(name)s's profile", name=current_user.nickname), page="me", title=_(u"%(name)s's profile", name=current_user.nickname), page="me",
registered_oauth=oauth_check, oauth_status=oauth_status) 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"] current_user.email = to_save["email"]
if "show_random" in to_save and to_save["show_random"] == "on": if "show_random" in to_save and to_save["show_random"] == "on":
current_user.random_books = 1 current_user.random_books = 1

Loading…
Cancel
Save