def changepassword(): global loginmsg if request.method == "POST": np = request.form.get('newpass') cp = request.form.get('confpass') print('New Password : '******'Confirm Password : '******'New Pasword Matched!') x = Users.find_one({'email': receiver}) print('Receiver Data whose Password is to be changed : ', x) if x: hashed_password = crypt.generate_password_hash(np).decode( 'utf-8') print( f'Hash Password for {np} generated is :\n{hashed_password}' ) y = Users.update_one({"email": receiver}, {"$set": { "password": hashed_password }}) if y.modified_count: print('Password Changed Successfully!\n', Users.find_one({'email': receiver})) # send_confirmation() global pschanged pschanged = True loginmsg = 'Password Changed Successfully. You can login to your account now.' return redirect('/login') else: print('Error in Saving New Password!') else: print('No Such User Found with the Specified Email!') else: print('New Password did not match!') return render_template('changepassword.html', title='Change Password')
def account(): print(f"Logged In : {logged_in}") global logged_in_detail email = mobile = uname = oldpass = imgpath = message = umsg = None newvalues = {} if request.method == "POST": # print("Files Extraced :",request.files) # print("The user is :",logged_in_detail['first_name'],logged_in_detail['last_name']) fname = request.form.get('fname') lname = request.form.get('lname') email = request.form.get('email') mobile = request.form.get('mobile').strip() uname = request.form.get('uname').strip() oldpass = request.form.get('oldpass') newpass = request.form.get('newpass') confpass = request.form.get('confpass') if crypt.check_password_hash(logged_in_detail["password"], oldpass): print("Old Password Entered Correctly") print("Details Entered") print("Name :", fname, lname) print("Email :", email) print("Mobile :", mobile) print("Username :"******"Password :"******"We get",request.files.getlist('image_files'),"as File") if request.files.getlist('image_file'): image = request.files.getlist('image_file')[0] # print("The new image path is :",os.path.join(app.config['UPLOAD_FOLDER'].replace('\\','/'),image.filename)) print("And we get", image, "as File with filename :", image.filename) _, file_ext = os.path.splitext(image.filename) # print("Extracted file extention :",file_ext) random_filename = secrets.token_hex(8) # print("Random file name generated :",random_filename) new_filename = random_filename + file_ext print("New file name :", new_filename) # print("The uploading folder path is :",app.config['UPLOAD_FOLDER'].replace('\\','/')) # print("The root path is",app.root_path.replace('\\','/')) new_file_path = os.path.join(app.config['UPLOAD_FOLDER'], new_filename) print("The new path to store the image : ", new_file_path) image.save(new_file_path) i = Image.open(new_file_path) output_size = (200, 200) i.thumbnail(output_size) i.save(new_file_path) imgpath = "/static/profile_pics/" + new_filename print("imgpath : " + imgpath) print(i) print("Image Saved!!!") else: print("No File Selected!") if newpass == confpass and newpass != '' and confpass != '': newvalues['password'] = crypt.generate_password_hash( newpass).decode('utf-8') print("Hash of New Password created :", newvalues['password']) print("New Password is safe") else: print("Password Doesn\'t Match") if email and email != logged_in_detail['email']: newvalues['email'] = email else: newvalues['email'] = logged_in_detail['email'] if mobile and mobile != logged_in_detail['mobile']: newvalues['mobile'] = mobile else: newvalues['mobile'] = logged_in_detail['mobile'] if uname and uname != logged_in_detail['username']: if not Users.find_one({'username': uname}): newvalues['username'] = uname else: umsg = uname else: newvalues['username'] = logged_in_detail['username'] if imgpath: newvalues['image_file'] = imgpath else: newvalues['image_file'] = logged_in_detail['image_file'] print("The Data Created :", newvalues) print("Data Created for : ", logged_in_detail['first_name']) x = Users.update_one( {'first_name': logged_in_detail['first_name']}, {'$set': newvalues}) if x.modified_count: message = 'Changes Saved!' print(message) else: print("Any Error Occured in saving the Changes") logged_in_detail = check_data(newvalues['email']) print("Changed Logged in Data,", logged_in_detail['first_name'], logged_in_detail['image_file']) pre_data = {"email": email, "mobile": mobile, "uname": uname} if logged_in: print('Logged in Data :', logged_in_detail['first_name'], logged_in_detail['last_name'], logged_in_detail['image_file']) else: print("Not Logged In!") return render_template('account.html', title='Account', user=logged_in_detail, logged_in=logged_in, oldpass=oldpass, pd=pre_data, msg=message, umsg=umsg)