From 60f3fd5c0917192412f57835ba7a1b01b5c0b143 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 22:45:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=20=D1=81?= =?UTF-8?q?=D0=B5=D1=81=D1=81=D0=B8=D0=B8=20=D0=B8=20=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- __init__.py | 27 ++++++++++++------------ db_routing.py | 42 ++++++++++++++++++++++--------------- templates/index.html | 18 +++++++--------- templates/registration.html | 4 ++-- 5 files changed, 51 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 8406286..3de488a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ./idea ./venv -./vscode \ No newline at end of file +./vscode +./data.db \ No newline at end of file diff --git a/__init__.py b/__init__.py index a26628f..2eee348 100644 --- a/__init__.py +++ b/__init__.py @@ -9,9 +9,9 @@ login_manager = LoginManager(app) @login_manager.user_loader -def load_user(user): - user_id = user.UserID - return user_id +def load_user(user_id): + userID = db_routing.find_user(id=user_id).UserID + return userID @app.route('/', methods=['GET']) @@ -22,21 +22,22 @@ def index(): @app.route('/registration', methods=['GET', 'POST']) def register(): if request.method == 'POST': - userLogin = request.form['RegUserLogin'] + userName = request.form['RegUserLogin'] userPassw = request.form['RegUserPassw'] - if db_routing.add_user(userLogin, passw_hash(userPassw)): - return redirect('/content') + if db_routing.add_user(userName, passw_hash(userPassw)): + login_user(db_routing.find_user(username=userName)) + return redirect('content.html') return render_template('registration.html') @app.route('/login', methods=['POST']) def login(): - userLogin = request.form['LogUserLogin'] + userName = request.form['LogUserLogin'] userPassw = request.form['LogUserPassw'] - user = verify_password(userLogin, userPassw) + user = verify_password(userName, userPassw) if user: login_user(user) - return redirect('/content') + return render_template('content.html') @app.route('/content', methods=['GET']) @@ -58,11 +59,11 @@ def passw_hash(user_passw, salt=os.urandom(32)): return storage -def verify_password(user_login, user_passw): - User = db_routing.find_user(user_login) +def verify_password(username, password): + User = db_routing.find_user(username=username) if User: - userSalt = User.UserPassw[:32] - if passw_hash(user_passw, userSalt) == User.UserPassw: + userSalt = User.password[:32] + if passw_hash(password, userSalt) == User.password: return User else: print('Неверный пароль') diff --git a/db_routing.py b/db_routing.py index b60c34a..2285fc8 100644 --- a/db_routing.py +++ b/db_routing.py @@ -1,43 +1,48 @@ from flask_sqlalchemy import SQLAlchemy from flask import Flask - +from flask_login import UserMixin app = Flask('Jarvis', static_folder='static', template_folder='templates') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' +app.config['SECRET_KEY'] = 'Radius' db = SQLAlchemy(app) -class User(db.Model): +class User(UserMixin, db.Model): __tablename__ = 'Users' - UserID = db.Column(db.Integer, primary_key=True) - UserName = db.Column(db.String(80), unique=True, nullable=False) - UserPassw = db.Column(db.String(120), nullable=False) + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(80), unique=True, nullable=False) + password = db.Column(db.String(120), nullable=False) + + def __init__(self, username, password): + self.username = username + self.password = password class Trigger(db.Model): __tablename__ = 'Triggers' - TriggerID = db.Column(db.Integer, primary_key=True) - TriggerName = db.Column(db.String(80), unique=True, nullable=False) - TriggerArgs = db.Column(db.String(200)) + id = db.Column(db.Integer, primary_key=True) + triggername = db.Column(db.String(80), unique=True, nullable=False) + triggerargs = db.Column(db.String(200)) class Action(db.Model): __tablename__ = 'Actions' - ActionID = db.Column(db.Integer, primary_key=True) - ActionName = db.Column(db.String(80), unique=True, nullable=False) - ActionArgs = db.Column(db.String(200)) + id = db.Column(db.Integer, primary_key=True) + actionname = db.Column(db.String(80), unique=True, nullable=False) + actionargs = db.Column(db.String(200)) class Scenario(db.Model): __tablename__ = 'Scenarios' - ScenarioID = db.Column(db.Integer, primary_key=True) - ScenarioTrigger = db.Column(db.Integer, nullable=False) - ScenarioAction = db.Column(db.Integer, nullable=False) + id = db.Column(db.Integer, primary_key=True) + scenariotrigger = db.Column(db.Integer, nullable=False) + scenarioaction = db.Column(db.Integer, nullable=False) def add_user(user_name, passw_hash): if not find_user(user_name): - new_user = User(UserName=user_name, UserPassw=passw_hash) + new_user = User(username=user_name, password=passw_hash) db.session.add(new_user) db.session.commit() return new_user @@ -46,5 +51,8 @@ def add_user(user_name, passw_hash): return False -def find_user(user_name): - return User.query.filter_by(UserName=user_name).first() +def find_user(id=None, username=None): + if id: + return User.query.filter_by(id=id).first() + if username: + return User.query.filter_by(username=username).first() diff --git a/templates/index.html b/templates/index.html index 297851c..6fa3768 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,22 +22,20 @@
-
Войти
+
Войти
@@ -63,7 +61,7 @@

Ваш индивидуальный помощник на каждый день

- +
diff --git a/templates/registration.html b/templates/registration.html index 708caba..081abd3 100644 --- a/templates/registration.html +++ b/templates/registration.html @@ -31,7 +31,7 @@ @@ -88,7 +88,7 @@
- +