コード例 #1
0
ファイル: app.py プロジェクト: Juli03b/warbler
def profile():
    """Update profile for current user."""

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        user = User.authenticate(g.user.username, form.password.data)

        if user:
            user.username = form.username.data
            user.data = form.email.data
            user.image_url = form.image_url.data
            user.header_image_url = form.header_image_url.data
            user.bio = form.bio.data
            db.session.commit()

            flash('You updated your profile', 'success')

            return redirect(f'/users/{g.user.id}')
        else:
            flash('Incorrect credentials', 'danger')

            return redirect(url_for('profile'))
    else:

        return render_template('/users/edit.html', form=form)

    return redirect('/')
コード例 #2
0
ファイル: app.py プロジェクト: stgibson/warbler
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)
    if form.validate_on_submit():
        # verify user typed in correct password
        if g.user.authenticate(g.user.username, form.password.data):
            g.user.username = form.username.data
            g.user.email = form.email.data
            if form.image_url.data:
                g.user.image_url = form.image_url.data
            if form.header_image_url.data:
                g.user.header_image_url = form.header_image_url.data
            if form.bio.data:
                g.user.bio = form.bio.data
            db.session.add(g.user)
            db.session.commit()
            return redirect(f"/users/{g.user.id}")

        flash("Invalid password.", "danger")
        return redirect("/")

    return render_template("users/edit.html", form=form, user_id=g.user.id)
コード例 #3
0
def profile():
    """Update profile for current user."""

    if not g.user:
        flash('You must login!')
        return redirect('/login')

    form = UserUpdateForm(obj=g.user)
    if form.validate_on_submit():
        username = form.username.data
        email = form.email.data
        image_url = form.image_url.data or DEFAULT_USER_IMG
        header_image_url = form.header_image_url.data
        bio = form.bio.data
        password = form.password.data

        if User.authenticate(username, password) == g.user:
            g.user.username = username
            g.user.email = email
            g.user.image_url = image_url
            g.user.header_image_url = header_image_url
            g.user.bio = bio

            db.session.commit()

            return redirect(f'/users/{g.user.id}')
        else:
            flash("Username and/or password is invalid.")
            return redirect('/')
    else:
        return render_template("users/edit.html", form=form, user_id=g.user.id)
コード例 #4
0
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized", "danger")
        return redirect("/")
    form = UserUpdateForm()

    if form.validate_on_submit():
        user = User.authenticate(g.user.username, form.password.data)

        if user:
            user.username = form.username.data
            user.email = form.email.data
            user.image_url = form.image_url.data
            user.header_image_url = form.header_image_url.data
            user.bio = form.bio.data

            db.session.add(user)
            db.session.commit()
            flash("Information updated", "success")
            return redirect(f'/users/{g.user.id}')
        else:
            flash("Incorrect password")
            return redirect('/')

    else:
        form.username.data = g.user.username
        form.email.data = g.user.email
        form.image_url.data = g.user.image_url
        form.header_image_url.data = g.user.header_image_url
        form.bio.data = g.user.bio
        return render_template('/users/edit.html', form=form)
コード例 #5
0
ファイル: app.py プロジェクト: mtoprak53/SECT_Study
def profile():
    """Update profile for current user."""

    # IMPLEMENT THIS
    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        user = User.authenticate(g.user.username, form.password.data)

        if user:
            user.username = form.username.data
            user.email = form.email.data
            user.image_url = form.image_url.data
            user.header_image_url = form.header_image_url.data
            user.bio = form.bio.data
            db.session.commit()

            flash(f"User {user.username} updated!", "success")
            return redirect(f"/users/{user.id}")

        flash("Invalid credentials.", 'danger')
        return redirect("/")

    return render_template("/users/edit.html", form=form)
コード例 #6
0
def update_user():
    """Updates a user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        user = User.authenticate_username(g.user.username, form.pwd.data)

        if user:
            if form.new_pwd.data:
                g.user.password = User.hash_password(form.new_pwd.data)

            g.user.username = form.username.data
            g.user.email = form.email.data
            g.user.darkmode = form.darkmode.data

            db.session.add(user)
            db.session.commit()
            return redirect(f"/users/{g.user.id}")

        flash(
            "We couldn't authenticate you with that password. " +
            "Please try again.",
            "danger",
        )
        return render_template("update-user.html", form=form)

    return render_template("update-user.html", form=form)
コード例 #7
0
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        # check if password submitted on form is user's correct password
        if not g.user.authenticate(g.user.username, form.password.data):

            flash("Invalid Password", "danger")
            return render_template('users/edit.html', user=g.user, form=form)

        g.user.username = form.username.data
        g.user.email = form.email.data
        g.user.image_url = form.image_url.data
        g.user.header_image_url = form.header_image_url.data
        g.user.bio = form.bio.data

        db.session.commit()
        return redirect(f'/users/{g.user.id}')

    else:
        return render_template('users/edit.html', user=g.user, form=form)
コード例 #8
0
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    # user = User.query.get(session[CURR_USER_KEY])

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        user = User.authenticate(g.user.username, form.password.data)
        if user:
            user.username = form.username.data
            user.email = form.email.data
            user.image_url = form.image_url.data
            user.header_image_url = form.header_image_url.data
            user.bio = form.bio.data
            user.location = form.location.data

            db.session.commit()
            return redirect(f'/users/{user.id}')
        else:
            flash('Wrong password!')
            return redirect('/')

    return render_template('/users/edit.html', form=form)
コード例 #9
0
ファイル: app.py プロジェクト: elizabethlatimer/warbler
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)

    if form.validate_on_submit():
        if User.authenticate(g.user.username, form.password.data):
            g.user.username = form.username.data
            g.user.email = form.email.data
            g.user.image_url = form.image_url.data
            g.user.header_image_url = form.header_image_url.data
            g.user.location = form.location.data
            g.user.bio = form.bio.data

            db.session.commit()
            flash("Successfully updated your profile!", "success")

            return redirect(f"/users/{g.user.id}")

        else:
            flash("Incorrect password.", "danger")

    return render_template("users/edit.html", form=form, user=g.user)
コード例 #10
0
def render_admin_update_profile():
    contact = current_user.contact
    admin = Users.query.filter_by(contact=contact).first()
    if admin:
        form = UserUpdateForm(obj=admin)
        if request.method == 'POST' and form.validate_on_submit():
            profile = Users.query.filter_by(contact=contact).first()
            profile.username = form.username.data
            profile.password = form.password.data
            db.session.commit()
            print("Admin profile has been updated", flush=True)
            return redirect(url_for('view.render_admin_profile'))
        return render_template("update.html",
                               form=form,
                               username=current_user.username + " admin")
コード例 #11
0
def render_caretaker_update_profile():
    contact = current_user.contact
    caretaker = Users.query.filter_by(contact=contact).first()
    if caretaker:
        form = UserUpdateForm(obj=caretaker)
        if request.method == 'POST' and form.validate_on_submit():
            profile = Users.query.filter_by(contact=contact).first()
            profile.username = form.username.data
            profile.password = form.password.data
            profile.isparttime = form.is_part_time.data
            profile.postalcode = form.postal_code.data
            db.session.commit()
            print("Caretaker profile has been updated", flush=True)
            return redirect(url_for('view.render_caretaker_profile'))
        return render_template("update.html",
                               form=form,
                               username=current_user.username + " caretaker")
コード例 #12
0
def render_owner_profile_update():
    contact = current_user.contact
    petowner = Users.query.filter_by(contact=contact).first()
    if petowner:
        form = UserUpdateForm(obj=petowner)
        if request.method == 'POST' and form.validate_on_submit():
            profile = Users.query.filter_by(contact=contact).first()
            profile.username = form.username.data
            profile.password = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            profile.card = form.credit_card.data
            profile.postalcode = form.postal_code.data
            db.session.commit()
            print("Owner profile has been updated", flush=True)
            return redirect(url_for('view.render_owner_profile'))
        return render_template("update.html",
                               form=form,
                               username=current_user.username + " owner")
コード例 #13
0
ファイル: views.py プロジェクト: x-sangeeth/FlaskCRUDdemo
def viewprofile():
    """
    Handle requests to the /register route
    Add an notetaker to the database through the registration form
    """
    user = current_user
    form = UserUpdateForm(obj=user)
    form.populate_obj(user)
    if form.validate_on_submit():

        form.populate_obj(user)

        db.session.commit()

        flash('You have successfully edited your profile!')
    return render_template('user/user.html',
                           title="View Profile",
                           user=user,
                           form=form,
                           action='Edit')
コード例 #14
0
def render_owner_profile_update():
    contact = current_user.contact
    userQuery = "SELECT * FROM users WHERE contact = '{}';".format(contact)
    petowner = db.session.execute(userQuery).fetchall()
    if petowner:
        form = UserUpdateForm(obj=petowner)
        if request.method == 'POST' and form.validate_on_submit():
            update = """UPDATE users
                    SET username = '******', password = '******', card = '{}', postalcode = '{}'
                    WHERE contact = '{}';""".format(
                form.username.data,
                bcrypt.generate_password_hash(
                    form.password.data).decode('utf-8'), form.credit_card.data,
                form.postal_code.data, contact)
            db.session.execute(update)
            db.session.commit()
            return redirect(url_for('view.render_owner_profile'))
        return render_template("update.html",
                               form=form,
                               username=current_user.username)
コード例 #15
0
def update_profile(user_id):
    """Update profile for current user."""
    curr_user = User.query.get_or_404(user_id)
    form = UserUpdateForm(obj=curr_user)

    if form.validate_on_submit():
        user = User.authenticate(form.username.data, form.password.data)
        if user:
            curr_user.username = form.username.data
            curr_user.email = form.email.data
            curr_user.image_url = form.image_url.data
            curr_user.header_image_url = form.header_image_url.data
            curr_user.bio = form.bio.data
            db.session.add(curr_user)
            db.session.commit()
        else:
            flash('Incorrect username or password', 'danger')
            return render_template('users/edit.html',
                                   form=form,
                                   user=curr_user)
        return render_template('users/detail.html', user=user)
    else:
        return render_template('users/edit.html', form=form, user=curr_user)
コード例 #16
0
ファイル: app.py プロジェクト: pkempf/warbler
def profile():
    """Update profile for current user."""

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserUpdateForm(obj=g.user)
    if form.validate_on_submit():
        user = User.authenticate(g.user.username, form.pwd.data)

        if user:
            form.populate_obj(user)
            g.user = user
            db.session.add(user)
            db.session.commit()
            return redirect(f"/users/{g.user.id}")

        flash("That password didn't work. Please try again.", "danger")
        return render_template("/users/update.html", form=form)

    else:
        return render_template("/users/update.html", form=form)
コード例 #17
0
ファイル: views.py プロジェクト: ercecan99/Clubee
def profile(user_id):
    """
    a profile page for normal registered users
    if it is users own profile; user can change account info
    else; user can see the common clubs and basic information of the other user
    """
    if not current_user.is_authenticated:
        return redirect(url_for('login'))
    if current_user.is_admin:
        abort(401)
    try:
        form = UserUpdateForm()
        user = get_user_by_id(id=current_user.id)
        if request.method == 'GET':
            if user_id == current_user.id:
                user = get_user_by_id(id=current_user.id)
                form.name.data = user.name
                form.surname.data = user.surname
                form.student_id.data = user.student_id
                form.email.data = user.email
                #form.department.data = user.department
                if user.gender:
                    form.gender.data = user.gender
                return render_template('profile.html',
                                       form=form,
                                       name=None,
                                       surname=None,
                                       student_id=None,
                                       department=None)
            elif user_id != current_user.id:
                user = get_user_by_id(id=user_id)
                name = user.name
                surname = user.surname
                student_id = user.student_id
                department = user.department
                get_common_clubs_statement = """select clubs.id, clubs.name from clubs 
                                                join members 
                                                on clubs.id = members.club_id 
                                                join users 
                                                on users.id = members.user_id 
                                                where users.id = %s
                                                INTERSECT 
                                                select clubs.id, clubs.name from clubs 
                                                join members 
                                                on clubs.id = members.club_id 
                                                join users 
                                                on users.id = members.user_id 
                                                where users.id = %s """
                with connection.cursor() as cursor:
                    cursor.execute(get_common_clubs_statement,
                                   (current_user.id, user_id))
                    common_clubs = cursor.fetchall()

                return render_template('profile.html',
                                       name=name,
                                       surname=surname,
                                       student_id=student_id,
                                       department=department,
                                       common_clubs=common_clubs)
        elif request.method == 'POST':
            if 'delete' in request.form:
                return "asd"
                logout_user()
                delete_user(user_id=user_id)
                flash('User Deleted')
                return redirect(url_for('register'))
            if form.validate_on_submit():
                name = form.data["name"]
                surname = form.data["surname"]
                student_id = form.data["student_id"]
                email = form.data["email"]
                if form.data["gender"]:
                    gender = form.data["gender"]
                else:
                    gender = None
                x = False
                if (user.student_id != student_id):
                    x = True
                update_user(current_user.id, name, surname, student_id, email,
                            gender)
                if x:
                    flash(
                        'Your student id is changed, login again with your new id'
                    )
                return redirect(url_for('profile', user_id=current_user.id))
            else:
                print(form.errors)
                return render_template('profile.html', form=form)
    except Exception as e:
        print("Error in profile page", e)