def update_patient():
    if request.method == 'GET':
        return render_template("update_user_info.html")
    if request.method == 'POST':
        new_username = request.form.get("new_username")
        new_password = request.form.get("new_password")
        current_user.set_username(new_username)
        current_user.set_password(new_password)
        return render_template("my_profile.html")
Beispiel #2
0
def edit_profile():
    form = EditProfileForm(current_user.display_name)
    if form.validate_on_submit():
        current_user.display_name = form.username.data
        current_user.about_me = form.about_me.data
        current_user.set_username()
        db.session.commit()
        flash('Changes have been saved.')
        return redirect(url_for('edit_profile'))
    elif request.method == 'GET':
        form.username.data = current_user.display_name
        form.about_me.data = current_user.about_me
    return render_template('edit_profile.html',
                           title='Edit Profile',
                           form=form)
Beispiel #3
0
def update(_username, _email, _bio, _password):
    """
	Used to update user data
	Authenticates user password given
	Updates if:
	1. Password authenticates
	2. Username given is not in use
	3. Email given is not in use
	4. Information given is different than previous value
	"""
    try:
        current_user.check_password(current_user.username, _password)
    except Exception as e:
        flash(u"Incorrect password")
        return redirect(url_for("routes.update_user"))
    try:
        updated_username = False
        if not current_user.username == _username:
            username = User.query.filter_by(username=_username).first()
            if username is not None:
                flash(u'Username: {username} already in use'.format(
                    username=_username))
                return redirect(url_for('routes.update_user'))
            posts = Post.query.filter_by(author=current_user.id).all()
            for post in posts:
                try:
                    post.set_author(current_user.id, _username)
                except Exception as e:
                    logging.error(f"{e}")
                    return ErrorController.error(e)
            current_user.set_username(_username)
            updated_username = True
        updated_email = False
        if not current_user.email == _email:
            email = User.query.filter_by(email=_email).first()
            if email is not None:
                flash(u'Email: {email} already in use'.format(email=_email))
                return redirect(url_for("routes.update_user"))
            try:
                current_user.set_email(_email)
                updated_email = True
            except Exception as e:
                logging.error(f"{e}")
                return ErrorController.error(e)
        updated_bio = False
        if not current_user.bio == _bio:
            try:
                current_user.set_bio(_bio)
                updated_bio = True
            except Exception as e:
                logging.error(f"{e}")
                return ErrorController.error(e)
        try:
            db.session.commit()
        except Exception as e:
            logging.error(f"{e}")
            return ErrorController.error(e)
    except Exception as e:
        logging.error(f"{e}")
        return ErrorController.error(e)
    print(f"User: {_username} [updated]")
    flash(u"Profile updated")
    file = f"{api_directory}/user/{current_user.id}.json"
    if os.path.exists(
            file
    ) and updated_username is True or updated_email is True or updated_bio is True:
        with open(file, "rt") as fp:
            data = json.load(fp)
        if updated_username is True:
            data["username"] = current_user.username
        if updated_email is True:
            data["email"] = current_user.email
        if updated_bio is True:
            data["bio"] = current_user.bio
        with open(file, "wt") as json_file:
            json.dump(data, json_file)
    return redirect(url_for('routes.show_user',
                            username=current_user.username))
Beispiel #4
0
def users_edit(user_id):
    form = UsernameForm()
    if form.validate_on_submit():
        current_user.set_username(form.username.data)
        return redirect(url_for("users_view", user_id=user_id))
    return render_template("users/edit.html", form=form)