commit 10232e73fd92d8f9265bd2253817f18f464815a8 Author: wangjiacai Date: Fri Mar 31 22:11:34 2023 +0800 init commit. diff --git a/project/__init__.py b/project/__init__.py new file mode 100644 index 0000000..3febd32 --- /dev/null +++ b/project/__init__.py @@ -0,0 +1,40 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_login import LoginManager + +# init SQLAlchemy so we can use it later in our models +db = SQLAlchemy() + + +def create_app(): + app = Flask(__name__) + + app.config['SECRET_KEY'] = 'secret-key-goes-here' + app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sqlite.db' + + db.init_app(app) + login_manager = LoginManager() + login_manager.login_view = 'auth.login' + login_manager.init_app(app) + + from .models import User + + @login_manager.user_loader + def load_user(user_id): + # since the user_id is just the primary key of our user table, use it in the query for the user + return User.query.get(int(user_id)) + + from . import models + + with app.app_context(): + db.create_all() + + # blueprint for auth routes in our app + from .auth import auth as auth_blueprint + app.register_blueprint(auth_blueprint) + + # blueprint for non-auth parts of app + from .main import main as main_blueprint + app.register_blueprint(main_blueprint) + + return app diff --git a/project/auth.py b/project/auth.py new file mode 100644 index 0000000..b987c8c --- /dev/null +++ b/project/auth.py @@ -0,0 +1,77 @@ +from flask_login import login_user, logout_user +from flask import Blueprint, render_template, redirect, url_for, request, flash +from werkzeug.security import generate_password_hash, check_password_hash +from flask_login import login_required, current_user, login_manager +from .models import User +from . import db + +auth = Blueprint('auth', __name__) + + +@auth.route('/login') +def login(): + return render_template('login.html') + + +@auth.route('/login', methods=['POST']) +def login_post(): + # login code goes here + email = request.form.get('email') + password = request.form.get('password') + remember = True if request.form.get('remember') else False + + user = User.query.filter_by(email=email).first() + + # check if the user actually exists + # take the user-supplied password, hash it, and compare it to the hashed password in the database + if not user or not check_password_hash(user.password, password): + flash('Please check your login details and try again.') + # if the user doesn't exist or password is wrong, reload the page + return redirect(url_for('auth.login')) + + # if the above check passes, then we know the user has the right credentials + login_user(user, remember=remember) + return redirect(url_for('main.profile')) + + +@auth.route('/signup') +def signup(): + return render_template('signup.html') + + +@auth.route('/signup', methods=['POST']) +def signup_post(): + # code to validate and add user to database goes here + email = request.form.get('email') + name = request.form.get('name') + password = request.form.get('password') + # if this returns a user, then the email already exists in database + user = User.query.filter_by(email=email).first() + + if user: # if a user is found, we want to redirect back to signup page so user can try again + flash('此邮箱已注册!') + return redirect(url_for('auth.signup')) + if not (email): + flash('Email missing!') + return redirect(url_for('auth.signup')) + if not (name): + name = email + if not (password): + flash('Password missing!') + return redirect(url_for('auth.signup')) + # create a new user with the form data. Hash the password so the plaintext version isn't saved. + new_user = User(email=email, name=name, + password=generate_password_hash(password, method='sha256'), role='user', isActivated=False) + + # add the new user to the database + db.session.add(new_user) + db.session.commit() + return redirect(url_for('auth.login')) + + +@auth.route('/logout') +def logout(): + if current_user.is_authenticated: + logout_user() + return redirect(url_for('main.index')) + diff --git a/project/main.py b/project/main.py new file mode 100644 index 0000000..a5f087c --- /dev/null +++ b/project/main.py @@ -0,0 +1,21 @@ +from flask import Blueprint, render_template +from flask_login import login_required, current_user, login_manager +from . import db + +main = Blueprint('main', __name__) + + +@main.route('/') +def index(): + if current_user.is_authenticated: + name = current_user.name + else: + name = '游客' + return render_template('index.html', username=name, is_authenticated=current_user.is_authenticated) + + +@main.route('/profile') +@login_required +def profile(): + login_manager.login_message = "请先登录" + return render_template('profile.html', username=current_user.name, isActivated=current_user.isActivated) diff --git a/project/models.py b/project/models.py new file mode 100644 index 0000000..c2e1dc0 --- /dev/null +++ b/project/models.py @@ -0,0 +1,12 @@ +from flask_login import UserMixin +from . import db + + +class User(UserMixin, db.Model): + # primary keys are required by SQLAlchemy + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(100), unique=True, nullable=False) + password = db.Column(db.String(100), nullable=False) + name = db.Column(db.String(100), nullable=False) + role = db.Column(db.String(100), nullable=False) + isActivated = db.Column(db.Boolean, nullable=False) diff --git a/project/templates/base.html b/project/templates/base.html new file mode 100644 index 0000000..10e89e9 --- /dev/null +++ b/project/templates/base.html @@ -0,0 +1,47 @@ + + + + + + + + Flask + + + + + + + + + +
+
+ {% block content %} + {% endblock %} +
+
+ + + \ No newline at end of file diff --git a/project/templates/index.html b/project/templates/index.html new file mode 100644 index 0000000..45ac53e --- /dev/null +++ b/project/templates/index.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block content %} + +

+ 你好, {{ username }}。 + {% if not is_authenticated %} +

+ + + {% endif %} +

+{% endblock %} \ No newline at end of file diff --git a/project/templates/login.html b/project/templates/login.html new file mode 100644 index 0000000..54827e2 --- /dev/null +++ b/project/templates/login.html @@ -0,0 +1,36 @@ +{% extends "base.html" %} + +{% block content %} +
+

登录

+
+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ {{ messages[0] }} +
+ {% endif %} + {% endwith %} +
+
+
+ +
+
+ +
+
+ +
+
+
+ +
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/project/templates/profile.html b/project/templates/profile.html new file mode 100644 index 0000000..76ab72f --- /dev/null +++ b/project/templates/profile.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block content %} +

+ 欢迎回来, {{ username }}! +

+{% if isActivated %} +开始聊天吧 +{% else %} +您的账号暂未激活,请等待管理员激活此账号。 +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/project/templates/signup.html b/project/templates/signup.html new file mode 100644 index 0000000..bfb69a6 --- /dev/null +++ b/project/templates/signup.html @@ -0,0 +1,41 @@ +{% extends "base.html" %} + +{% block content %} +
+

注册

+
+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ {{ messages[0] }} + {% if messages[0] == '此邮箱已注册!' %} + 点此登录. + {% endif %} +
+ {% endif %} + {% endwith %} +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+
+
+{% endblock %} \ No newline at end of file