def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: old_picture = current_user.image_file try: if old_picture != 'default.jpg': os.remove('{}/static/profile_pics/{}'.format( current_app.root_path, old_picture)) except OSError: print('No file') picture_file = save_picture(form.picture.data) current_user.image_file = picture_file except Exception: flash('Something went wrong', 'danger') return redirect(url_for('users.account')) else: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename=f'profile_pics/{current_user.image_file}') return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) last_img = os.path.join(current_app.root_path, 'static/profile_pics/', current_user.image_file) if os.path.exists( last_img) and not last_img.endswith('default.png'): os.remove(last_img) # else: # raise NameError(last_img) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': # about browser message: "are you shure you want submit again?" form.username.data == current_user.username form.email.data == current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title=f'Account {current_user.username}', image_file=image_file, form=form)
def account(): """ The Account page route that displays the user's information and allows them to update it. Uses the UpdateAccountForm. """ form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename=f'profile_pics/{current_user.image_file}') return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: old_picture = current_user.image_file # Temporarily remember old picture picture_file = save_picture(form.picture.data) current_user.image_file = picture_file if old_picture != 'default.jpg': # Remove old profile pic if not default image os.remove( os.path.join(current_app.root_path, 'static/profile_pics', old_picture)) current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): # Create instance on UpdateAccountForm imported from forms module form = UpdateAccountForm() # Check if POST route and form is valid if form.validate_on_submit(): # Check if profile picture exists, use save_picture function created to pass in information and save file so file system, get new picture filename back if form.picture.data: # CREATE LOGIC TO REMOVE OLD PROFIILE PICTURE WHEN NEW ONE IS UPLOADED # ************************************ picture_file = save_picture(form.picture.data) # Call save_picture function which returns new picture file name current_user.image_file = picture_file # Change current data to submitted data from form, using flask_login module, current_user class current_user.username = form.username.data current_user.email = form.email.data # Update db db.session.commit() # Send flash message to user successful update flash('Your account has been updated!', 'success') # POST,GET redirect pattern, seen on browser reload, Are your sure you want to reload, prevent POST request to account return redirect(url_for('users.account')) elif request.method == 'GET': # Populate account update form with current user data if method is GET form.username.data = current_user.username form.email.data = current_user.email # Get user profile pic from the db image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): if form.image.data: post_image = save_picture(form.image.data, directory='static/post_image') # if post already has image, remove it if post.image_file: old_image = post.image_file remove_picture(old_image, directory='static/post_image') post.image_file = post_image post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template("posts/create_post.html", title="Update Post", form=form, legend="Update Post")
def account(): form = AccountInfo() if form.validate_on_submit(): if form.picture.data: # delete current profile image if current is not default.jpg if current_user.image_file != 'default.jpg': delete_picture(current_user.image_file) # save new profile image current_user.image_file = save_picture(form.picture.data) current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated.', 'success') return redirect(url_for('users.account')) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', form=form, image_file=image_file)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: file_name = save_picture(form.picture.data) remove_picure(current_user.image_file) # updating the image file current_user.image_file = file_name # Updating the user details with given values current_user.username = form.username.data current_user.email = form.email.data # commiting all changes db.session.commit() flash('Your account information has been updated succesfully', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title="Account", image=image, form=form)
def account(): form = UpdateAccountForm() #create instance of UpdateAccountForm #Add condition if our form is valid during submission (this is for 'POST' request) if (form.validate_on_submit()): #Add condition to see if there is any picture data. This is not required filed so we need to have this check if (form.picture.data): picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() #submit updated username and email to db flash( 'Your account has been updated!', 'success' ) #Add flash message that will tell user that username nad mail have been updated return redirect( url_for('users.account')) #Redirect now to account page #It will be nice if our form will be already populated will username data and email. This if is for this purpose #If request.method == 'GET' then populate the forms with username and email elif (request.method == 'GET'): form.username.data = current_user.username form.email.data = current_user.email image_file = url_for( 'static', filename='profile_pics/' + current_user.image_file ) # user images will be located in static/profile_pics folder return render_template('account.html', title='Account', image_file=image_file, form=form) #pass form to our account.html template
def account(): form = UpdateAccountForm() user = User.query.filter_by(id=current_user.id).first_or_404() page = request.args.get('page', 1, type=int) posts = Post.query.filter_by(author=user).order_by( Post.date_posted.desc()) #.paginate(page=page,per_page=5) #sample code replace it with pagination total = 0 for post in posts: total += 1 if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('account has been updated', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='account', image_file=image_file, form=form, posts=posts, user=user, total=total)
def account(): form = UpdateAccountForm() # this if block is for POST method if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/%s' % current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): # ---------------- update the picture --------------------------------- if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file #-----------------update the user and the email ----------------------- current_user.username = form.username.data current_user.email = form.email.data #commit the changes to the database db.session.commit() # send a flash messge that the username or email was changed flash('Your account has been updated', 'success') #go back to account return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): updateAccountForm = UpdateAccountForm() if updateAccountForm.validate_on_submit(): if updateAccountForm.display_picture.data: # call save picture function which returns a file name picture_file = save_picture(updateAccountForm.display_picture.data) # set new picture with new file name current_user.profile_image = picture_file current_user.username = updateAccountForm.username.data current_user.email = updateAccountForm.email.data db.session.commit() flash('Your account has been successfully updated!', 'success') # redirect to account page again to prevent POST GET redirect pattern (page reload and data resubmit) return redirect(url_for('users.account')) # retrieve user data elif request.method == 'GET': updateAccountForm.username.data = current_user.username updateAccountForm.email.data = current_user.email profile_image = url_for('static', filename='profile_pics/' + current_user.profile_image) return render_template('account.html', title='Account', profile_image=profile_image, form=updateAccountForm)
def account(): """ To update the account information :return: if the form is submitted, redirect to users.account, else render account.html, title, image_file, form, year_dict, month_dict, side_posts with the current user and email filled on the form """ form = UpdateAccountForm() if form.validate_on_submit(): # if the picture is updated if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email # to show the image data on the web site image_file = url_for('static', filename='profile_pics/' + current_user.image_file) year_dict, month_dict = get_postdates() side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5) return render_template('account.html', title='Account', image_file=image_file, form=form, year_dict=year_dict, month_dict=month_dict, side_posts=side_posts)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) #This conditional is saying: "If the user has uploaded a new file, # ...then set the image file as that new file." current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account details have been updated.', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if not form.picture.data and current_user.username == form.username.data and current_user.email == form.email.data: flash('nothing has changed. please check form.', 'warning') return redirect(url_for('users.account')) if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file db.session.commit() flash('Your profile image had been updated!', 'success') if current_user.username != form.username.data or current_user.email != form.email.data: current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account had been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash("Your account has been updated.", "success") return redirect(url_for("users.account")) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email image_file = url_for("static", filename=f"profile_pics/{current_user.image_file}") return render_template("account.html", title="Account", image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.first_name = form.first_name.data current_user.last_name = form.last_name.data current_user.email = form.email.data current_user.phone_number = form.phone_number.data current_user.address = form.address.data current_user.about_me = form.about_me.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.first_name.data = current_user.first_name form.last_name.data = current_user.last_name form.email.data = current_user.email form.phone_number.data = current_user.phone_number form.address.data = current_user.address form.about_me.data = current_user.about_me image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: # if picture is uploaded by the user, save the picture in the filesystem picture_file = save_picture( form.picture.data ) # and set the current user's picture to this saved current_user.image_file = picture_file # profile picture current_user.username = form.username.data current_user.email = form.email.data db.session.commit( ) # no need for .add() because both username and email are already in the database flash('Your account has been updated!', 'success') return redirect( url_for('users.account') ) # redirecting causes the browser to send a GET request instead of rendering template # which causes the browser to send the POST request again. This is called the # "post/redirect/get" pattern elif request.method == 'GET': # to populate the current user's data in the form by default on GET form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def new_task(): tasks = Task.query.filter_by(author=current_user).all() if len(tasks) >= 50: flash('You can not add more than 50 tasks', 'danger') return redirect(url_for('users.home')) else: form = TaskForm() if form.validate_on_submit(): file = None if form.file.data: file = save_picture(form.file.data) task = Task(title=form.title.data, content=form.content.data, author=current_user, due_date=form.due_date.data, completion=form.completion.data, completion_date=form.completion_date.data, attachment=file) print('lol : ', file) db.session.add(task) db.session.commit() flash('Your task has been created!', 'success') return redirect(url_for('users.home')) return render_template('create_task.html', title='New Task', form=form, legend='New Task')
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash(f'Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email form.designation.data = current_user.designation form.team.data = current_user.team eligible = User.query.filter(User.balance < 1000).filter( User.balance > 0).order_by(desc(User.earned)) image_file = url_for('static', filename='profile_pics/' + current_user.image_file) counts = User.query.order_by(desc(User.earned)).limit(5) return render_template('account.html', title='Account', image_file=image_file, form=form, counts=counts, eligible=eligible)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): # TODO maybe add profile pic clean up # profile pic update if form.picture.data: picture_f_name = save_picture(form.picture.data) current_user.image_file = picture_f_name # user info is updated current_user.username = form.username.data current_user.email = form.email.data db.session.commit() # remember to actually save the changes to the db flash('Account Updated!', 'success') return redirect( url_for('users.account')) # ensures a GET once return to page elif request.method == 'GET': # Pre-fill user information form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): page = request.args.get('page', 1, type=int) form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email posts = Post.query.filter_by(author=current_user) image_file = url_for('static', filename='profile_pictures/' + current_user.image_file) posts = Post.query.filter_by(author=current_user).order_by( Post.date_posted.desc()).paginate(page=page, per_page=5) #---by me return render_template('account.html', title='Account', image_file=image_file, form=form, posts=posts)
def account(): form = UpdateAccountForm() form.country.choices = [(i['name'], i['name']) for i in countries] if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data # update the current user's username with the one if form current_user.email = form.email.data current_user.country = form.country.data current_user.about_me = form.about_me.data db.session.commit() flash('Your account has been updated', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email form.country.data = current_user.country form.about_me.data = current_user.about_me image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def settings(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.firstname = form.firstname.data current_user.lastname = form.lastname.data current_user.email = form.email.data current_user.about = form.about.data current_user.location = form.location.data hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') current_user.password = hashed_password db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.user_posts', id=current_user.id)) elif request.method == 'GET': form.firstname.data = current_user.firstname form.lastname.data = current_user.lastname form.about.data = current_user.about form.email.data = current_user.email form.birthday.data = current_user.birthday form.location.data = current_user.location image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('settings.html', title='Settings', image_file=image_file, form=form)
def account(): """Account""" form = UpdateAccountForm() if form.validate_on_submit(): # check if theres picture data if form.picture.data: picture_file = save_picture(form.picture.data) # update currentuser with new image current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated.', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': # populate fields if GET form.username.data = current_user.username form.email.data = current_user.email # get users imagefile from db and load it image_file = url_for('static', filename=f'profile_pics/{current_user.image_file}') return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_path = os.path.join(current_app.root_path, 'static/profile_pics', current_user.image_file) if current_user.image_file != 'default.jpg': os.remove(picture_path) picture_file = save_picture(form.picture.data) current_user.image_file = picture_file if current_user.email != form.email.data: current_user.confirmed_email = False current_user.username = form.username.data current_user.email = form.email.data session.commit() flash('Your account has been updated', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form, confirm_email=current_user.confirmed_email)
def account(): form = AccountForm() languages = current_app.config['LANGUAGES'] locale = list(languages)[0] if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.img_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash(_('Your account has been updated!'), 'success') return redirect(url_for('account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.img_file) return render_template('account.html', title='Account', image_file=image_file, form=form, languages=languages, locale=locale, _l=_l)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: oldn = current_user.image_file if not oldn == "\\default.jpg": oldpath = os.path.join(current_app.root_path, "static/profile_pics", oldn) os.remove(oldpath) picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data current_user.bio = form.bio.data db.session.commit() flash("Your account has been updated!", "success") return redirect(url_for("users.account")) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email form.bio.data = current_user.bio image_file = url_for("static", filename="profile_pics/" + current_user.image_file) return render_template("account.html", title="Account", image_file=image_file, form=form)
def account(): #Create instance of form form = UpdateAccountForm() if form.validate_on_submit(): print('form.picture.data is') print(form.picture.data) #If there is picture data, change profile picture if form.picture.data: picture_file = save_picture(form.picture.data) #Update in db current_user.image_file = picture_file #Update data in db current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) #Populate form fields with current fields elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email #Get image file image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)