diff --git a/config.example.py b/config.example.py index 844c0e8..904eb41 100644 --- a/config.example.py +++ b/config.example.py @@ -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 diff --git a/nyaa/__init__.py b/nyaa/__init__.py index e743c6f..062b4fd 100644 --- a/nyaa/__init__.py +++ b/nyaa/__init__.py @@ -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 diff --git a/nyaa/models.py b/nyaa/models.py index fda4be2..f4b2c48 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -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):