2018-08-16 01:26:55 +02:00
|
|
|
from django.conf import settings
|
2022-06-21 22:23:34 +02:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-08-16 01:26:55 +02:00
|
|
|
from django.shortcuts import redirect
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2022-04-14 23:37:16 +02:00
|
|
|
from zerver.actions.realm_logo import do_change_logo_source
|
2018-08-16 01:26:55 +02:00
|
|
|
from zerver.decorator import require_realm_admin
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-08-19 19:46:45 +02:00
|
|
|
from zerver.lib.realm_logo import get_realm_logo_url
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2018-08-16 01:26:55 +02:00
|
|
|
from zerver.lib.upload import upload_logo_image
|
2021-10-14 01:45:34 +02:00
|
|
|
from zerver.lib.url_encoding import append_url_query_string
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.validator import check_bool
|
2020-05-09 19:46:56 +02:00
|
|
|
from zerver.models import UserProfile
|
2018-08-16 01:26:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2019-01-27 08:25:10 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def upload_logo(
|
2021-04-07 22:00:44 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool)
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-05-19 14:38:43 +02:00
|
|
|
user_profile.realm.ensure_not_on_limited_plan()
|
2018-08-16 01:26:55 +02:00
|
|
|
|
|
|
|
if len(request.FILES) != 1:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("You must upload exactly one logo."))
|
2018-08-16 01:26:55 +02:00
|
|
|
logo_file = list(request.FILES.values())[0]
|
2022-06-21 22:23:34 +02:00
|
|
|
assert isinstance(logo_file, UploadedFile)
|
|
|
|
assert logo_file.size is not None
|
2021-05-29 08:59:21 +02:00
|
|
|
if (settings.MAX_LOGO_FILE_SIZE_MIB * 1024 * 1024) < logo_file.size:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Uploaded file is larger than the allowed limit of {} MiB").format(
|
2021-05-29 08:59:21 +02:00
|
|
|
settings.MAX_LOGO_FILE_SIZE_MIB,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2019-01-27 08:25:10 +01:00
|
|
|
upload_logo_image(logo_file, user_profile, night)
|
2021-02-12 08:19:30 +01:00
|
|
|
do_change_logo_source(
|
|
|
|
user_profile.realm, user_profile.realm.LOGO_UPLOADED, night, acting_user=user_profile
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-16 01:26:55 +02:00
|
|
|
@require_realm_admin
|
2019-01-27 08:25:10 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def delete_logo_backend(
|
2021-04-07 22:00:44 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool)
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-08-16 01:26:55 +02:00
|
|
|
# We don't actually delete the logo because it might still
|
2020-08-11 01:47:44 +02:00
|
|
|
# be needed if the URL was cached and it is rewritten
|
2018-08-16 01:26:55 +02:00
|
|
|
# in any case after next update.
|
2021-02-12 08:19:30 +01:00
|
|
|
do_change_logo_source(
|
|
|
|
user_profile.realm, user_profile.realm.LOGO_DEFAULT, night, acting_user=user_profile
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-01-27 08:25:10 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_logo_backend(
|
2021-04-07 22:00:44 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, night: bool = REQ(json_validator=check_bool)
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2019-08-19 19:46:45 +02:00
|
|
|
url = get_realm_logo_url(user_profile.realm, night)
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# We can rely on the URL already having query parameters. Because
|
2018-08-16 01:26:55 +02:00
|
|
|
# our templates depend on being able to use the ampersand to
|
|
|
|
# add query parameters to our url, get_logo_url does '?version=version_number'
|
|
|
|
# hacks to prevent us from having to jump through decode/encode hoops.
|
2021-10-14 01:45:34 +02:00
|
|
|
url = append_url_query_string(url, request.META["QUERY_STRING"])
|
2018-08-16 01:26:55 +02:00
|
|
|
return redirect(url)
|