-
-
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.
- Loading branch information
1 parent
1d203ce
commit 168a9e8
Showing
5 changed files
with
60 additions
and
21 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from datetime import datetime | ||
import hashlib | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer | ||
from flask import current_app | ||
from flask import current_app, request | ||
from flask_login import UserMixin, AnonymousUserMixin | ||
from . import db, login_manager | ||
|
||
|
@@ -166,6 +167,12 @@ def ping(self): | |
self.last_seen = datetime.utcnow() | ||
db.session.add(self) | ||
|
||
def gravatar(self, size=100, default='identicon', rating='g'): | ||
url = 'https://secure.gravatar.com/avatar' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
miguelgrinberg
Author
Owner
|
||
hash = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() | ||
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( | ||
url=url, hash=hash, size=size, default=default, rating=rating) | ||
|
||
def __repr__(self): | ||
return '<User %r>' % self.username | ||
|
||
|
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,8 @@ | ||
.profile-thumbnail { | ||
position: absolute; | ||
} | ||
.profile-header { | ||
min-height: 260px; | ||
margin-left: 280px; | ||
} | ||
|
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
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
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 |
---|---|---|
|
@@ -156,3 +156,20 @@ def test_ping(self): | |
last_seen_before = u.last_seen | ||
u.ping() | ||
self.assertTrue(u.last_seen > last_seen_before) | ||
|
||
def test_gravatar(self): | ||
u = User(email='[email protected]', password='cat') | ||
with self.app.test_request_context('/'): | ||
gravatar = u.gravatar() | ||
gravatar_256 = u.gravatar(size=256) | ||
gravatar_pg = u.gravatar(rating='pg') | ||
gravatar_retro = u.gravatar(default='retro') | ||
with self.app.test_request_context('/', base_url='https://example.com'): | ||
gravatar_ssl = u.gravatar() | ||
self.assertTrue('http://www.gravatar.com/avatar/' + | ||
'd4c74594d841139328695756648b6bd6'in gravatar) | ||
self.assertTrue('s=256' in gravatar_256) | ||
self.assertTrue('r=pg' in gravatar_pg) | ||
self.assertTrue('d=retro' in gravatar_retro) | ||
self.assertTrue('https://secure.gravatar.com/avatar/' + | ||
'd4c74594d841139328695756648b6bd6' in gravatar_ssl) |
When constructing this
url
, shouldn't the method ask for secure or non-secure request?When I do the tests
test_gravatar
fails. I believe it is because the strings'https://secure.gravatar.com/avatar/'
and'http://www.gravatar.com/avatar/'
will never match,because the method
gravatar
will always return a string containing at the beginning'https://secure.gravatar.com/avatar'
and never'http://www.gravatar.com/avatar/'
.I did
Is this a good practice? Sorry, I'm relatively new to web dev.