settings: Make DEFAULT_LOGO_URI/DEFAULT_AVATAR_URI use staticfiles.

This commit is contained in:
Alex Vandiver 2023-01-24 23:30:24 +00:00 committed by Alex Vandiver
parent 5d0d0ba4a9
commit e31767dda4
5 changed files with 53 additions and 8 deletions

View File

@ -2,6 +2,7 @@ import urllib
from typing import Any, Dict, Optional
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from zerver.lib.avatar_hash import (
gravatar_hash,
@ -106,7 +107,10 @@ def _get_unversioned_gravatar_url(email: str, medium: bool) -> str:
gravitar_query_suffix = f"&s={MEDIUM_AVATAR_SIZE}" if medium else ""
hash_key = gravatar_hash(email)
return f"https://secure.gravatar.com/avatar/{hash_key}?d=identicon{gravitar_query_suffix}"
return settings.DEFAULT_AVATAR_URI
elif settings.DEFAULT_AVATAR_URI is not None:
return settings.DEFAULT_AVATAR_URI
else:
return staticfiles_storage.url("images/default-avatar.png")
def _get_unversioned_avatar_url(

View File

@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from zerver.lib.avatar_hash import gravatar_hash
from zerver.lib.upload import upload_backend
@ -15,5 +16,7 @@ def get_realm_icon_url(realm: Realm) -> str:
elif settings.ENABLE_GRAVATAR:
hash_key = gravatar_hash(realm.string_id)
return f"https://secure.gravatar.com/avatar/{hash_key}?d=identicon"
elif settings.DEFAULT_AVATAR_URI is not None:
return settings.DEFAULT_AVATAR_URI
else:
return settings.DEFAULT_AVATAR_URI + "?version=0"
return staticfiles_storage.url("images/default-avatar.png") + "?version=0"

View File

@ -1,6 +1,7 @@
from typing import Any, Dict
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from zerver.lib.upload import upload_backend
from zerver.models import Realm
@ -23,7 +24,9 @@ def get_realm_logo_url(realm: Realm, night: bool) -> str:
else:
logo_version = realm.logo_version
return upload_backend.get_realm_logo_url(realm.id, logo_version, night)
return settings.DEFAULT_LOGO_URI + "?version=0"
if settings.DEFAULT_LOGO_URI is not None:
return settings.DEFAULT_LOGO_URI
return staticfiles_storage.url("images/logo/zulip-org-logo.svg") + "?version=0"
def get_realm_logo_data(realm: Realm, night: bool) -> Dict[str, Any]:

View File

@ -1123,6 +1123,18 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
redirect_url = response["Location"]
self.assertTrue(redirect_url.endswith(str(avatar_url(cordelia)) + "&foo=bar"))
def test_get_settings_avatar(self) -> None:
self.login("hamlet")
cordelia = self.example_user("cordelia")
cordelia.email = cordelia.delivery_email
cordelia.save()
with self.settings(
ENABLE_GRAVATAR=False, DEFAULT_AVATAR_URI="http://other.server/avatar.svg"
):
response = self.client_get("/avatar/cordelia@zulip.com", {"foo": "bar"})
redirect_url = response["Location"]
self.assertEqual(redirect_url, "http://other.server/avatar.svg?version=1&foo=bar")
def test_get_user_avatar(self) -> None:
hamlet = self.example_user("hamlet")
self.login_user(hamlet)
@ -1530,7 +1542,16 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase):
redirect_url = response["Location"]
self.assertTrue(redirect_url.endswith(realm_icon_url(realm) + "&foo=bar"))
def test_get_realm_icon(self) -> None:
def test_get_settings_realm_icon(self) -> None:
self.login("hamlet")
with self.settings(
ENABLE_GRAVATAR=False, DEFAULT_AVATAR_URI="http://other.server/icon.svg"
):
response = self.client_get("/json/realm/icon", {"foo": "bar"})
redirect_url = response["Location"]
self.assertEqual(redirect_url, "http://other.server/icon.svg?foo=bar")
def test_get_uploaded_realm_icon(self) -> None:
self.login("hamlet")
realm = get_realm("zulip")
@ -1675,9 +1696,22 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):
redirect_url = response["Location"]
is_night_str = str(self.night).lower()
self.assertEqual(
redirect_url, f"/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}"
redirect_url,
f"http://testserver/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}",
)
def test_get_settings_logo(self) -> None:
self.login("hamlet")
with self.settings(DEFAULT_LOGO_URI="http://other.server/logo.svg"):
response = self.client_get(
"/json/realm/logo", {"night": orjson.dumps(self.night).decode()}
)
redirect_url = response["Location"]
self.assertEqual(
redirect_url,
f"http://other.server/logo.svg?night={str(self.night).lower()}",
)
def test_get_realm_logo(self) -> None:
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
@ -1710,7 +1744,8 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):
response = self.client_get("/json/realm/logo", {"night": orjson.dumps(self.night).decode()})
redirect_url = response["Location"]
self.assertEqual(
redirect_url, f"/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}"
redirect_url,
f"http://testserver/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}",
)
def test_valid_logos(self) -> None:

View File

@ -133,8 +133,8 @@ SENTRY_DSN: Optional[str] = None
# File uploads and avatars
# TODO: Rename MAX_FILE_UPLOAD_SIZE to have unit in name.
DEFAULT_AVATAR_URI = "/static/images/default-avatar.png"
DEFAULT_LOGO_URI = "/static/images/logo/zulip-org-logo.svg"
DEFAULT_AVATAR_URI: Optional[str] = None
DEFAULT_LOGO_URI: Optional[str] = None
S3_AVATAR_BUCKET = ""
S3_AUTH_UPLOADS_BUCKET = ""
S3_REGION: Optional[str] = None