From 0420bec422d7b07e616d7b952d12794420672854 Mon Sep 17 00:00:00 2001
From: "Ethan Clay (UWE)" <ethan2.clay@live.uwe.ac.uk>
Date: Tue, 3 Dec 2024 11:43:07 +0000
Subject: [PATCH] Begin to work on login sessions

---
 app/__init__.py                   |  8 ++++++++
 app/admin/__init__.py             |  5 +++++
 app/admin/routes.py               |  8 ++++++++
 app/models/user.py                | 10 ++++++++-
 app/profile/__init__.py           |  5 +++++
 app/profile/routes.py             | 34 +++++++++++++++++++++++++++++++
 app/templates/profile/index.html  |  7 +++++++
 app/templates/profile/login.html  | 29 ++++++++++++++++++++++++++
 app/templates/profile/signup.html | 30 +++++++++++++++++++++++++++
 requirements.txt                  |  3 ++-
 10 files changed, 137 insertions(+), 2 deletions(-)
 create mode 100644 app/admin/__init__.py
 create mode 100644 app/admin/routes.py
 create mode 100644 app/profile/__init__.py
 create mode 100644 app/profile/routes.py
 create mode 100644 app/templates/profile/index.html
 create mode 100644 app/templates/profile/login.html
 create mode 100644 app/templates/profile/signup.html

diff --git a/app/__init__.py b/app/__init__.py
index 56be70a..df1d438 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -26,6 +26,8 @@ def create_app(config_class=Config):
     db_password = os.getenv("DATABASE_PASSWORD")
     db_name = os.getenv("DATABASE_NAME")
     
+    app.config['SECRET_KEY'] = 'tnm]H+akmfnf_#PT>i|(Qo4LT@+n£9"~e3'
+    
     app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}".format(db_user=db_user, db_password=db_password, db_host=db_host, db_name=db_name)
     print(app.config['SQLALCHEMY_DATABASE_URI'])
     db.init_app(app)
@@ -43,6 +45,12 @@ def create_app(config_class=Config):
     from app.api import bp as api_bp
     app.register_blueprint(api_bp, url_prefix='/api')
     
+    from app.admin import bp as admin_bp
+    app.register_blueprint(admin_bp, url_prefix='/admin')
+    
+    from app.profile import bp as profile_bp
+    app.register_blueprint(profile_bp, url_prefix='/profile')
+    
     if __name__ == "__main__":
         app.run(use_reloader=True)
 
diff --git a/app/admin/__init__.py b/app/admin/__init__.py
new file mode 100644
index 0000000..3355430
--- /dev/null
+++ b/app/admin/__init__.py
@@ -0,0 +1,5 @@
+from flask import Blueprint
+
+bp = Blueprint('admin', __name__)
+
+from app.admin import routes
\ No newline at end of file
diff --git a/app/admin/routes.py b/app/admin/routes.py
new file mode 100644
index 0000000..64218ae
--- /dev/null
+++ b/app/admin/routes.py
@@ -0,0 +1,8 @@
+from flask import render_template, redirect, url_for
+from app.admin import bp
+from app.models import Listings, ListingImages 
+
+
+@bp.route('/manage_listings')
+def manage_listings():
+    return render_template('admin/index.html', top_listings=top_listings, top_listing_images=top_listing_images)
\ No newline at end of file
diff --git a/app/models/user.py b/app/models/user.py
index c22b429..c1881bf 100644
--- a/app/models/user.py
+++ b/app/models/user.py
@@ -21,4 +21,12 @@ class User(db.Model):
 
     @classmethod
     def search_user_id(cls, user_id):
-        return cls.query.get(user_id)
\ No newline at end of file
+        return cls.query.get(user_id)
+    
+
+    @classmethod
+    def search_user_by_email(cls, user_email):
+        
+        user_exist = cls.query.filter_by(email=user_email).first()
+        
+        return user_exist
\ No newline at end of file
diff --git a/app/profile/__init__.py b/app/profile/__init__.py
new file mode 100644
index 0000000..c57db61
--- /dev/null
+++ b/app/profile/__init__.py
@@ -0,0 +1,5 @@
+from flask import Blueprint
+
+bp = Blueprint('profile', __name__)
+
+from app.profile import routes
\ No newline at end of file
diff --git a/app/profile/routes.py b/app/profile/routes.py
new file mode 100644
index 0000000..2fd51c3
--- /dev/null
+++ b/app/profile/routes.py
@@ -0,0 +1,34 @@
+from flask import render_template, redirect, url_for
+from app.profile import bp
+from app.models import User 
+
+@bp.route('/')
+def index():
+    return render_template('profile/index.html')
+
+@bp.route('/login')
+def login():
+    return 'Login'
+
+@bp.route('/signup')
+def signup():
+    return render_template('profile/signup.html')
+
+@bp.route('/signup', methods=['POST'])
+def signup_post():
+    email = request.form.get('email')
+    name = request.form.get('name')
+    password = request.form.get('password')
+    if User.search_user_by_email(email):
+        return redirect(url_for('auth.signup'))
+
+    new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))
+
+    db.session.add(new_user)
+    db.session.commit()
+    
+    return redirect(url_for('auth.login'))
+
+@bp.route('/logout')
+def logout():
+    return 'Logout'
\ No newline at end of file
diff --git a/app/templates/profile/index.html b/app/templates/profile/index.html
new file mode 100644
index 0000000..7da6f97
--- /dev/null
+++ b/app/templates/profile/index.html
@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+# Implements CSS Slider from https://swiffyslider.com/docs/
+{% block content %}
+<div>
+    <p>Welcome logged in!</p>
+</div>
+{% endblock %}
\ No newline at end of file
diff --git a/app/templates/profile/login.html b/app/templates/profile/login.html
new file mode 100644
index 0000000..0384d22
--- /dev/null
+++ b/app/templates/profile/login.html
@@ -0,0 +1,29 @@
+{% extends 'base.html' %}
+# Implements CSS Slider from https://swiffyslider.com/docs/
+{% block content %}
+<div class="column is-4 is-offset-4">
+    <h3 class="title">Login</h3>
+    <div class="box">
+        <form method="POST" action="/login">
+            <div class="field">
+                <div class="control">
+                    <input class="input is-large" type="email" name="email" placeholder="Your Email" autofocus="">
+                </div>
+            </div>
+
+            <div class="field">
+                <div class="control">
+                    <input class="input is-large" type="password" name="password" placeholder="Your Password">
+                </div>
+            </div>
+            <div class="field">
+                <label class="checkbox">
+                    <input type="checkbox" name="remember">
+                    Remember me
+                </label>
+            </div>
+            <button class="button is-block is-info is-large is-fullwidth">Login</button>
+        </form>
+    </div>
+</div>
+{% endblock %}
\ No newline at end of file
diff --git a/app/templates/profile/signup.html b/app/templates/profile/signup.html
new file mode 100644
index 0000000..8966ed1
--- /dev/null
+++ b/app/templates/profile/signup.html
@@ -0,0 +1,30 @@
+{% extends 'base.html' %}
+
+{% block content %}
+<div class="column is-4 is-offset-4">
+    <h3 class="title">Sign Up</h3>
+    <div class="box">
+        <form method="POST" action="/signup">
+            <div class="field">
+                <div class="control">
+                    <input class="input is-large" type="email" name="email" placeholder="Email" autofocus="">
+                </div>
+            </div>
+
+            <div class="field">
+                <div class="control">
+                    <input class="input is-large" type="text" name="name" placeholder="Name" autofocus="">
+                </div>
+            </div>
+
+            <div class="field">
+                <div class="control">
+                    <input class="input is-large" type="password" name="password" placeholder="Password">
+                </div>
+            </div>
+
+            <button class="button is-block is-info is-large is-fullwidth">Sign Up</button>
+        </form>
+    </div>
+</div>
+{% endblock %}
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index bb63d03..4f3f85f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,4 +4,5 @@ flask_migrate
 pymysql
 python-dotenv
 jinja2
-cryptography
\ No newline at end of file
+cryptography
+flask-login
\ No newline at end of file
-- 
GitLab