Implement an edit account feature
This commit is contained in:
parent
ccbe5a91d8
commit
ecacfaf64f
205
accounts.py
205
accounts.py
@ -3,6 +3,9 @@ import pymongo
|
||||
import time
|
||||
from werkzeug.security import *
|
||||
|
||||
from jobs import *
|
||||
from bank import *
|
||||
|
||||
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
|
||||
db = client.VE
|
||||
|
||||
@ -66,36 +69,184 @@ def get_account_status(code):
|
||||
|
||||
return user['account_status']
|
||||
|
||||
def assign_job(code, job_id):
|
||||
account_coll = db.accounts
|
||||
job_coll = db.jobs
|
||||
|
||||
user = account_coll.find_one({"_id": code})
|
||||
job = job_coll.find_one({"_id": job_id})
|
||||
|
||||
if user is None or job is None:
|
||||
print("Either that user doesn't exist, or the job doesn't exist")
|
||||
return None
|
||||
|
||||
user_education = user['education']
|
||||
job_education = job['education']
|
||||
|
||||
if user_education < job_education:
|
||||
print("We are sorry, but you are not qualified for this job."
|
||||
"Please go to the education center to get a higher education level.")
|
||||
time.sleep(1)
|
||||
return False
|
||||
else:
|
||||
account_coll.update_one({"_id": code}, {"$set": {"job": job_id}})
|
||||
return True
|
||||
|
||||
|
||||
def get_user_job(code):
|
||||
def edit_email(code, email):
|
||||
coll = db.accounts
|
||||
user = coll.find_one({"_id": code})
|
||||
|
||||
if user is None:
|
||||
print("That user could not be found")
|
||||
print("The user does not have an account")
|
||||
return None
|
||||
|
||||
return user['job']
|
||||
coll.update_one({"_id": code}, {"$set": {"email": email}})
|
||||
return True
|
||||
|
||||
def edit_password(code, password):
|
||||
coll = db.accounts
|
||||
user = coll.find_one({"_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user does not have an account")
|
||||
return None
|
||||
|
||||
coll.update_one({"_id": code}, {"$set": {"password": generate_password_hash(password, "scrypt")}})
|
||||
return True
|
||||
|
||||
def get_user_password(code, secret_phrase):
|
||||
if secret_phrase != os.getenv("SECRET"):
|
||||
print("Operation not permitted.")
|
||||
time.sleep(4)
|
||||
return
|
||||
|
||||
coll = db.accounts
|
||||
user = coll.find_one({"_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user could not be found")
|
||||
return
|
||||
|
||||
return user['password']
|
||||
|
||||
def account_manager(code):
|
||||
while True:
|
||||
print(f"""
|
||||
Welcome to the account manager!
|
||||
|
||||
Options:
|
||||
job - view your job if you have one. it'll also show the education requirement and the pay per hour.
|
||||
bank - enter a sub menu for the bank
|
||||
edit - edit your email or your password
|
||||
leave - leave the account manager
|
||||
""")
|
||||
option = input("")
|
||||
|
||||
# begin job
|
||||
if option == "job":
|
||||
job = get_user_job(code)
|
||||
|
||||
if job is None:
|
||||
print("You do not have a job at the moment")
|
||||
time.sleep(3)
|
||||
|
||||
job_details = get_job_details(job)
|
||||
|
||||
print(f"Job Title: {job_details.title}\n"
|
||||
f"Education Requirement: {job_details.education}\n"
|
||||
f"Pay Per Hour: {job_details.pph}")
|
||||
time.sleep(3)
|
||||
# end job
|
||||
# begin bank
|
||||
elif option == "bank":
|
||||
bank_status = get_bank_status(code)
|
||||
|
||||
if bank_status is False:
|
||||
activate = input("Your bank account is not activated. Do you want to activate it? (y/n): ")
|
||||
|
||||
if activate == "y":
|
||||
did_activate = activate_bank(code)
|
||||
|
||||
if did_activate is True:
|
||||
print("You have successfully activated your bank account. You can now start getting money in your bank account")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong with activating. Try again later..")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Cancelling..")
|
||||
time.sleep(3)
|
||||
|
||||
option = input(f"Welcome to your bank account. What would you like to do?\n"
|
||||
f"deactivate - deactivate your bank account\n"
|
||||
f"bankrupt - declare bankruptcy\n"
|
||||
f"empty - clear your bank account\n"
|
||||
f"exit - exit the bank manager\n")
|
||||
|
||||
if option == "deactivate":
|
||||
is_sure = input("Are you sure? (y/n): ")
|
||||
if is_sure == "y":
|
||||
did_deactivate = deactivate_bank(code)
|
||||
|
||||
if did_deactivate:
|
||||
print("Action completed successfully")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong..")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Cancelling..")
|
||||
time.sleep(3)
|
||||
elif option == "bankrupt":
|
||||
is_sure = input("Are you sure? (y/n): ")
|
||||
if is_sure == "y":
|
||||
did_declare_bankruptcy = declare_bankruptcy(code)
|
||||
|
||||
if did_declare_bankruptcy is True:
|
||||
print("Action completed successfully")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong..")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Cancelling..")
|
||||
time.sleep(3)
|
||||
elif option == "empty":
|
||||
is_sure = input("Are you sure? (y/n): ")
|
||||
if is_sure == "y":
|
||||
did_empty = empty_bank(code)
|
||||
|
||||
if did_empty is True:
|
||||
print("Action completed successfully")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong..")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Cancelling..")
|
||||
time.sleep(3)
|
||||
# end bank
|
||||
# begin edit
|
||||
elif option == "edit":
|
||||
option = input("What would you like to do?\n"
|
||||
"email - change your email\n"
|
||||
"password - change your password\n"
|
||||
"exit - leave the edit menu\n")
|
||||
|
||||
if option == "email":
|
||||
email = input("Enter your new email: ")
|
||||
did_change_email = edit_email(code, email)
|
||||
|
||||
if did_change_email is True:
|
||||
print("Action completed successfully")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong..")
|
||||
time.sleep(3)
|
||||
elif option == "password":
|
||||
password = input("Enter your old password: ")
|
||||
|
||||
old_password = get_user_password(code, os.getenv("SECRET"))
|
||||
|
||||
same_passwords = check_password_hash(old_password, password)
|
||||
|
||||
if same_passwords is True:
|
||||
new_password = input("Please enter your new password: ")
|
||||
did_change_password = edit_password(code, new_password)
|
||||
|
||||
if did_change_password is True:
|
||||
print("Action completed successfully")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("Something went wrong..")
|
||||
time.sleep(3)
|
||||
else:
|
||||
print("The passwords did not match. Cancelling operation..")
|
||||
time.sleep(4)
|
||||
# end edit
|
||||
# begin leave
|
||||
elif option == "leave":
|
||||
return
|
||||
# end leave
|
||||
|
||||
# check if the user wants to leave the account manager
|
||||
leave = input("Do you want to continue editing your account? (y/n): ")
|
||||
if leave != "y":
|
||||
break
|
89
bank.py
Normal file
89
bank.py
Normal file
@ -0,0 +1,89 @@
|
||||
import pymongo
|
||||
import os
|
||||
import random
|
||||
|
||||
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
|
||||
db = client.VE
|
||||
|
||||
def get_bank_status(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user could not be found.")
|
||||
return False
|
||||
|
||||
return user['active']
|
||||
|
||||
def activate_bank(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user could not be found.")
|
||||
return False
|
||||
|
||||
coll.update_one({"account_id": code}, {"$set": {"active": True}})
|
||||
return True
|
||||
|
||||
def deactivate_bank(code):
|
||||
coll = db.bank
|
||||
econ_coll = db.economy
|
||||
user = coll.find_one({"account_id": code})
|
||||
econ_user = econ_coll.find_one({"account_id": code})
|
||||
|
||||
if user is None or econ_user is None:
|
||||
print("The user could not be found.")
|
||||
return False
|
||||
|
||||
if user['balance'] > 0:
|
||||
coll.update_one({"account_id": code}, {"$set": {"balance": 0}})
|
||||
econ_coll.update_one({"account": code}, {"$inc": {"cash": user['balance']}})
|
||||
|
||||
coll.update_one({"account_id": code}, {"$set": {"active": False}})
|
||||
return True
|
||||
|
||||
def create_bank_account(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is not None:
|
||||
print("User already has a bank account")
|
||||
return None
|
||||
|
||||
coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0, 'active': False})
|
||||
return True
|
||||
|
||||
def get_balance(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
create_bank_account(code)
|
||||
return 0
|
||||
|
||||
return user['balance']
|
||||
|
||||
def declare_bankruptcy(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user could not be found")
|
||||
return False
|
||||
|
||||
coll.update_one({"account_id": code}, {"$set": {"balance": 0}})
|
||||
coll.update_one({"account_id": code}, {"$set": {"active": False}})
|
||||
|
||||
return True
|
||||
|
||||
def empty_bank(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
print("The user could not be found")
|
||||
return False
|
||||
|
||||
coll.update_one({"account_id": code}, {"$set": {"balance": 0}})
|
||||
return True
|
25
economy.py
25
economy.py
@ -1,28 +1,9 @@
|
||||
from accounts import *
|
||||
from bank import *
|
||||
|
||||
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
|
||||
db = client.VE
|
||||
|
||||
def create_bank_account(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is not None:
|
||||
print("User already has a bank account")
|
||||
return None
|
||||
|
||||
coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0, 'active': False})
|
||||
return True
|
||||
|
||||
def get_balance(code):
|
||||
coll = db.bank
|
||||
user = coll.find_one({"account_id": code})
|
||||
|
||||
if user is None:
|
||||
create_bank_account(code)
|
||||
return 0
|
||||
|
||||
return user['balance']
|
||||
|
||||
def get_cash(code):
|
||||
coll = db.economy
|
||||
@ -45,11 +26,12 @@ def create_econ_account(code):
|
||||
return False
|
||||
|
||||
econ_coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "cash": 0})
|
||||
bank_coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0})
|
||||
bank_coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0, 'active': False})
|
||||
|
||||
return True
|
||||
|
||||
def deposit(code, amount):
|
||||
# TODO: Check if the bank account is active. If not then don't allow the user to deposit
|
||||
bank_coll = db.bank
|
||||
econ_coll = db.economy
|
||||
bank_user = bank_coll.find_one({"account_id": code})
|
||||
@ -69,6 +51,7 @@ def deposit(code, amount):
|
||||
return True
|
||||
|
||||
def withdraw(code, amount):
|
||||
# TODO: Check if the bank account is active. If not then don't allow the user to withdraw
|
||||
bank_coll = db.bank
|
||||
econ_coll = db.economy
|
||||
|
||||
|
35
jobs.py
35
jobs.py
@ -1,5 +1,6 @@
|
||||
import pymongo
|
||||
import os
|
||||
import time
|
||||
|
||||
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
|
||||
db = client.VE
|
||||
@ -60,3 +61,37 @@ Pay Per Hour: {job['pph']}
|
||||
""")
|
||||
|
||||
return
|
||||
|
||||
def assign_job(code, job_id):
|
||||
account_coll = db.accounts
|
||||
job_coll = db.jobs
|
||||
|
||||
user = account_coll.find_one({"_id": code})
|
||||
job = job_coll.find_one({"_id": job_id})
|
||||
|
||||
if user is None or job is None:
|
||||
print("Either that user doesn't exist, or the job doesn't exist")
|
||||
return None
|
||||
|
||||
user_education = user['education']
|
||||
job_education = job['education']
|
||||
|
||||
if user_education < job_education:
|
||||
print("We are sorry, but you are not qualified for this job."
|
||||
"Please go to the education center to get a higher education level.")
|
||||
time.sleep(1)
|
||||
return False
|
||||
else:
|
||||
account_coll.update_one({"_id": code}, {"$set": {"job": job_id}})
|
||||
return True
|
||||
|
||||
|
||||
def get_user_job(code):
|
||||
coll = db.accounts
|
||||
user = coll.find_one({"_id": code})
|
||||
|
||||
if user is None:
|
||||
print("That user could not be found")
|
||||
return None
|
||||
|
||||
return user['job']
|
33
main.py
33
main.py
@ -107,7 +107,38 @@ Great job today!
|
||||
time.sleep(3)
|
||||
return
|
||||
# end balance
|
||||
# TODO: Add setup, store, inventory, deposit, and withdraw
|
||||
# TODO: Add setup, store, and inventory
|
||||
# begin deposit
|
||||
elif option == "deposit":
|
||||
amount = int(input("How much would you like to deposit? "))
|
||||
did_deposit = deposit(code, amount)
|
||||
|
||||
if did_deposit:
|
||||
print("The deposit is complete!")
|
||||
time.sleep(2)
|
||||
return
|
||||
|
||||
return
|
||||
# end deposit
|
||||
# begin withdraw
|
||||
elif option == "withdraw":
|
||||
amount = int(input("How much would you like to withdraw? "))
|
||||
did_withdraw = withdraw(code, amount)
|
||||
|
||||
if did_withdraw:
|
||||
print("The withdraw is complete!")
|
||||
time.sleep(2)
|
||||
return
|
||||
|
||||
return
|
||||
# end withdraw
|
||||
# begin setup
|
||||
elif option == "setup":
|
||||
account_manager(code)
|
||||
time.sleep(3)
|
||||
return
|
||||
# end setup
|
||||
|
||||
# main function
|
||||
while code is not None:
|
||||
print(f"""
|
||||
|
Loading…
Reference in New Issue
Block a user