diff --git a/Notes.docx b/Notes.docx index 8d0c737..85fe73e 100644 Binary files a/Notes.docx and b/Notes.docx differ diff --git a/blog-app/blogapp/__init__.py b/blog-app/blogapp/__init__.py index 75d94a1..efd6fcf 100644 --- a/blog-app/blogapp/__init__.py +++ b/blog-app/blogapp/__init__.py @@ -13,6 +13,6 @@ db = SQLAlchemy(app) bcrypt = Bcrypt(app) login_manager = LoginManager(app) login_manager.login_view = 'login' -# login_manager.login_message_category = 'info' bootstrap class + from blogapp import routes diff --git a/blog-app/blogapp/forms.py b/blog-app/blogapp/forms.py index 16d0797..9bcb4bd 100644 --- a/blog-app/blogapp/forms.py +++ b/blog-app/blogapp/forms.py @@ -1,4 +1,6 @@ from flask_wtf import FlaskForm +from flask_wtf.file import FileField, FileAllowed +from flask_login import current_user #type: ignore from wtforms import StringField, PasswordField, SubmitField, BooleanField from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError from blogapp.models import User @@ -6,7 +8,7 @@ from blogapp.models import User class RegistrationForm(FlaskForm): username = StringField('Username', - validators=[DataRequired(), Length(min=2, max=20)]) + validators=[DataRequired(), Length(min=4, max=20)]) email = StringField('Email', validators=[DataRequired(), Email()]) @@ -31,4 +33,25 @@ class LoginForm(FlaskForm): password = PasswordField('Password' , validators=[DataRequired()]) remember = BooleanField('Remember Me') - submit =SubmitField('Login') \ No newline at end of file + submit =SubmitField('Login') + +class UpdateAccountForm(FlaskForm): + username = StringField('Username', + validators=[DataRequired(), Length(min=4, max=20)]) + email = StringField('Email', + validators=[DataRequired(), Email()]) + + picture = FileField('Update Profile Picture', validators=[FileAllowed(['jpg','png','jpeg'])]) + submit =SubmitField('Update') + + def validate_username(self, username): + if username.data != current_user.username: + user = User.query.filter_by(username=username.data).first() + if user: + raise ValidationError('That username is taken. Please choose a different one') + + def validate_email(self, email): + if email.data != current_user.email: + user = User.query.filter_by(email=email.data).first() + if user: + raise ValidationError('That email is taken. Please choose a different one') diff --git a/blog-app/blogapp/models.py b/blog-app/blogapp/models.py index 5677c70..8d44b3f 100644 --- a/blog-app/blogapp/models.py +++ b/blog-app/blogapp/models.py @@ -11,7 +11,7 @@ class User(db.Model, UserMixin): username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, - default='default.jpg') + default='default.svg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) diff --git a/blog-app/blogapp/routes.py b/blog-app/blogapp/routes.py index 2a621c4..ae184e8 100644 --- a/blog-app/blogapp/routes.py +++ b/blog-app/blogapp/routes.py @@ -1,6 +1,9 @@ +import os +import secrets +from PIL import Image #type: ignore from flask import render_template, url_for, flash, redirect, request from blogapp import app, db, bcrypt -from blogapp.forms import RegistrationForm, LoginForm +from blogapp.forms import RegistrationForm, LoginForm, UpdateAccountForm from blogapp.models import User, Post from flask_login import login_user, current_user, logout_user, login_required # type: ignore @@ -65,7 +68,33 @@ def logout(): logout_user() return redirect(url_for('home')) -@app.route("/account") +def save_picture(form_picture): + random_hex = secrets.token_hex(8) + _, f_ext = os.path.splitext(form_picture.filename) + picture_fn = random_hex + f_ext + picture_path = os.path.join(app.root_path, 'static/profil_pics', picture_fn) + output_size = (125,125) + i = Image.open(form_picture) + i.thumbnail(output_size) + i.save(picture_path) + return picture_fn + +@app.route("/account", methods=['GET', 'POST']) @login_required def account(): - return render_template('account.html', title='account', pagetitle=pagetitle) \ No newline at end of file + form = UpdateAccountForm() + if form.validate_on_submit(): + if form.picture.data: + picture_file = save_picture(form.picture.data) + current_user.image_file = picture_file + current_user.username = form.username.data + current_user.email = form.email.data + db.session.commit() + flash('Your account has been updated') + return redirect(url_for('account')) + elif request.method == 'GET': + form.username.data = current_user.username + form.email.data = current_user.email + image_file = url_for('static', filename='profil_pics/' + current_user.image_file) + return render_template('account.html', title='account', pagetitle=pagetitle, + image_file=image_file, form=form) \ No newline at end of file diff --git a/blog-app/blogapp/static/main.css b/blog-app/blogapp/static/main.css index eb75506..2790656 100644 --- a/blog-app/blogapp/static/main.css +++ b/blog-app/blogapp/static/main.css @@ -1,3 +1,9 @@ .invalid { color:red; +} + +.round-image{ + width: 100px; + height:100px; + border-radius: 50%; } \ No newline at end of file diff --git a/blog-app/blogapp/static/profil_pics/default.svg b/blog-app/blogapp/static/profil_pics/default.svg new file mode 100644 index 0000000..b243d79 --- /dev/null +++ b/blog-app/blogapp/static/profil_pics/default.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/blog-app/blogapp/static/profil_pics/f49fd8e7ae9e59c8.jpeg b/blog-app/blogapp/static/profil_pics/f49fd8e7ae9e59c8.jpeg new file mode 100644 index 0000000..b1ff4bf Binary files /dev/null and b/blog-app/blogapp/static/profil_pics/f49fd8e7ae9e59c8.jpeg differ diff --git a/blog-app/blogapp/templates/account.html b/blog-app/blogapp/templates/account.html index 4b8de3d..74cd7ea 100644 --- a/blog-app/blogapp/templates/account.html +++ b/blog-app/blogapp/templates/account.html @@ -2,6 +2,59 @@ {% block content %} + +

Welcome {{ current_user.username }}

+
+
+ {{ form.hidden_tag() }} +
+
+ Account Info +
+ {{ form.username.label() }} + {% if form.username.errors %} + {{ form.username()}} +
+ {% for error in form.username.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.username() }} + {% endif %} +
+
+
+ {{ form.email.label() }} + {% if form.email.errors %} + {{ form.email() }} +
+ {% for error in form.email.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.email() }} + {% endif %} +
+
+
+ {{ form.picture.label() }} + {{ form.picture() }} + {% if form.picture.errors %} + {% for error in form.picture.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ +
+
+ {{ form.submit() }} +
+
+
+ {% endblock content %} \ No newline at end of file diff --git a/blog-app/blogapp/templates/login.html b/blog-app/blogapp/templates/login.html index d9d19c1..db33905 100644 --- a/blog-app/blogapp/templates/login.html +++ b/blog-app/blogapp/templates/login.html @@ -5,6 +5,7 @@
{{ form.hidden_tag() }} +
Login
diff --git a/blog-app/blogapp/templates/register.html b/blog-app/blogapp/templates/register.html index 4e4017f..64e136b 100644 --- a/blog-app/blogapp/templates/register.html +++ b/blog-app/blogapp/templates/register.html @@ -5,6 +5,7 @@
{{ form.hidden_tag() }} +
Join Today
diff --git a/blog-app/instance/blogsite.db b/blog-app/instance/blogsite.db index 893a6ec..9a35935 100644 Binary files a/blog-app/instance/blogsite.db and b/blog-app/instance/blogsite.db differ