Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions UI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from getpass import getpass
# importing 'mysql.connector' as mysql
import mysql.connector as mysql

class DbConnectionError(Exception):
pass

#DB Connection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All db connection things to be moved to db_utils

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that over the weekend :)

def _connect_to_db():
cnx = mysql.connect(
host = "localhost",
user = "root",
passwd = "password", #Reviewer Password
database = "walk2zero"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After moving to db_utils.py, we will need to import variables from the config.py file from config import USER, PASSWORD, HOST, and change string values to those variables

)
return cnx

#Function to accept User Input
def user_reg():
first_name = input("Please enter your first name = ")
last_name = input("Please enter your last name = ")
email = input("Please enter your email address = ")
password = getpass("Please enter a desired password = ")
new_registration(first_name,last_name,email,password)

#Function to log-in an exisiting user
def login():
check_email = input("Please enter your email address = ")
check_password = getpass("Please enter your password = ")
verify_login(check_email,check_password)


#Function to register new user details into the DB
def new_registration(first_name,last_name,email,password):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to db_utils

#calling DB connection
db_connection = _connect_to_db()
cur = db_connection.cursor()

#defining the Query
query = "INSERT INTO users (fname, lname, email, pword) VALUES (%s, %s,%s,%s)"

#storing values in a variable
values = (first_name, last_name,email,password)

#executing the query with values
cur.execute(query, values)
db_connection.commit()
cur.close()
print("Successful Registration!")
print("")


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to have this in a separate file when we get to writing the user interface properly.

But I really like the exception handling!!!!

#Function to verify login
def verify_login(check_email,check_password):
try:
#calling DB connection
db_connection = _connect_to_db()
cur = db_connection.cursor()

#defining the query
query = "SELECT email,pword FROM users"
cur.execute(query)
result = cur.fetchall()

#converting the tuple query output to dictionary to have Key as email and value as password
user_dict = dict((x, y) for x, y in result)

#Comparing the email and password in the dictionary to check_email and check_password
for key in user_dict.keys():
if key == check_email:
if user_dict[key] == check_password:
print("Successful Login!")
else:
print("Incorrect Credentials. Please try logging in again!")
login()

except Exception:
raise DbConnectionError("Failed to read data from DB")

finally:
if db_connection:
db_connection.close()
print("")


#menu function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brill way to start the UI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!! Once I've migrates stuff to db.utils, as per your rec, we could delete the branch and create a new one for review with the wider team.

def menu():
print ("Welcome to Walk2Zero!")
print("")
print ("Taking Climate Conscious Steps for a Cooler Earth!")
print("")
print ("1. New user. Register to the service")
print ("2. Existing user. Login->")
choice = int(input("Enter your choice = "))

if choice == 1:
user_reg()
elif choice == 2:
login()

def main():
menu()

if __name__ == "__main__":
main()