[Config Change] Various Gravatar-related changes (#467)

* Add config option to enable/disable gravatar

This is useful when running a development instance behind a firewall
or NAT, where gravatar cannot reach you to serve up the default user
avatar.

* Pregenerate Gravatar default image URLs

If possible (i.e. SERVER_NAME is set), we can pregenerate the constant
gravatar default URL once at application startup, and re-use that,
as url_for calls are surprisingly expensive.

Especially on torrent view pages with lots of comments, this cuts down
on url_for calls massively, saving on my system about 0.3 ms per call.
This commit is contained in:
Nicolas F 2018-07-10 07:20:26 +02:00 committed by Arylide
parent e892f358e8
commit 56a670977e
3 changed files with 31 additions and 13 deletions

View File

@ -152,3 +152,7 @@ ES_INDEX_NAME = SITE_FLAVOR
# Time limit for editing a comment after it has been posted (seconds)
# Set to 0 to disable
EDITING_TIME_LIMIT = 0
# Whether to use Gravatar or just always use the default avatar
# (Useful if run as development instance behind NAT/firewall)
ENABLE_GRAVATAR = True

View File

@ -96,4 +96,10 @@ def create_app(config):
app.register_blueprint(api_blueprint)
register_views(app)
# Pregenerate some URLs to avoid repeat url_for calls
if 'SERVER_NAME' in app.config and app.config['SERVER_NAME']:
with app.app_context():
url = flask.url_for('static', filename='img/avatar/default.png', _external=True)
app.config['DEFAULT_GRAVATAR_URL'] = url
return app

View File

@ -518,19 +518,27 @@ class User(db.Model):
return all(checks)
def gravatar_url(self):
# from http://en.gravatar.com/site/implement/images/python/
params = {
# Image size (https://en.gravatar.com/site/implement/images/#size)
's': 120,
# Default image (https://en.gravatar.com/site/implement/images/#default-image)
'd': flask.url_for('static', filename='img/avatar/default.png', _external=True),
# Image rating (https://en.gravatar.com/site/implement/images/#rating)
# Nyaa: PG-rated, Sukebei: X-rated
'r': 'pg' if app.config['SITE_FLAVOR'] == 'nyaa' else 'x',
}
# construct the url
return 'https://www.gravatar.com/avatar/{}?{}'.format(
md5(self.email.encode('utf-8').lower()).hexdigest(), urlencode(params))
if 'DEFAULT_GRAVATAR_URL' in app.config:
default_url = app.config['DEFAULT_GRAVATAR_URL']
else:
default_url = flask.url_for('static', filename='img/avatar/default.png',
_external=True)
if app.config['ENABLE_GRAVATAR']:
# from http://en.gravatar.com/site/implement/images/python/
params = {
# Image size (https://en.gravatar.com/site/implement/images/#size)
's': 120,
# Default image (https://en.gravatar.com/site/implement/images/#default-image)
'd': default_url,
# Image rating (https://en.gravatar.com/site/implement/images/#rating)
# Nyaa: PG-rated, Sukebei: X-rated
'r': 'pg' if app.config['SITE_FLAVOR'] == 'nyaa' else 'x',
}
# construct the url
return 'https://www.gravatar.com/avatar/{}?{}'.format(
md5(self.email.encode('utf-8').lower()).hexdigest(), urlencode(params))
else:
return default_url
@property
def userlevel_str(self):