From db7a41c898d8f86431b04977ec8a468a476681f7 Mon Sep 17 00:00:00 2001 From: Adnan Memic Date: Thu, 5 Mar 2020 22:42:46 +0000 Subject: [PATCH] profile working --- location_app/app/functions/auth/base.py | 77 +++++++++++++++++++ .../app/functions/auth/change_password.py | 52 +++++++++++++ .../app/functions/auth/create_profile.py | 77 +++++++++++++++++++ location_app/app/functions/auth/login.py | 32 ++++++++ .../app/functions/auth/password_check.py | 70 ----------------- .../app/functions/data_tools/data_getter.py | 20 ++--- location_app/app/mqtt/mqtt_message_handler.py | 13 ++-- location_app/app/routes/login.py | 34 ++++++-- location_app/app/routes/main.py | 1 - location_app/app/static/css/login/forgot.css | 14 +++- location_app/app/static/css/login/index.css | 5 ++ location_app/app/static/js/map/test.js | 12 +-- location_app/app/templates/base.html | 1 - location_app/app/templates/login/create.html | 23 +++++- location_app/app/templates/login/forgot.html | 23 +++++- location_app/app/templates/login/index.html | 6 ++ 16 files changed, 355 insertions(+), 105 deletions(-) create mode 100644 location_app/app/functions/auth/base.py create mode 100644 location_app/app/functions/auth/change_password.py create mode 100644 location_app/app/functions/auth/create_profile.py create mode 100644 location_app/app/functions/auth/login.py delete mode 100644 location_app/app/functions/auth/password_check.py diff --git a/location_app/app/functions/auth/base.py b/location_app/app/functions/auth/base.py new file mode 100644 index 0000000..081c942 --- /dev/null +++ b/location_app/app/functions/auth/base.py @@ -0,0 +1,77 @@ +import sqlite3 as sql +from datetime import datetime + + +database_user = "app/databases/users.db" +database_locations = 'app/databases/locations.db' + +def create(): + with sql.connect(database_user) as cur: + try: + cur.execute("CREATE TABLE UserDatabase(username VARCHAR2(20), password VARCHAR2(20), realname VARCHAR2(20), gender VARCHAR2(20), dob VARCHAR2(20), height VARCHAR2(20), weight VARCHAR2(20));") + except: + pass + +def user_exists(username): + 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: + return False + else: + return True + +def tid_exists(username): + 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: + return False + else: + return True + +def compare(username, value_in, value_type): + 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: + return True + else: + return False + +def check_month(month, day): + 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): + return True + else: + return False + else: + if (day > 0 and day <= 30): + return True + else: + return False + +def check_date(day, month, year): + today = datetime.today().strftime('%Y-%m-%d').split("-") + if int(year) > int(today[0]): + return False + elif int(year) == int(today[0]): + if int(month) >= int(today[1]): + return False + else: + if int(day) >= int(today[2]): + return False + else: + return check_month(int(today[1]), int(today[2])) + else: + return check_month(int(month), int(day)) + \ No newline at end of file diff --git a/location_app/app/functions/auth/change_password.py b/location_app/app/functions/auth/change_password.py new file mode 100644 index 0000000..7c62b87 --- /dev/null +++ b/location_app/app/functions/auth/change_password.py @@ -0,0 +1,52 @@ +import sqlite3 as sql +from app.functions.auth import base + + +database_user = "app/databases/users.db" +database_locations = 'app/databases/locations.db' + +def change_password(data): + base.create() + _status = check_pass_data(data) + if _status == "success": + new_password(data) + return _status + +def check_pass_data(data): + _status = check_empty(data) + if _status == "ok": + if base.tid_exists(data['username']): + _date = f"{data['day']}-{data['month']}-{data['year']}" + if not base.compare(data['username'], _date, "dob"): + return "wrong_date" + if not (data['password'] == data['r_password']): + return "pass_no_match" + return "success" + return "no_user" + else: + return _status + + +def check_empty(data): + if data['username'] == "": + return "empty_id" + elif data['day'] == "0": + return "empty_day" + elif data['month'] == "0": + return "empty_month" + elif data['year'] == "0": + return "empty_year" + elif data['password'] == "": + return "empty_pass" + else: + return "ok" + +def new_password(data): + 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) + con.commit() + cur.close() + con.close() \ No newline at end of file diff --git a/location_app/app/functions/auth/create_profile.py b/location_app/app/functions/auth/create_profile.py new file mode 100644 index 0000000..15b95c9 --- /dev/null +++ b/location_app/app/functions/auth/create_profile.py @@ -0,0 +1,77 @@ +import sqlite3 as sql +from app.functions.auth import base + + +database_user = "app/databases/users.db" +database_locations = 'app/databases/locations.db' + +def create_profile(data): + base.create() + _status = check_prof_data(data) + if _status == "success": + make_user(data) + return _status + +def check_prof_data(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']): + return "bad_date" + else: + return "success" + else: + return "user_exists" + else: + return "no_id" + return _status + +def check_empty(data): + if data['username'] == "": + return "empty_id" + elif data['realname'] == "": + return "empty_name" + elif data['day'] == "0": + return "empty_day" + elif data['month'] == "0": + return "empty_month" + elif data['year'] == "0": + return "empty_year" + elif data['gender'] == "": + return "empty_gender" + elif data['height'] == "": + return "empty_height" + elif data['weight'] == "": + return "empty_weight" + elif data['password'] == "": + return "empty_pass" + else: + return "ok" + +def make_user(data): + 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']}','{data['gender']}','{_date}','{data['height']}','{data['weight']}');" + print(com) + cur.execute(com) + con.commit() + cur.close() + con.close() + + +# ImmutableMultiDict([ +# ('username', '69'), +# ('realname', 'Adi'), +# ('day', '23'), +# ('month', '1'), +# ('year', '1998'), +# ('gender', 'male'), +# ('height', '183'), +# ('weight', '75'), +# ('password', 'fisgib-duwxut-4Rowhi'), +# ('r_password', 'fisgib-duwxut-4Rowhi')]) + diff --git a/location_app/app/functions/auth/login.py b/location_app/app/functions/auth/login.py new file mode 100644 index 0000000..ec5c6f5 --- /dev/null +++ b/location_app/app/functions/auth/login.py @@ -0,0 +1,32 @@ +import sqlite3 as sql +from app.functions.auth import base + + +database_user = "app/databases/users.db" +database_locations = 'app/databases/locations.db' + +def login(data): + base.create() + l_status = check_login_data(data) + return l_status + +def check_login_data(data): + _status = check_empty(data) + if _status == "ok": + if data['username'] == "admin": + return "success" + if not base.user_exists(data['username']): + return "no_user" + if not base.compare(data['username'], data['password'], "password"): + return "wrong_password" + return "success" + else: + return _status + +def check_empty(data): + if data['username'] == "": + return "empty_id" + elif data['password'] == "": + return "empty_pass" + else: + return "ok" \ No newline at end of file diff --git a/location_app/app/functions/auth/password_check.py b/location_app/app/functions/auth/password_check.py deleted file mode 100644 index be9a672..0000000 --- a/location_app/app/functions/auth/password_check.py +++ /dev/null @@ -1,70 +0,0 @@ -import sqlite3 as sql - - -database_user = "app/databases/users.db" -database_locations = 'app/databases/locations.db' - -def if_user_exists(username): - print(f"checked for existance - {username}") - with sql.connect(database_user) as cur: - res = cur.execute(f"SELECT count(*) FROM UserDatabase WHERE username='{username}';").fetchone()[0] - if res == 0: - return False - else: - return True - -def if_tid_exists(username): - print(f"checked for tid existance - {username}") - with sql.connect(database_locations) as cur: - res = cur.execute(f"SELECT count(*) FROM Location WHERE tid='{username}';").fetchone()[0] - if res == 0: - return False - else: - return True - -def password_for(username, password): - print(f"checked password - {username}") - with sql.connect(database_user) as cur: - res = cur.execute(f"SELECT password FROM UserDatabase WHERE username='{username}';").fetchone()[0] - if password == res: - return True - else: - return False - -def make_user(username, password): - print(f"created a new user - {username}") - con = sql.connect(database_user) - cur = con.cursor() - cur.execute(f"INSERT INTO UserDatabase values('{username}','{password}');") - con.commit() - cur.close() - con.close() - -def change_password(username, new_password): - print(f"created a new password - {username}") - con = sql.connect(database_user) - cur = con.cursor() - cur.execute(f"UPDATE UserDatabase SET password='{new_password}' WHERE username='{username}';") - con.commit() - cur.close() - con.close() - -def username_and_password(username, password): - with sql.connect(database_user) as cur: - try: - cur.execute("CREATE TABLE UserDatabase(username VARCHAR2(20), password VARCHAR2(20));") - except: - pass - if username == "admin" and password == "admin": - return "L" - if if_user_exists(username): - if password_for(username, password): - return "L" - else: - return "WP" - else: - if if_tid_exists(username): - make_user(username, password) - return "L" - else: - return "NO" \ No newline at end of file diff --git a/location_app/app/functions/data_tools/data_getter.py b/location_app/app/functions/data_tools/data_getter.py index 985c4bc..a7d63ea 100644 --- a/location_app/app/functions/data_tools/data_getter.py +++ b/location_app/app/functions/data_tools/data_getter.py @@ -49,11 +49,11 @@ def get_locations_for(username): locations = [] with sql.connect(database_locations) as cur: if username == 'admin': - res = cur.execute(f"SELECT DISTINCT * From Location ORDER BY tid, date, time;") + res = cur.execute(f"SELECT DISTINCT * From Location ORDER BY tid, tst DESC;") else: - res = cur.execute(f"SELECT DISTINCT * From Location WHERE tid='{username}' ORDER BY date, time;") - for tid, batt, lon, lat, city, road, date, time, in res: - locations.append([tid, batt, lon, lat, city, road, date, time]) + res = cur.execute(f"SELECT DISTINCT * From Location WHERE tid='{username}' ORDER BY tst DESC;") + for tid, batt, lon, lat, city, road, _date, _time, tst, in res: + locations.append([tid, batt, lon, lat, city, road, _date, _time]) return locations @@ -61,14 +61,16 @@ def get_locations_for(username): def get_map_locations_for(username): locations = [] with sql.connect(database_locations) as cur: - res = cur.execute(f"SELECT DISTINCT tid, longitude, latitude, date, time From Location WHERE tid='{username}' ORDER BY tid, date, time;") - for tid, lon, lat, date, time, in res: - locations.append([tid, lon, lat, date, time]) + res = cur.execute(f"SELECT DISTINCT longitude, latitude, date, time From Location WHERE tid='{username}' ORDER BY tst DESC;") + for lon, lat, _date, _time, in res: + locations.append([lon, lat, _date, _time]) return locations def get_map_location_dates(username): + date_list = [] with sql.connect(database_locations) as cur: - res = cur.execute(f"SELECT DISTINCT date From Location WHERE tid='{username}' ORDER BY date;") - date_list = list(map(lambda x: x[0], res)) + res = cur.execute(f"SELECT DISTINCT date From Location WHERE tid='{username}' ORDER BY tst DESC;") + for _date, in res: + date_list.append(_date) return date_list diff --git a/location_app/app/mqtt/mqtt_message_handler.py b/location_app/app/mqtt/mqtt_message_handler.py index 23a3f44..786cd25 100644 --- a/location_app/app/mqtt/mqtt_message_handler.py +++ b/location_app/app/mqtt/mqtt_message_handler.py @@ -10,7 +10,7 @@ def getMsg(msg): cur = con.cursor() geolocator = Nominatim(user_agent="Web_app") try: - cur.execute("CREATE TABLE Location(tid VARCHAR2(2), battery INT(3), longitude NUMBER(10,6), latitude NUMBER(10,6), city VARCHAR2(20), road VARCHAR2(30), date VARCHAR2(15), time VARCHAR2(15));") + cur.execute("CREATE TABLE Location(tid VARCHAR2(2), battery INT(3), longitude NUMBER(10,6), latitude NUMBER(10,6), city VARCHAR2(20), road VARCHAR2(30), date VARCHAR2(20), time VARCHAR2(20), tst INT(15));") except: pass data = json.loads(msg.payload.decode("utf8")) @@ -21,11 +21,12 @@ def getMsg(msg): location = geolocator.reverse(f"{data['lat']},{data['lon']}") city = location.raw["address"]["city"] road = location.raw["address"]["road"] - rtst = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data["tst"])).split(" ") - - print(rtst, ' ---- ', tid) - - cur.execute(f"INSERT INTO Location values('{tid}','{batt}','{lon}','{lat}','{city}','{road}','{rtst[0]}','{rtst[1]}');") + tst = data["tst"] + _time = time.strftime('%H:%M:%S', time.localtime(tst)) + _date = time.strftime('%d-%m-%Y', time.localtime(tst)) + com = f"INSERT INTO Location values('{tid}','{batt}','{lon}','{lat}','{city}','{road}','{_date}','{_time}','{tst}');" + print(com) + cur.execute(com) con.commit() cur.close() diff --git a/location_app/app/routes/login.py b/location_app/app/routes/login.py index a8dca73..9d61769 100644 --- a/location_app/app/routes/login.py +++ b/location_app/app/routes/login.py @@ -1,24 +1,44 @@ from flask import Blueprint, render_template, request, url_for, escape, redirect, session -from app.functions.data_tools import data_getter +from app.functions.auth.login import login +from app.functions.auth.create_profile import create_profile +from app.functions.auth.change_password import change_password from datetime import datetime login_bl = Blueprint('login', __name__) +DAYS = [i for i in range(1,32)] +MONTHS = [("January", 1), ("February", 2), ("March", 3), ("April", 4), ("May", 5), ("June", 6), ("July", 7), ("August", 8), ("September", 9), ("October", 10), ("November", 11), ("December", 12)] +YEARS = list(reversed([i for i in range(1900, (int(datetime.today().strftime('%Y')) + 1))])) + @login_bl.route("/", methods = ["GET", "POST"]) def index(): if request.method == "POST": - print(request.form) - return render_template("login/index.html") + _status = login(request.form) + if _status == "success": + session["username"] = request.form["username"] + return redirect(url_for('main.index')) + else: + return render_template("login/index.html", status = _status) + return render_template("login/index.html", status = "0") + @login_bl.route("/create_account", methods = ["GET", "POST"]) def create_account(): if request.method == "POST": - print(request.form) - return render_template("login/create.html", today = datetime.today().strftime('%Y-%m-%d')) + _status = create_profile(request.form) + if _status == "success": + return redirect(url_for('login.index')) + else: + return render_template("login/create.html", status = _status, days = DAYS, months = MONTHS, years = YEARS) + return render_template("login/create.html", status = "0", days = DAYS, months = MONTHS, years = YEARS) @login_bl.route("/forgot_password", methods = ["GET", "POST"]) def forgot_password(): if request.method == "POST": - print(request.form) - return render_template("login/forgot.html") \ No newline at end of file + _status = change_password(request.form) + if _status == "success": + return redirect(url_for('login.index')) + else: + return render_template("login/forgot.html", status = _status, days = DAYS, months = MONTHS, years = YEARS) + return render_template("login/forgot.html", status = "0", days = DAYS, months = MONTHS, years = YEARS) \ No newline at end of file diff --git a/location_app/app/routes/main.py b/location_app/app/routes/main.py index caf921c..d29323c 100644 --- a/location_app/app/routes/main.py +++ b/location_app/app/routes/main.py @@ -5,7 +5,6 @@ main_bl = Blueprint('main', __name__) @main_bl.route("/") def index(): - session['username'] = "JC" return render_template("main/index.html", locations = data_getter.get_locations_for(session['username'])) @main_bl.route("/logout") diff --git a/location_app/app/static/css/login/forgot.css b/location_app/app/static/css/login/forgot.css index 6b2b847..dcaa9f3 100644 --- a/location_app/app/static/css/login/forgot.css +++ b/location_app/app/static/css/login/forgot.css @@ -52,11 +52,13 @@ justify-content: start; align-content: center; grid-template-columns: 2fr 2fr; - grid-template-rows: 0.5fr 1.5fr 0.5fr 1.5fr 1.5fr 1.5fr; + grid-template-rows: 0.5fr 1.5fr 0.5fr 1.5fr 0.5fr 1.5fr 1.5fr 1fr; grid-template-areas: '. .' 'user user' '. .' + 'dob dob' + '. .' 'pass pass' 'r_pass r_pass' '. forgot_button'; @@ -71,6 +73,16 @@ margin-bottom: 1px; font-size: 2vw; } +.dob { + display: grid; + justify-content: center stretch; + align-content: center; + grid-area: dob; +} +.dob_text { + margin-bottom: 1px; + font-size: 2vw; +} .password { display: grid; justify-content: center stretch; diff --git a/location_app/app/static/css/login/index.css b/location_app/app/static/css/login/index.css index 3f2c1dd..d52d2d6 100644 --- a/location_app/app/static/css/login/index.css +++ b/location_app/app/static/css/login/index.css @@ -110,6 +110,11 @@ .cacc_text { font-size: 2vw; } +.error_text { + color: red; + font-size: 1.5vw; + padding-top: 5px; +} input { width: 100%; font-size: 2vw; diff --git a/location_app/app/static/js/map/test.js b/location_app/app/static/js/map/test.js index 9aef96b..91e288a 100644 --- a/location_app/app/static/js/map/test.js +++ b/location_app/app/static/js/map/test.js @@ -43,7 +43,7 @@ function get_map_for_date(date) { reset_map_layers(); var this_date_loc = []; for(i = 0; i < locations.length; i++) { - if(locations[i][3] == date) { + if(locations[i][2] == date) { this_date_loc.push(locations[i]); } } @@ -67,8 +67,8 @@ function get_map_for_date(date) { function get_average(array_in) { var center = [0, 0]; for(i = 0; i < array_in.length; i++) { - center[0] += array_in[i][1]; - center[1] += array_in[i][2]; + center[0] += array_in[i][0]; + center[1] += array_in[i][1]; } return [center[0] / array_in.length, center[1] / array_in.length] } @@ -77,7 +77,7 @@ function add_map_point(location, color = '#ff0000') { var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [new ol.Feature({ - geometry: new ol.geom.Point(ol.proj.fromLonLat([location[1], location[2]])), + geometry: new ol.geom.Point(ol.proj.fromLonLat([location[0], location[1]])), })] }), style: new ol.style.Style({ @@ -95,8 +95,8 @@ function add_map_point(location, color = '#ff0000') { } function add_map_lines(loc1, loc2) { - var pos1 = ol.proj.fromLonLat([loc1[1], loc1[2]]); - var pos2 = ol.proj.fromLonLat([loc2[1], loc2[2]]); + var pos1 = ol.proj.fromLonLat([loc1[0], loc1[1]]); + var pos2 = ol.proj.fromLonLat([loc2[0], loc2[1]]); var lineStyle = [ new ol.style.Style({ stroke: new ol.style.Stroke({ diff --git a/location_app/app/templates/base.html b/location_app/app/templates/base.html index 8f187ec..189b647 100644 --- a/location_app/app/templates/base.html +++ b/location_app/app/templates/base.html @@ -9,7 +9,6 @@ {% block basejs %}{% endblock %} {% block title %}{% endblock %} - {% block basecss %}{% endblock %} diff --git a/location_app/app/templates/login/create.html b/location_app/app/templates/login/create.html index 7bd0a74..8370111 100644 --- a/location_app/app/templates/login/create.html +++ b/location_app/app/templates/login/create.html @@ -20,7 +20,7 @@

Trackmaster

-

Create Account

+

Create Account - {{status}}

@@ -34,8 +34,25 @@
-

DOB (yyyy-mm-dd):


- +

Birthday:


+ + +

Gender:


diff --git a/location_app/app/templates/login/forgot.html b/location_app/app/templates/login/forgot.html index a31cff5..5884067 100644 --- a/location_app/app/templates/login/forgot.html +++ b/location_app/app/templates/login/forgot.html @@ -20,7 +20,7 @@

Trackmaster

-

Forgot Password

+

Forgot Password - {{status}}

@@ -29,6 +29,27 @@

ID:


+
+

Birthday:


+ + + +

New Password:


diff --git a/location_app/app/templates/login/index.html b/location_app/app/templates/login/index.html index 095da58..b54fe75 100644 --- a/location_app/app/templates/login/index.html +++ b/location_app/app/templates/login/index.html @@ -28,10 +28,16 @@

ID:


+ {% if status == 'no_user' %} +

Wrong ID

+ {% endif %}

Password:


+ {% if status == 'wrong_password' %} +

Wrong password

+ {% endif %}