From cdebe081c3fcd25b21e4d84c2b298fd0b442a025 Mon Sep 17 00:00:00 2001 From: finn Date: Sat, 3 Aug 2024 02:14:20 -0700 Subject: [PATCH] mgt c6 checkpoint --- backend/README.md | 1 + backend/app/models.py | 32 +++++++++++++++++- backend/app/routes.py | 11 +++++- backend/app/templates/_post.html | 6 ++++ backend/app/templates/base.html | 1 + backend/app/templates/user.html | 15 ++++++++ .../39466ff4e782fc398fed2c3b21d53ba2.png | Bin 0 -> 384 bytes .../686fed96bd36dc9ba0c0434036031942.png | Bin 0 -> 397 bytes .../7a651a7bab949a73092e21ad0bd2f4a8.png | Bin 0 -> 378 bytes backend/requirements.txt | 2 ++ 10 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 backend/app/templates/_post.html create mode 100644 backend/app/templates/user.html create mode 100644 backend/app/usercontent/identicon/39466ff4e782fc398fed2c3b21d53ba2.png create mode 100644 backend/app/usercontent/identicon/686fed96bd36dc9ba0c0434036031942.png create mode 100644 backend/app/usercontent/identicon/7a651a7bab949a73092e21ad0bd2f4a8.png diff --git a/backend/README.md b/backend/README.md index 0fc2bd1..07ea60b 100644 --- a/backend/README.md +++ b/backend/README.md @@ -16,6 +16,7 @@ pip install flask-sqlalchemy pip install flask-migrate pip install flask-login pip install email-validator +pip install pydenticon ... pip freeze > requirements.txt ``` diff --git a/backend/app/models.py b/backend/app/models.py index 36cbb11..36c70a5 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1,9 +1,12 @@ +import os from datetime import datetime, timezone from typing import Optional import sqlalchemy as sa import sqlalchemy.orm as so -from app import db, login from werkzeug.security import generate_password_hash, check_password_hash +import pydenticon, hashlib, base64 + +from app import db, login from flask_login import UserMixin class User(UserMixin, db.Model): @@ -17,6 +20,33 @@ class User(UserMixin, db.Model): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) + def gen_avatar(self, write_png=True): + foreground = [ "rgb(45,79,255)", + "rgb(254,180,44)", + "rgb(226,121,234)", + "rgb(30,179,253)", + "rgb(232,77,65)", + "rgb(49,203,115)", + "rgb(141,69,170)" ] + background = "rgb(22,22,22)" + + digest = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() + basedir = os.path.abspath(os.path.dirname(__file__)) + pngloc = os.path.join(basedir, 'usercontent', 'identicon', str(digest) + '.png') + icongen = pydenticon.Generator(5, 5, digest=hashlib.md5, foreground=foreground, background=background) + pngicon = icongen.generate(self.email, 120, 120, padding=(10, 10, 10, 10), inverted=False, output_format="png") + if write_png: + pngfile = open(pngloc, "wb") + pngfile.write(pngicon) + pngfile.close() + else: + return str(base64.b64encode(pngicon))[2:-1] + def avatar(self): + digest = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() + basedir = os.path.abspath(os.path.dirname(__file__)) + pngloc = os.path.join(basedir, 'usercontent', 'identicon', digest + '.png') + return pngloc + def __repr__(self): return ''.format(self.username) diff --git a/backend/app/routes.py b/backend/app/routes.py index 72d750a..e301aaf 100644 --- a/backend/app/routes.py +++ b/backend/app/routes.py @@ -57,8 +57,17 @@ def register(): user.set_password(form.password.data) db.session.add(user) db.session.commit() + user.gen_avatar() flash('User has been created.') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form) - +@app.route('/user/') +@login_required +def user(username): + user = db.first_or_404(sa.select(User).where(User.username == username)) + posts = [ + {'author': user, 'body': 'Test1'}, + {'author': user, 'body': 'Test2?'} + ] + return render_template('user.html', user=user, posts=posts) diff --git a/backend/app/templates/_post.html b/backend/app/templates/_post.html new file mode 100644 index 0000000..424bfc2 --- /dev/null +++ b/backend/app/templates/_post.html @@ -0,0 +1,6 @@ + + + + + +
{{ post.author.username }} says:
{{ post.body }}
diff --git a/backend/app/templates/base.html b/backend/app/templates/base.html index e83424e..1c5890a 100644 --- a/backend/app/templates/base.html +++ b/backend/app/templates/base.html @@ -15,6 +15,7 @@ {% if current_user.is_anonymous %} login {% else %} + Profile logout {% endif %} diff --git a/backend/app/templates/user.html b/backend/app/templates/user.html new file mode 100644 index 0000000..508579a --- /dev/null +++ b/backend/app/templates/user.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} + + + + + +

User: {{ user.username }}

+
+ {% for post in posts %} + {% include '_post.html' %} + {% endfor %} +{% endblock %} + diff --git a/backend/app/usercontent/identicon/39466ff4e782fc398fed2c3b21d53ba2.png b/backend/app/usercontent/identicon/39466ff4e782fc398fed2c3b21d53ba2.png new file mode 100644 index 0000000000000000000000000000000000000000..0e771b051fbeddbf1dcb239e5d92f644de153806 GIT binary patch literal 384 zcmeAS@N?(olHy`uVBq!ia0vp^Js`}%1|-)VaI|A!V08C%aSW-r_4e*T&ISVk*Mlx= z6eH%hM$W&{Ql_BAQdh8y>EiKcTb6CRc8-bPH;%E%(SU>Ppn`+|50Ld_X0Tk!{aNb^ zlOy?s+1!Pi7=bb@%?UtBEG)3TpexJk7f99SulOE!KK@=mXR|UVM2`c+T%c}D7W31i zahG0Hz2{weCq3m5*a}QV2#w4zN3vjHfgO(U0!!CU{gvNq-4=WA>_6E8^&B>R2r&)E nYR?%U*EWDW3ev)I*j?PnQ7pQoqGly9WEnhN{an^LB{Ts5e}ZwQ literal 0 HcmV?d00001 diff --git a/backend/app/usercontent/identicon/686fed96bd36dc9ba0c0434036031942.png b/backend/app/usercontent/identicon/686fed96bd36dc9ba0c0434036031942.png new file mode 100644 index 0000000000000000000000000000000000000000..88c2f586d2c7f0899eff624bcf733920f3d98936 GIT binary patch literal 397 zcmeAS@N?(olHy`uVBq!ia0vp^Js`}%1|-)VaI|A!U<~neaSW-r_4e*T&Q=2fhl4I_ z6mxIPD9`?ommIc4)v5+U_0250Awl7H$2TB zx$Nxz!o<7j!ffF}O^gQ>Bv_h3a#&bEZ3+^bR_u*m@axyTfG=_9l?A1qsjt%uNp$3hyhFnqF(q=*9p9p00i_>zopr0A(YCf&c&j literal 0 HcmV?d00001 diff --git a/backend/app/usercontent/identicon/7a651a7bab949a73092e21ad0bd2f4a8.png b/backend/app/usercontent/identicon/7a651a7bab949a73092e21ad0bd2f4a8.png new file mode 100644 index 0000000000000000000000000000000000000000..20f3c735219733f4aea39ce0da4441423ab69c17 GIT binary patch literal 378 zcmeAS@N?(olHy`uVBq!ia0vp^Js`}%1|-)VaI|A!V083!aSW-r_4e*Y&ISVkhd^b` zIS&r}G@AZ-r_-(*77b1<9`~dj{C>^}_2}x@cEEYHe?x)*Pm`ko2OCQ>koEZFVxCR1 z&zGNHpK89J7bF6dQ;-15H8CE<#=3C)ak1w)-kJB?4(fA4R5$>Q!^L9Wp83w(h*-0I zyE>j3Cme>_3o})K2ODe8g!Iab>)#u%O5L7_*9Bnj0^OJPaGy#Js^5O>=6