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)
def add_user() -> Union[str, werkzeug.Response]: form = NewUserForm() if form.validate_on_submit(): form_valid = True username = request.form['username'] first_name = request.form['first_name'] last_name = request.form['last_name'] password = request.form['password'] is_admin = bool(request.form.get('is_admin')) try: otp_secret = None if request.form.get('is_hotp', False): otp_secret = request.form.get('otp_secret', '') new_user = Journalist(username=username, password=password, first_name=first_name, last_name=last_name, is_admin=is_admin, otp_secret=otp_secret) db.session.add(new_user) db.session.commit() except PasswordError: flash( gettext( 'There was an error with the autogenerated password. ' 'User not created. Please try again.'), 'error') form_valid = False except InvalidUsernameException as e: form_valid = False flash('Invalid username: '******'Username "{user}" already taken.'.format( user=username)), "error") else: flash( gettext("An error occurred saving this user" " to the database." " Please inform your admin."), "error") current_app.logger.error("Adding user " "'{}' failed: {}".format( username, e)) if form_valid: return redirect( url_for('admin.new_user_two_factor', uid=new_user.id)) return render_template("admin_add_user.html", password=make_password(config), form=form)
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 = make_password(config) return render_template("edit_account.html", user=user, password=password)
def add_user(): form = NewUserForm() if form.validate_on_submit(): form_valid = True username = request.form['username'] password = request.form['password'] is_admin = bool(request.form.get('is_admin')) try: otp_secret = None if request.form.get('is_hotp', False): otp_secret = request.form.get('otp_secret', '') new_user = Journalist(username=username, password=password, is_admin=is_admin, otp_secret=otp_secret) db_session.add(new_user) db_session.commit() except PasswordError: flash(gettext( 'There was an error with the autogenerated password. ' 'User not created. Please try again.'), 'error') form_valid = False except InvalidUsernameException as e: form_valid = False flash('Invalid username: '******'{}' failed: {}".format( username, e)) if form_valid: return redirect(url_for('admin.new_user_two_factor', uid=new_user.id)) return render_template("admin_add_user.html", password=make_password(config), form=form)
def edit() -> str: password = make_password(config) return render_template('edit_account.html', password=password)
def edit(): password = make_password() return render_template('edit_account.html', password=password)
def edit(): password = make_password(config) return render_template('edit_account.html', password=password)