def user_panel_profile(): form = UpdateProfile() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture( form.picture.data ) # save_picture is the function created above current_user.image_file = picture_file if form.email.data != current_user.email: user_check = user.query.filter_by(email=form.email.data).first() if user_check: flash( 'Такой email уже используется в системе! Введите другой email' ) return redirect(url_for('user_panel_profile')) current_user.phone = form.phone.data current_user.email = form.email.data db.session.commit() flash('Ваш профиль был обновлен', 'success') return redirect(url_for('user_panel_profile')) elif request.method == 'GET': form.phone.data = current_user.phone form.email.data = current_user.email image_file = url_for('static', filename='avatars/' + current_user.image_file) return render_template('UserPanel/profile.html', image_file=image_file, form=form, current_user=current_user)
def update_profile(id): user = load_user(current_user.get_id()) profile = Profile.query.filter_by(profile_id=id, user_id=user.user_id) if profile.count() != 1: flash("Can't find profile") return redirect(url_for("web_profiles.show_profiles")) form = UpdateProfile(obj=profile.first()) if form.validate_on_submit(): data = { "name": form.name.data, "restrictions": form.restriction.data } fields = profile_schema.load(data, partial=True) profile.update(fields) db.session.commit() flash("Profile updated!") return redirect(url_for("web_profiles.show_profiles")) return render_template("update_profile.html", form=form, id=id)
def edit_profile(hiker_id): profile_to_edit = Hiker.objects.get({'_id': ObjectId(hiker_id)}) form = UpdateProfile(obj=profile_to_edit) if form.validate_on_submit(): try: Hiker.objects.raw({"_id": ObjectId(hiker_id)}).update( {"$set": {"fname": form.fname.data, "lname": form.lname.data, "origin": form.origin.data, "email": form.email.data, "trails_completed": form.trails_completed.data, "profile_pic": form.profile_pic.data } }, upsert=False) flash("Profile Updated Successfully.", 'teal') return redirect(url_for('index')) except ValidationError as ve: errors = ve.message flash(errors, 'deep-orange darken-3') return render_template('trails/edit_profile.template.html', form=form, profile=profile_to_edit, cloud_name=CLOUD_NAME, upload_preset=UPLOAD_PRESET, api_key=API_KEY)
def profile(): '''queries all the posts from database ''' posts = Post.query.all() ''' form for updating profile , which updates profile picture of user''' form = UpdateProfile() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) '''saves the picture given by user''' current_user.image_file = picture_file ''' sets the user profile as the inputed image''' db.session.commit() ''' commits the changes done in database ''' flash('Your account has been updated!', 'success') ''' flashes message on successfully updating profile picture ''' return redirect(url_for('profile')) image_file = url_for('static', filename='profile_pics/' + current_user.image_file) ''' sets the profile picture as updated(if updated) image''' return render_template('profile.html', image_file=image_file, form=form, posts=posts)