From 1f4e0c11f066c700a78d9eb78a4390892572d192 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 20:12:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0?= =?UTF-8?q?=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B9=20=D0=B8=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 53 ++++++---- db_routing.py | 29 +++++- requirements.txt | 2 +- templates/index.html | 234 +++++++++++++++++++++---------------------- 4 files changed, 177 insertions(+), 141 deletions(-) diff --git a/__init__.py b/__init__.py index 2bc514c..a26628f 100644 --- a/__init__.py +++ b/__init__.py @@ -1,16 +1,17 @@ -from flask import Flask, render_template, request +from flask import render_template, request, redirect import db_routing -from flask_httpauth import HTTPBasicAuth +from flask_login import LoginManager, current_user, login_user, login_required +from db_routing import app, db import os import hashlib +login_manager = LoginManager(app) -if not os.path.exists('./data.db'): - db_routing.db.create_all() -app = Flask('Jarvis', static_folder='static', template_folder='templates') -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' -auth = HTTPBasicAuth() +@login_manager.user_loader +def load_user(user): + user_id = user.UserID + return user_id @app.route('/', methods=['GET']) @@ -21,26 +22,25 @@ def index(): @app.route('/registration', methods=['GET', 'POST']) def register(): if request.method == 'POST': - userLogin = request.form['UserLogin'] - userPassw = request.form['UserPassw'] + userLogin = request.form['RegUserLogin'] + userPassw = request.form['RegUserPassw'] if db_routing.add_user(userLogin, passw_hash(userPassw)): - return render_template('content.html') + return redirect('/content') return render_template('registration.html') -@auth.verify_password -def verify_password(user_login, user_passw): - User = db_routing.find_user(user_login) - if User: - userSalt = User.UserPassw[:32] - if passw_hash(user_passw, userSalt) == User.UserPassw: - return True - else: - return False +@app.route('/login', methods=['POST']) +def login(): + userLogin = request.form['LogUserLogin'] + userPassw = request.form['LogUserPassw'] + user = verify_password(userLogin, userPassw) + if user: + login_user(user) + return redirect('/content') @app.route('/content', methods=['GET']) -@auth.login_required +@login_required def content(): return render_template('content.html') @@ -58,5 +58,18 @@ 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) + if User: + userSalt = User.UserPassw[:32] + if passw_hash(user_passw, userSalt) == User.UserPassw: + return User + else: + print('Неверный пароль') + return False + + if __name__ == '__main__': + if not os.path.exists('./data.db'): + db.create_all() app.run() diff --git a/db_routing.py b/db_routing.py index 7862aab..b60c34a 100644 --- a/db_routing.py +++ b/db_routing.py @@ -1,23 +1,46 @@ from flask_sqlalchemy import SQLAlchemy -from __init__ import app +from flask import Flask +app = Flask('Jarvis', static_folder='static', template_folder='templates') +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' db = SQLAlchemy(app) class User(db.Model): - __tablename__ = 'users' + __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) +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)) + + +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)) + + +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) + + def add_user(user_name, passw_hash): if not find_user(user_name): new_user = User(UserName=user_name, UserPassw=passw_hash) db.session.add(new_user) db.session.commit() - return True + return new_user else: print('Логин занят') return False diff --git a/requirements.txt b/requirements.txt index 74fd393..81fdd30 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ Flask==1.1.1 Flask-HTTPAuth==3.3.0 Flask-SQLAlchemy==2.4.1 itsdangerous==1.1.0 -Jinja2==2.11.1 +Jinja2==3.0.0a1 MarkupSafe==1.1.1 SQLAlchemy==1.3.15 Werkzeug==1.0.0 diff --git a/templates/index.html b/templates/index.html index c0750a7..2790d55 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,118 +1,118 @@ - - - - - - - jarvis - - - - -
- -
-
- -
-
-
- Войти - -
-
-
-
-
- -
-
-
-
- -
-
-
- Jarvis -
-
-

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

-
-
- - - -
-
-
-
-
- - -
- - - - + + + + + + + jarvis + + + + +
+ +
+
+ +
+
+
+ Войти + +
+
+
+
+
+ +
+
+
+
+ +
+
+
+ Jarvis +
+
+

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

+
+
+ + + +
+
+
+
+
+ + +
+ + + + \ No newline at end of file