Ver código fonte

added password change, profile pic upload functionality, updated db model

control 3 anos atrás
pai
commit
12bb77c0e5

BIN
app/database.db


+ 33 - 4
app/logic.py

@@ -1,5 +1,6 @@
 # Site Back-End Logic
 from hmac import new
+from unicodedata import category
 from flask import Blueprint, render_template, request, flash, redirect, url_for
 from flask_login import login_user, login_required, logout_user, current_user
 from werkzeug.security import generate_password_hash, check_password_hash
@@ -9,9 +10,37 @@ import string, secrets
 
 logic = Blueprint('logic', __name__)
 
-@logic.route('/', methods=['GET'])
+@logic.route('/', methods=['GET', 'POST'])
 @login_required
-def profile():
+def home():
+    if request.method == "POST":
+        passwd = request.form.get('password')
+        passwdc = request.form.get('password_confirm')
+        new_pic = request.files.get('profile_image')
+
+        if passwd and passwdc:
+            if passwd == passwdc:
+                npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
+                npasswd_dbcall.password = generate_password_hash(passwdc, method='sha256')
+                db.session.commit()
+                flash('Password successfully updated', category='success')
+            else:
+                flash('Passwords must match!', category='error')
+        elif new_pic:
+            # generate random filename for uploaded file 
+            alphanumeric = string.ascii_letters + string.digits
+            ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
+            new_pic_dbcall = User.query.filter_by(id=current_user.id).first()
+            if '.png' in new_pic.filename:
+                new_pic.save(f'app/static/uploads/{ralphanum}.png')
+                new_pic_dbcall.profile_image = f'{ralphanum}.png'
+            elif '.jpg' in new_pic.filename or 'jpeg' in new_pic.filename:
+                new_pic.save(f'app/static/uploads/{ralphanum}.jpeg')
+                new_pic_dbcall.profile_image = f'{ralphanum}.jpeg'
+            db.session.commit()
+            flash('Profile Pic updated!', category='success')
+
+
     return render_template('home.html', user=current_user)
 
 @logic.route('/login', methods=['GET', 'POST'])
@@ -25,7 +54,7 @@ def login():
             if check_password_hash(user.password, challenge_passwd):
                 flash('Successful Login!', category='success')
                 login_user(user, remember=True)
-                return redirect(url_for('logic.profile'))
+                return redirect(url_for('logic.home'))
             else:
                 flash('Unsucessful Login!', category='error')
         else:
@@ -73,7 +102,7 @@ def register():
                     db.session.add(new_user)
                     db.session.commit()
                     flash('Account Registration Successful!', category='success')
-                    return redirect(url_for('logic.profile'))
+                    return redirect(url_for('logic.home'))
                 else:
                     flash('Registration Failed', category='error')
     return render_template("register.html", user=current_user)

+ 2 - 0
app/models.py

@@ -8,6 +8,8 @@ class User(db.Model, UserMixin): # User Database
    email = db.Column(db.String(150), unique=True)
    password = db.Column(db.String(150))
    username = db.Column(db.String(150))
+   profile_image = db.Column(db.String(150))
+
 
 class Bookmark(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)

BIN
app/static/uploads/I61SBBhnYX6PElmh.png


+ 4 - 4
app/templates/home.html

@@ -9,7 +9,7 @@
 <!------  Profile Information ---------->
 <p>Profile Information</p>
 <div class="card">
-    <img src="userimg.jpg" style="width:100%"><p>(Show user image)</p>
+    <img src="static/uploads/{{ user.profile_image }}" style="width:100%"><p>(Show user image)</p>
     <h1>(User name)</h1>
     <p>(email)</p>
 </div>
@@ -17,7 +17,7 @@
 
 <!------  Change Password ---------->
 <p>Change Password</p> 
-<form>
+<form method="POST">
     <label for="password">Password</label><br>
     <input type="text" id="password" name="password"><br>
     <label for="password_confirm">Password(Confirm)</label><br>
@@ -29,8 +29,8 @@
 
 <!------  Upload Profile Image ---------->
 <p>Upload Profile Image</p> 
-<form action="/" method="POST" enctype="multipart/form-data">
-    <input type="file" id="profile_image" name="filename">   
+<form action="" method="POST" enctype="multipart/form-data">
+    <input type="file" id="profile_image" name="profile_image" accept="image/png, image/jpeg">   
     <input type="submit" value="Upload">
 </form>
 <hr style="border: 5px dashed black;"/>