def delete_user(): message = None typed = None print(f"Logged In : {logged_in}") User_cursor = Users.find().sort('username') if request.method == 'POST': userid = request.form.get('uid') if userid: message = None print("User Name to be Edited :", userid) x = Users.find_one({'username': userid}) if x: print("The Data found :", x) y = Users.delete_one({'username': userid}) message = f"{y.deleted_count} Record Deleted!" typed = "success" #return redirect(f'/update_cart/{userid}') else: message = 'Invalid Cart Id!' typed = "danger" print("Enter a Valid Cart Id") else: print("Please Enter a Cart Id") return render_template('deleteuser.html', usrcrsr=Users, message=message, type=typed, logged_in=logged_in, logged_in_detail=logged_in_detail, title='Delete User')
def is_present(key, value): query = {key: value} x = Users.find_one(query) if x: return True else: return False
def coutnadmins(): global admincount admincount = 0 x = Users.find({"admin": "True"}) for i in x: print(i['first_name'] + " " + i['last_name']) admincount += 1 print("No of Admins : ", admincount)
def add_user(): message = None print(f"Logged In : {logged_in}") User_cursor = Users.find().sort("username") if request.method == 'POST': username = request.form.get('uname') if username: message = None print("Username to be Edited :", username) if Users.find_one({'username': username}): print("The Data found :", Users.find_one({'username': username})) else: message = 'Invalid Username!' print("Enter a Valid Username") else: print("Please Enter a Username") return render_template('addusers.html', usrcrsr=Users, message=message, title='Add User')
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 dashboard(): # graph = create_plot() # ids,graphJSON = index() User_cursor = Users.find().sort("username").limit(8) Cart_cursor = Carts.find().sort("_id").limit(8) Product_cursor = Products.find().sort("_id").limit(8) Customer_cart_cursor = Customer_Cart.find().sort("used_date").limit(8) Billing_cursor = Billing.find().sort("_id").limit(8) print(f"Logged In : {logged_in}") return render_template( 'dashboard.html', title='Dashboard', logged_in=logged_in, user=logged_in_detail, usrcrsr=User_cursor, crtcrsr=Cart_cursor, prdcrsr=Product_cursor, cccrsr=Customer_cart_cursor, bilcrsr=Billing_cursor) #, graphJSON=graphJSON, ids=ids)
def forgotpassword(): message = None global logged_in global receiver if request.method == "POST": receiver = request.form.get('receiver') admin_pass = request.form.get('admin_pass') print("Mail Receiver : ", receiver, " Company Password : "******"Logged In : {logged_in}") return render_template('forgotpassword.html', title='Forgot Password', message=message)
def save_data(username, fname, lname, mobile, email, password, access, image="/static/profile_pics/default.jpg"): value = { "username": username, "first_name": fname, "last_name": lname, "mobile": mobile, "email": email, "password": password, "image_file": image } if access == 'Admin': value['admin'] = 'True' else: value['admin'] = 'False' x = Users.insert_one(value) print("Data Inserted", x.inserted_id)
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)
def check_data(email): query = {"email": email} x = Users.find_one(query) return x