-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 10: Caching of user avatar hashes (10d)
- Loading branch information
1 parent
168a9e8
commit 20ce2f9
Showing
3 changed files
with
35 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ class User(UserMixin, db.Model): | |
about_me = db.Column(db.Text()) | ||
member_since = db.Column(db.DateTime(), default=datetime.utcnow) | ||
last_seen = db.Column(db.DateTime(), default=datetime.utcnow) | ||
avatar_hash = db.Column(db.String(32)) | ||
|
||
def __init__(self, **kwargs): | ||
super(User, self).__init__(**kwargs) | ||
|
@@ -89,6 +90,8 @@ def __init__(self, **kwargs): | |
self.role = Role.query.filter_by(name='Administrator').first() | ||
if self.role is None: | ||
self.role = Role.query.filter_by(default=True).first() | ||
if self.email is not None and self.avatar_hash is None: | ||
self.avatar_hash = self.gravatar_hash() | ||
|
||
@property | ||
def password(self): | ||
|
@@ -154,6 +157,7 @@ def change_email(self, token): | |
if self.query.filter_by(email=new_email).first() is not None: | ||
return False | ||
self.email = new_email | ||
self.avatar_hash = self.gravatar_hash() | ||
db.session.add(self) | ||
return True | ||
|
||
|
@@ -167,9 +171,12 @@ def ping(self): | |
self.last_seen = datetime.utcnow() | ||
db.session.add(self) | ||
|
||
def gravatar_hash(self): | ||
return hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() | ||
|
||
def gravatar(self, size=100, default='identicon', rating='g'): | ||
url = 'https://secure.gravatar.com/avatar' | ||
hash = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() | ||
hash = self.avatar_hash or self.gravatar_hash() | ||
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( | ||
url=url, hash=hash, size=size, default=default, rating=rating) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
miguelgrinberg
Author
Owner
|
||
|
||
|
26 changes: 26 additions & 0 deletions
26
migrations/versions/198b0eebcf9_caching_of_avatar_hashes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""caching of avatar hashes | ||
Revision ID: 198b0eebcf9 | ||
Revises: d66f086b258 | ||
Create Date: 2014-02-04 09:10:02.245503 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '198b0eebcf9' | ||
down_revision = 'd66f086b258' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('users', sa.Column('avatar_hash', sa.String(length=32), nullable=True)) | ||
### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('users', 'avatar_hash') | ||
### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is there any security implication to using f-strings for this return statement, as in:
return f'{url}/{hash}?s={size}&d={default}&r={rating}'
It seems to be less verbose, but I was wondering if there is a particular reason why you choose to use format strings instead, beyond maybe compatibility with older versions of Python. Thanks