def withdraw_reciept(new_amount, withdrawed_amount): """This function create a recipt bill for withdraw balance and open the file. parameters: new_amount: the amount of money after depsoit withdrawd_amount: the amount of desposited money """ id = current_user().id name = current_user().name new_amount = number_format(new_amount) withdrawed_amount = number_format(withdrawed_amount) recipt = fpdf.FPDF('P', 'mm', (115, 115)) recipt.add_page() recipt.set_font('Arial', '', 9) text1 = "Welcome To Banking System".center(75, "-") text2 = f"User ID: {id} \n Name: {name} \n Date: {datetime.now()} \n" text3 = f"Withdraw Money \n" text4 = f"Withdrawed Amount: {withdrawed_amount} Afg \n" text5 = f"New Balance: {new_amount} Afg\n" text6 = "Thank You".center(80, "-") text = f"{text1} \n {text2} {text3} {text4} {text5} {text6}" recipt.multi_cell(100, 10, text, border=0, align='c') recipt.output(f"{id}{name}-recipt.pdf") #open the recipt file run_file(f"{id}{name}-recipt.pdf")
def email_is_unique(model, email, ftype): """This function query the table to check the email is unique or not. It is shared by both users and admins. for admins when they add new user or another admin. And for users when the update their profile info. parameters: ---------- model: model -- The model in whihc it has to search form email: str -- The value of email to be checked ftype: string -- Specify it checks for update or adding Return: boolean """ if not is_authenticated(): return redirect(url_for('login')) if ftype == 'add': row = model.query.filter_by(email=email).first() else: # now check email to be unique in all rows except the current one rows = model.query.filter_by(email=email).all() for row in rows: if row.id != current_user().id: return False row = None if row is not None: return False else: return True
def change_password(): """This function change both users and amins password. It is accessable by both users and admin that is why we first access the is_admin() to check if it is an admin or not. """ if not is_authenticated(): return redirect(url_for('login')) admin = is_admin() form = AdminUpdateForm() if admin else UserUpdateForm() pass_form = changePasswordForm() redirect_page_url = "admin/edit_profile_admin.html" if admin \ else "edit_profile.html" redirect_url = "admin_manage_profile" if admin else "profile" user = current_user() if pass_form.validate(): if not verify_password(pass_form.old_password.data): flash("Invalid Password", category="old_pass_incorect") return render_template(redirect_page_url, form=form, pass_form=pass_form ) else: user.password = pbkdf2_sha256.hash(pass_form.new_password.data) db.session.commit() flash("Password Changed", category="addSuccess") return redirect(url_for(redirect_url)) else: return render_template(redirect_page_url, form=form, pass_form=pass_form )
def deposit_money(): """This function deposit money if the user is normal user.""" # redirct if user is already authenticated if not is_authenticated() or is_admin(): return redirect(url_for('login')) form = DepositMoneyForm() if request.method == "GET": return render_template("deposit.html", form=form) else: if form.validate(): current_user().balance += int(form.amount.data) db.session.commit() flash("Seccessfully Deposited", category="addSuccess") if form.reciept.data: Recipt.deposit_reciept(current_user().balance, form.amount.data) return redirect(url_for('deposit_money')) else: return render_template("deposit.html", form=form)
def balance_reciept(): """This function create a recipt bill for checking balance and open the file""" id = current_user().id name = current_user().name balance = number_format(current_user().balance) recipt = fpdf.FPDF('P', 'mm', (115, 110)) recipt.add_page() recipt.set_font('Arial', '', 9) text1 = "Welcome To Banking System".center(75, "-") text2 = f"User ID: {id} \n User Name: {name} \n Date: {datetime.now()}\n" text3 = f"Check Balnace \n Amount: {balance} AFG \n" text4 = "Thank You".center(80, "-") text = f"{text1} \n {text2} {text3} {text4}" recipt.multi_cell(100, 10, text, border=0, align='c') recipt.output(f"{id}{name}-recipt.pdf") #open the recipt file run_file(f"{id}{name}-recipt.pdf")
def admin_manage_profile(): """This function return edit form in get reques and update info in post.""" if not is_authenticated() or not is_admin(): return redirect(url_for('login')) form = AdminUpdateForm() pass_form = changePasswordForm() if request.method == "GET": form.name.data = current_user().name if current_user() else "" form.email.data = current_user().email if current_user() else "" return render_template("admin/edit_profile_admin.html", form=form, pass_form=pass_form ) else: if form.validate(): if not email_is_unique(AdminModel, form.email.data, 'update'): flash("email already taken", category="emailNotUnique") return render_template("admin/edit_profile_admin.html", form=form, pass_form=pass_form ) if verify_password(form.password_verify.data): user = current_user() user.name = form.name.data user.email = form.email.data.lower() db.session.commit() flash("Admin Updated", category="addSuccess") return redirect(url_for('admin_manage_profile')) else: flash("Invalid Password", category="passwordIncorrect") return render_template("admin/edit_profile_admin.html", form=form, pass_form=pass_form ) else: return render_template("admin/edit_profile_admin.html", form=form, pass_form=pass_form )
def verify_password(password): """This function check the user password by hashing them parameters: ---------- password: the user typed password Return: Boolean """ if not is_authenticated(): return redirect(url_for('login')) user = current_user() result = pbkdf2_sha256.verify(password, user.password) return result
def profile(): """This function edit normal user profile.""" # redirct if user is already authenticated if not is_authenticated() or is_admin(): return redirect(url_for('login')) form = UserUpdateForm() pass_form = changePasswordForm() user = current_user() if request.method == "GET": form.name.data = user.name form.address.data = user.address form.email.data = user.email return render_template("edit_profile.html", form = form, \ pass_form = pass_form) else: if form.validate(): if not email_is_unique(UserModel, form.email.data, 'update'): flash("email already taken", category="emailNotUnique") return render_template("edit_profile.html", form=form, pass_form=pass_form) if verify_password(form.password_verify.data): user.name = form.name.data user.address = form.address.data user.email = form.email.data.lower() db.session.commit() flash("User Updated", category="addSuccess") return redirect(url_for('profile')) else: flash("Invalid Password", category="passwordIncorrect") return render_template("edit_profile.html", form=form, pass_form=pass_form) else: return render_template("edit_profile.html", form = form, \ pass_form = pass_form)