Skip to content
Permalink
Browse files
docstrings
  • Loading branch information
memica committed Mar 14, 2020
1 parent 51ff405 commit 7487bc39f561e6b7790e82754bbed4449dd82c87
Show file tree
Hide file tree
Showing 32 changed files with 801 additions and 217 deletions.
BIN -21.8 KB image.png
Binary file not shown.
@@ -1,57 +1,87 @@
import sqlite3 as sql
from datetime import datetime

"""Validation for fields"""

database_user = "app/databases/users.db" # Taking the databases
database_locations = 'app/databases/locations.db'
database_user = "app/databases/users.db"
database_locations = "app/databases/locations.db"


def create():
"""
Creates a user database if it doesn't exist
"""
with sql.connect(database_user) as cur:
try: # Tries the sql table
cur.execute("CREATE TABLE UserDatabase(username VARCHAR2(20), password VARCHAR2(20), realname VARCHAR2(20), dob VARCHAR2(20), weight VARCHAR2(20));")
try:
cur.execute("""
CREATE TABLE UserDatabase(username VARCHAR2(20),
password VARCHAR2(20),
realname VARCHAR2(20),
dob VARCHAR2(20),
color VARCHAR2(20),
weight VARCHAR2(20));
""")
except:
pass

def user_exists(username): # Checks for username

def user_exists(username):
"""
Returns true or false depending on if the user exists
"""
with sql.connect(database_user) as cur:
com = f"SELECT count(*) FROM UserDatabase WHERE username='{username}';"
print(com)
res = cur.execute(com).fetchone()[0]
if res == 0:
res = cur.execute(f"""
SELECT count(*)
FROM UserDatabase
WHERE username='{username}';
""")
if res.fetchone()[0] == 0:
return False
else:
return True

def tid_exists(username): # Checks for tid
def tid_exists(username):
"""
Returns true or false depending on if there is any data
for that username in the database
"""
with sql.connect(database_locations) as cur:
com = f"SELECT count(*) FROM Location WHERE tid='{username}';"
print(com)
res = cur.execute(com).fetchone()[0]
if res == 0:
res = cur.execute(f"""
SELECT count(*)
FROM Location
WHERE tid='{username}';
""")
if res.fetchone()[0] == 0:
return False
else:
return True

def compare(username, value_in, value_type): # Comparing
def compare(username, value_in, value_type):
"""
Takes in username, value and type and checks if they match with
the ones already saved in the database
"""
with sql.connect(database_user) as cur:
com = f"SELECT {value_type} FROM UserDatabase WHERE username='{username}';"
print(com)
res = cur.execute(com).fetchone()[0]
print(f"comparing - {value_in} == {res}")
if value_in == res:
res = cur.execute(f"""
SELECT {value_type}
FROM UserDatabase
WHERE username='{username}';
""")
if value_in == res.fetchone()[0]:
return True
else:
return False

def check_month(month, day): # Checks the month and day

def check_month(month, day):
"""
Takes in month and day and checks if the day
is valid for that month
"""
if month in [1, 3, 5, 7, 8, 10, 12]:
if (day > 0 and day <= 31):
return True
else:
return False
elif month == 2:
if (day > 0 and day <= 28): # Checking February
if (day > 0 and day <= 28):
return True
else:
return False
@@ -61,7 +91,12 @@ def check_month(month, day): # Ch
else:
return False

def check_date(day, month, year): # Checks full date

def check_date(day, month, year):
"""
Takes in a date and checks if it's in the future and then calls
check_month to see if the month/day are valid
"""
today = datetime.today().strftime('%Y-%m-%d').split("-")
if int(year) > int(today[0]):
return False
@@ -1,17 +1,25 @@
import sqlite3 as sql
from app.functions.auth import base # Imports base for later use
from app.functions.auth import base


database_user = "app/databases/users.db"

def change_password(data): # Changing password

def change_password(data):
"""
Function to allow password change
"""
base.create()
_status = check_pass_data(data)
if _status == "success":
new_password(data)
update_password(data)
return _status

def check_pass_data(data): # Checking password data


def check_pass_data(data):
"""
Function to check password data is valid
"""
_status = check_empty(data)
if _status == "ok":
if base.tid_exists(data['username']):
@@ -26,7 +34,11 @@ def check_pass_data(data): # Checki
return _status


def check_empty(data): # Checks the entry to see if empty

def check_empty(data):
"""
Function to if the fields are empty
"""
if data['username'] == "":
return "empty_id"
elif data['day'] == "0":
@@ -42,12 +54,19 @@ def check_empty(data): # Checks
else:
return "ok"

def new_password(data): # Updates database with new password


def update_password(data):
"""
Function to update new password in the database
"""
con = sql.connect(database_user)
cur = con.cursor()
com = f"UPDATE UserDatabase SET password='{data['password']}' WHERE username='{data['username']}';"
print(com)
cur.execute(com)
cur.execute(f"""
UPDATE UserDatabase
SET password='{data['password']}'
WHERE username='{data['username']}';
""")
con.commit()
cur.close()
con.close()
@@ -1,25 +1,34 @@
import sqlite3 as sql # Imports sql for use within database
import sqlite3 as sql
from app.functions.auth import base


database_user = "app/databases/users.db"

def create_profile(data): # Creating new data for a new user

def create_profile(data):
"""
Function to create new profile
"""
base.create()
_status = check_prof_data(data)
if _status == "success":
make_user(data)
return _status

def check_prof_data(data): # Checking new
print(data)


def check_prof_data(data):
"""
Function to the validity of profile data
"""
_status = check_empty(data)
if _status == "ok":
if base.tid_exists(data['username']):
if not base.user_exists(data['username']):
if not (data['password'] == data['r_password']):
return "pass_no_match"
elif not base.check_date(data['day'], data['month'], data['year']):
elif not base.check_date(data['day'],
data['month'],
data['year']):
return "wrong_date"
else:
return "success"
@@ -29,7 +38,11 @@ def check_prof_data(data): # Ch
return "no_id"
return _status


def check_empty(data):
"""
Function to check if the fields are filled
"""
if data['username'] == "":
return "empty_id"
elif data['realname'] == "":
@@ -49,13 +62,24 @@ def check_empty(data):
else:
return "ok"



def make_user(data):
"""
Function to add new user to the database
"""
con = sql.connect(database_user)
cur = con.cursor()
_date = f"{data['day']}-{data['month']}-{data['year']}"
com = f"INSERT INTO UserDatabase values('{data['username']}','{data['password']}','{data['realname']}','{_date}','{data['weight']}');"
print(com)
cur.execute(com)
cur.execute(f"""
INSERT INTO UserDatabase
values('{data['username']}',
'{data['password']}',
'{data['realname']}',
'{_date}',
'{data['color']}',
'{data['weight']}');
""")
con.commit()
cur.close()
con.close()
@@ -3,24 +3,38 @@ from app.functions.auth import base


def login(data):
"""
Function to start to start the login
"""
base.create()
l_status = check_login_data(data)
return l_status



def check_login_data(data):
"""
Function to check the login data
"""
_status = check_empty(data)
if _status == "ok":
if data['username'] == "admin":
return "success"
if not base.user_exists(data['username']):
return "no_id"
if not base.compare(data['username'], data['password'], "password"):
if not base.compare(data['username'],
data['password'],
"password"):
return "wrong_pass"
return "success"
else:
return _status


def check_empty(data):
"""
Function to check whether the fields are filled
"""
if data['username'] == "":
return "empty_id"
elif data['password'] == "":

0 comments on commit 7487bc3

Please sign in to comment.