Skip to content
Open
Show file tree
Hide file tree
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
59 changes: 48 additions & 11 deletions neo_dolfin/api/database_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,37 @@ class VARCHAR(50),
return "An error occurred: " + str(e)


def register_user(email, mobile, first_name, middle_name, last_name, password, gender, occupation, birth, address, city,
country, state, postcode):
import sqlite3

def register_user(username, email, mobile, first_name, middle_name, last_name, password):
"""
Registers a new user in the database.
Parameters include personal information, credentials of the user, and address details.
Creates a new user and adds it to the users table in the database.

Args:
username (str): User's username.
email (str): User's email address.
mobile (str): User's mobile number.
first_name (str): User's first name.
middle_name (str): User's middle name.
last_name (str): User's last name.
password (str): User's hashed password.

Returns:
dict: A dictionary containing the result of the operation or an error message.
"""
database_address = "../db/dolfin_db.db" # Path to your database
try:
with sqlite3.connect(database_address) as conn:
cursor = conn.cursor()
cursor.execute('''INSERT INTO users (email, mobile, first_name, middle_name, last_name, password, gender, occupation, birth, address, city, country, state, postcode)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(email, mobile, first_name, middle_name, last_name, password, gender, occupation, birth,
address, city, country, state, postcode))
# SQL to insert a new user into the users table
cursor.execute('''
INSERT INTO users (username, email, mobile, first_name, middle_name, last_name, password)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (username, email, mobile, first_name, middle_name, last_name, password))
conn.commit()
return "User inserted successfully into 'users' table."
return {cursor.lastrowid} # Return new user ID
except sqlite3.Error as e:
return "An error occurred: " + str(e)

return {"success": False, "error": str(e)} # Return error message if something goes wrong

def get_basiq_id(user_id):
"""
Expand Down Expand Up @@ -321,3 +334,27 @@ def verify_user(email, provided_password):
except sqlite3.Error as e:
print("An error occurred:", e)
return None

def check_user_exists(username, email):
"""
Check if a user with the given username or email already exists in the database.

Args:
username (str): The username to check in the database.
email (str): The email to check in the database.

Returns:
bool: True if user exists, False otherwise.
"""
database_address = "../db/dolfin_db.db" # Adjust the path as necessary
try:
with sqlite3.connect(database_address) as conn:
cursor = conn.cursor()
# Query to check if a username or email already exists
cursor.execute("SELECT 1 FROM users WHERE username = ? OR email = ?", (username, email))
if cursor.fetchone():
return True # User exists
return False # No user found
except sqlite3.Error as e:
print("An error occurred:", e)
return False # Return False or handle the error appropriately depending on your error handling policy
37 changes: 10 additions & 27 deletions neo_dolfin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,23 +411,10 @@ def register():
# If present, set validation_checkbox to True; otherwise, set it to False
validation_checkbox = True if 'validation' in request.form else False

""" OLD DB FORMAT:
# Check if the username or email already exists in the database
existing_user = User.query.filter_by(username=input_username).first()
existing_email = User.query.filter_by(email=input_email).first()

# OLD Create a new user and add it to the database
new_user = User(username=input_username, email=input_email, password=input_password)
db.session.add(new_user)
db.session.commit()
"""
#
existing_user = UsersNew.query.filter_by(username=input_username).first()
existing_email = UsersNew.query.filter_by(email=input_email).first()
print(existing_user)
existing_user = db_op.check_user_exists(input_username, input_email)

# If user exists, they need to retry.
if existing_user or existing_email:
if existing_user:
user_log.info("REGISTER: (ip?) attempted to register as an existing user.") # keep usernames ambiguous, for now
return 'Username or email already exists. Please choose a different one.'

Expand All @@ -437,26 +424,22 @@ def register():
arg_hash = PasswordHasher()
input_password = arg_hash.hash(input_password) #; print(hashed)

# Create a new user and add it to the users_new database
# Names are currently hard coded pending name fields in registration
new_user = UsersNew(username=input_username, email=input_email, mobile="+61450627105",
new_user_id = db_op.create_user(username=input_username, email=input_email, mobile="+61450627105",
first_name="SAMPLE1",middle_name="test",last_name="USER",password=input_password)
db.session.add(new_user)
db.session.commit()


user_ops.register_basiq_id(new_user.id) # Create a new entity on our API key, based on the data passed into the user registration form
user_ops.register_basiq_id(new_user_id) # Create a new entity on our API key, based on the data passed into the user registration form

user_ops.link_bank_account(new_user.id) # A user will need to link an account to their Basiq entity (that they won't see the entity)
user_ops.link_bank_account(new_user_id) # A user will need to link an account to their Basiq entity (that they won't see the entity)
# Log result
user_log.info("REGISTER: New user %s, (%s %s) successfully registered and added to database."%(new_user.username,new_user.first_name, new_user.last_name))
user_log.info("REGISTER: New user %s, (%s %s) successfully registered and added to database."%(input_username,"SAMPLE1", "USER"))

# create a new mapping for a user
# not relevant in users_new but remains in if need for later database gen
new_user_id = new_user.id
new_user_map = UserTestMap(userid = input_username, testid=new_user_id)
db.session.add(new_user_map)
db.session.commit()

# new_user_map = UserTestMap(userid = input_username, testid=new_user_id)
# db.session.add(new_user_map)
# db.session.commit()

# Validate address using AddressFinder API and Create a new user address entry to the database
user = User.query.filter_by(id=new_user_id).first()
Expand Down