commit 6a52b0cba5654b97c3c975419abc7373df53fa32 Author: Samuel ORTION Date: Thu May 19 11:07:34 2022 +0200 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b9826 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv +config.py +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..be3d67c --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# BirdQuizz + +Identify Bird Song ! + + +# Installation + +```bash +git clone https://forge.chapril.org/UncleSamulus/BirdQuizz.git +cd BirdQuizz +python3 -m venv +. venv/bin/activate +pip install -r requirements.txt +``` + +## Database configuration + +```bash +sudo mysql +``` + +```sql +CREATE DATABASE birdquizz; +GRANT ALL ON birdquizz.* TO birdquizz@localhost IDENTIFIED BY 'secret'; +``` + +## Configuration + +```bash +cp config.py.example config.py +``` + +## Database migration + +```bash +python3 +``` + +```python3 +from app import * +app.app_context().push() +db.create_all() +``` \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..bba6710 --- /dev/null +++ b/app.py @@ -0,0 +1,68 @@ +from flask import Flask +from flask import render_template +from flask import session +from flask import request +from flask import redirect +from flask import url_for + +from werkzeug.security import generate_password_hash, check_password_hash + +from config import secret_key, database_uri +from model import db, User + +app = Flask(__name__) +app.secret_key = secret_key + +app.config['SQLALCHEMY_DATABASE_URI'] = database_uri +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +db.init_app(app) + +@app.route("/") +def home(): + if 'username' in session: + return render_template("index.html", username=session["username"]) + else: + return render_template("index.html") + +@app.route("/signup", methods=["GET", "POST"]) +def signup(): + if request.method == "POST": + username = request.form['username'] + email = request.form['email'] + password = request.form['password'] + registered_user = User.query.filter_by(username=username).first() + if registered_user is None: + password_hash = generate_password_hash(password) + registered_user = User(username=username, email=email, password=password_hash) + db.session.add(registered_user) + db.session.commit() + else: + return render_template("auth/signup.html", message="Username already used. Try with an other.") + return redirect(url_for("login")) + elif request.method == "GET": + return render_template("auth/signup.html") + +@app.route("/login", methods=["GET", "POST"]) +def login(): + if request.method == "POST": + username = request.form['username'] + password = request.form['password'] + user = User.query.filter_by(username=username).first() + if user is None: + return render_template("auth/login.html", message="No user with this username already registered") + else: + password_hash = user.password + if check_password_hash(password_hash, password): + session["username"] = username + return redirect(url_for("home")) + else: + return render_template("auth/login.html", message="Password incorrect. Try again") + if request.method == "GET": + return render_template("auth/login.html") + +@app.route("/logout") +def logout(): + # Remove username from the session if it's there + session.pop("username", None) + return redirect(url_for("home")) \ No newline at end of file diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..f91f0d4 --- /dev/null +++ b/config.py.example @@ -0,0 +1,7 @@ +secret_key = b'secret' + +db_user = "birdquizz" +db_pass = "secret" # change this +db_name = "birdquizz" + +database_uri = f"mysql+mysqlconnector://{db_user}:{db_pass}@localhost/{db_name}" \ No newline at end of file diff --git a/model.py b/model.py new file mode 100644 index 0000000..1f2e38b --- /dev/null +++ b/model.py @@ -0,0 +1,11 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +class User(db.Model): + __tablename__ = 'user' + + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(64), unique=True) + email = db.Column(db.String(64), unique=True, index=True) + password = db.Column(db.String(128)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..197bdb6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +click==8.1.3 +Flask==2.1.2 +Flask-SQLAlchemy==2.5.1 +greenlet==1.1.2 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +mysql-connector-python==8.0.29 +protobuf==3.20.1 +PyMySQL==1.0.2 +SQLAlchemy==1.4.36 +Werkzeug==2.1.2 diff --git a/static/styles/style.css b/static/styles/style.css new file mode 100644 index 0000000..79a3e7e --- /dev/null +++ b/static/styles/style.css @@ -0,0 +1,4 @@ +body { + background-color: black; + color: white; +} \ No newline at end of file diff --git a/templates/auth/login.html b/templates/auth/login.html new file mode 100644 index 0000000..e885d5d --- /dev/null +++ b/templates/auth/login.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block content %} + +{% if message is defined %} +

+ {{ message }} +

+{% endif %} + +
+ + + + + +
+ +{% endblock %} diff --git a/templates/auth/signup.html b/templates/auth/signup.html new file mode 100644 index 0000000..4f7a04f --- /dev/null +++ b/templates/auth/signup.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} + +{% block content %} + +{% if message is defined %} +

+ {{ message }} +

+{% endif %} + +
+ + + + + + + +
+ +{% endblock %} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..d6ae020 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,24 @@ + + + + + + + {% block title %}BirdQuizz{% endblock %} + + + +
+

BirdQuizz

+ {% if username is defined %} +

Welcome {{ username }}

+ {% endif %} +
+
+ {% block content %}{% endblock %} +
+ + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6726a1a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block content %} +Welcome to BirdQuizz ! +{% endblock %} \ No newline at end of file