commit 18a339e6b21b2002ecbf73790d13e66f8ea76b53 Author: enzo.jolys Date: Mon Oct 31 18:42:18 2022 +0100 first commit diff --git a/instance/database.db b/instance/database.db new file mode 100644 index 0000000..06a8d72 Binary files /dev/null and b/instance/database.db differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..6bbb7c9 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from website import create_app + + +app = create_app() + +if __name__ == '__name__' : + app.run(debug=True) \ No newline at end of file diff --git a/run b/run new file mode 100755 index 0000000..23b0b75 --- /dev/null +++ b/run @@ -0,0 +1 @@ +FLASK_APP=main.py flask run \ No newline at end of file diff --git a/script_Add_Picture.py b/script_Add_Picture.py new file mode 100644 index 0000000..625bf89 --- /dev/null +++ b/script_Add_Picture.py @@ -0,0 +1,44 @@ +import sqlite3 +import sys + +def info(): + print("Info Argument: Name_Picture Name_User") + +def calculUser(cur): + id_free = 1 + + while True : + cur.execute('SELECT id FROM Data_Picture WHERE id == ?',(str(id_free))) + tmp = cur.fetchall() + + if len(tmp) == 0 : + break + id_free+=1 + + return id_free + +arg = sys.argv + +if len(arg) != 3 : + info() + exit(1) + +try : + conn = sqlite3.connect('instance/database.db') + cur = conn.cursor() + print("Connection : OK") + + id_free = calculUser(cur) + + nom_picture = arg[1] + user = arg[2] + + cur.execute('INSERT INTO Data_Picture(id,data,user_id) VALUES(?,?,?)',(id_free,nom_picture,user)) + conn.commit() + print("Picture add") + + cur.close() + conn.close() +except sqlite3.Error as error : + print(error) + \ No newline at end of file diff --git a/website/__init__.py b/website/__init__.py new file mode 100644 index 0000000..57acda1 --- /dev/null +++ b/website/__init__.py @@ -0,0 +1,53 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from os import path +from flask_login import LoginManager, login_manager + +db = SQLAlchemy() +DB_NAME = "database.db" + +def create_app(): + app = Flask(__name__,static_url_path='/static') + app.config['SECRET_KEY'] = 'toto' + app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}' + db.init_app(app) + + from .views import views + #import les routes de views + + from .auth import auth + #import les routes de auth + + app.register_blueprint(views,url_prefix='/') + app.register_blueprint(auth,url_prefix='/') + #Préfixes pour accéder à ces routes + + from .models import User,DataPicture + create_database(app) + + #redirect on the page login if not loggin + login_manager = LoginManager() + login_manager.login_view = 'auth.login' + #what app we use + login_manager.init_app(app) + + #how we load a User ( model User and the primary key id) + @login_manager.user_loader + def load_user(id): + return User.query.get(int(id)) + + return app + + + +def create_database(app): + if not path.exists('website/' + DB_NAME): + with app.app_context(): + db.create_all() + print('Created Database!') + + + + + + diff --git a/website/__pycache__/__init__.cpython-310.pyc b/website/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..1fe7302 Binary files /dev/null and b/website/__pycache__/__init__.cpython-310.pyc differ diff --git a/website/__pycache__/auth.cpython-310.pyc b/website/__pycache__/auth.cpython-310.pyc new file mode 100644 index 0000000..109d642 Binary files /dev/null and b/website/__pycache__/auth.cpython-310.pyc differ diff --git a/website/__pycache__/models.cpython-310.pyc b/website/__pycache__/models.cpython-310.pyc new file mode 100644 index 0000000..13aee06 Binary files /dev/null and b/website/__pycache__/models.cpython-310.pyc differ diff --git a/website/__pycache__/views.cpython-310.pyc b/website/__pycache__/views.cpython-310.pyc new file mode 100644 index 0000000..0e988d0 Binary files /dev/null and b/website/__pycache__/views.cpython-310.pyc differ diff --git a/website/auth.py b/website/auth.py new file mode 100644 index 0000000..17391b4 --- /dev/null +++ b/website/auth.py @@ -0,0 +1,73 @@ +from flask import Blueprint,render_template,request,flash, redirect ,url_for +from .models import User +from werkzeug.security import generate_password_hash,check_password_hash +from . import db +from flask_login import login_user,login_required,logout_user,current_user + +auth = Blueprint('auth',__name__) + + +@auth.route('/login',methods=['GET','POST']) +def login(): + if request.method == 'POST': + email = request.form.get('email') + password = request.form.get('password') + + user = User.query.filter_by(email=email).first() + if user: + if check_password_hash(user.password,password): + #logged + print("logged") + login_user(user,remember=True) + return redirect(url_for('views.home')) + else : + #Password wrong + print("Password wrong") + pass + else : + #Not find user + print("User don't exist") + + return render_template("login.html") + +@auth.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('auth.login')) + +@auth.route('/sign-up',methods=['GET','POST']) +def sign_up(): + if request.method == 'POST' : + email = request.form.get('email') + firstName = request.form.get('firstName') + password = request.form.get('password') + password2 = request.form.get('password2') + + #Check if exist user + user = User.query.filter_by(email=email).first() + if user : + print("User already exist") + return redirect(url_for('auth.login')) + + elif len(email) < 4 : + print("test") + pass + elif len(firstName) < 2 : + pass + elif password != password2 : + return "Wrong" + elif len(password) < 7 : + return "Wrong" + else : + new_user = User(email=email,firstName=firstName,password=generate_password_hash(password,method='sha256'),config_pi="rien") + db.session.add(new_user) + db.session.commit() + login_user(user,remember=True) + return redirect(url_for('auth.login')) + else : + return render_template("sign-up.html") + + + +# Utiliser flash pour envoyer des petits message (error ou sucess)!! \ No newline at end of file diff --git a/website/config/test.txt b/website/config/test.txt new file mode 100644 index 0000000..e5352d0 --- /dev/null +++ b/website/config/test.txt @@ -0,0 +1 @@ +TEST CONGIF \ No newline at end of file diff --git a/website/models.py b/website/models.py new file mode 100644 index 0000000..cea4cc1 --- /dev/null +++ b/website/models.py @@ -0,0 +1,29 @@ +from enum import unique +from . import db +#Import db from init.py +from flask_login import UserMixin +from sqlalchemy.sql import func + +class DataPicture(db.Model): + id = db.Column(db.Integer,primary_key=True) + data = db.Column(db.String(100)) + date = db.Column(db.DateTime(timezone=True),default=func.now()) + + #Foreign Key of User + user_id = db.Column(db.Integer,db.ForeignKey('user.id')) + + +class User(db.Model,UserMixin): + + id = db.Column(db.Integer,primary_key=True) + email = db.Column(db.String(100),unique=True) + password = db.Column(db.String(100)) + firstName = db.Column(db.String(100)) + + config_pi = db.Column(db.String(100)) + + #lien au niveau de html voir home.html + pictures = db.relationship('DataPicture') + + + diff --git a/website/static/picture/9.jpg b/website/static/picture/9.jpg new file mode 100644 index 0000000..ffa82f2 Binary files /dev/null and b/website/static/picture/9.jpg differ diff --git a/website/static/picture/test.png b/website/static/picture/test.png new file mode 100644 index 0000000..f0a868e Binary files /dev/null and b/website/static/picture/test.png differ diff --git a/website/templates/home.html b/website/templates/home.html new file mode 100644 index 0000000..2c8ccbb --- /dev/null +++ b/website/templates/home.html @@ -0,0 +1,35 @@ + + + + + + + +

+ Page Home : +

+ +

Login

+

Sign-up

+

Logout

+

Home

+

+ User info : +

+ {% if user.is_authenticated %} +

TEST TOTO

+ {% endif %} +

Test data

+ {% for var in user.pictures %} + + +

{{var.data}}

+ {% endfor %} +
+ +
+ +
+

delete_all_picture

+ + \ No newline at end of file diff --git a/website/templates/login.html b/website/templates/login.html new file mode 100644 index 0000000..39b2ddf --- /dev/null +++ b/website/templates/login.html @@ -0,0 +1,33 @@ + + + + + + + +

+ Page Sign-up : +

+ +

Login

+

Sign-up

+

Logout

+

Home

+ + + +
+

Login

+
+ + +
+
+ + +
+
+ +
+ + \ No newline at end of file diff --git a/website/templates/logout.html b/website/templates/logout.html new file mode 100644 index 0000000..e4e19cb --- /dev/null +++ b/website/templates/logout.html @@ -0,0 +1,17 @@ + + + + + + + +

+ Page Logout : +

+ +

Login

+

Sign-up

+

Logout

+

Home

+ + \ No newline at end of file diff --git a/website/templates/sign-up.html b/website/templates/sign-up.html new file mode 100644 index 0000000..9f27b8d --- /dev/null +++ b/website/templates/sign-up.html @@ -0,0 +1,41 @@ + + + + + + + +

+ Page Sign-up : +

+ +

Login

+

Sign-up

+

Logout

+

Home

+ + + +
+

Sign-up

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + \ No newline at end of file diff --git a/website/views.py b/website/views.py new file mode 100644 index 0000000..0612911 --- /dev/null +++ b/website/views.py @@ -0,0 +1,50 @@ +from crypt import methods +from flask import Blueprint, render_template,request,redirect ,url_for +import flask +from flask_login import login_required,current_user +from requests import session +from .models import DataPicture +from . import db + + +views = Blueprint('views',__name__) + + +@views.route('/',methods=['GET','POST']) +@login_required +def home(): + if request.method == 'POST': + var = request.form.get('data') + + if len(var) < 1 : + pass + else : + new_data = DataPicture(data=var,user_id=current_user.id) + db.session.add(new_data) + db.session.commit() + print("data add") + + return render_template("home.html",user=current_user) + + +@views.route('/delete_all_picture') +@login_required +def delete_all_picture(): + var = 1 + data = DataPicture.query.get(var) + + while data : + db.session.delete(data) + var+=1 + data = DataPicture.query.get(var) + db.session.commit() + + return redirect(url_for('views.home')) + + +@views.route('/api/config',methods=['GET']) +@login_required +def api(): + file = current_user.config_pi + file ="test.txt" + return flask.send_file("config/"+file) \ No newline at end of file