2017-03-06 06:22:28 +01: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
|
2017-02-21 03:41:20 +01:00
|
|
|
from django.shortcuts import redirect
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-02-21 03:41:20 +01:00
|
|
|
|
2022-04-14 23:39:22 +02:00
|
|
|
from zerver.actions.realm_icon import do_change_icon_source
|
2017-02-21 03:41:20 +01:00
|
|
|
from zerver.decorator import require_realm_admin
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2017-02-21 03:41:20 +01:00
|
|
|
from zerver.lib.realm_icon import realm_icon_url
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2017-02-21 03:41:20 +01:00
|
|
|
from zerver.lib.upload import upload_icon_image
|
2021-10-14 01:45:34 +02:00
|
|
|
from zerver.lib.url_encoding import append_url_query_string
|
2017-02-21 03:41:20 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2017-10-27 02:18:49 +02:00
|
|
|
def upload_icon(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2017-02-21 03:41:20 +01:00
|
|
|
if len(request.FILES) != 1:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("You must upload exactly one icon."))
|
2017-02-21 03:41:20 +01:00
|
|
|
|
2023-07-22 00:34:11 +02:00
|
|
|
[icon_file] = request.FILES.values()
|
2022-06-21 22:23:34 +02:00
|
|
|
assert isinstance(icon_file, UploadedFile)
|
|
|
|
assert icon_file.size is not None
|
2024-01-29 00:52:43 +01:00
|
|
|
if icon_file.size > settings.MAX_ICON_FILE_SIZE_MIB * 1024 * 1024:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2023-07-17 22:40:33 +02:00
|
|
|
_("Uploaded file is larger than the allowed limit of {max_size} MiB").format(
|
|
|
|
max_size=settings.MAX_ICON_FILE_SIZE_MIB,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2017-02-21 03:41:20 +01:00
|
|
|
upload_icon_image(icon_file, user_profile)
|
2021-02-12 08:19:30 +01:00
|
|
|
do_change_icon_source(
|
|
|
|
user_profile.realm, user_profile.realm.ICON_UPLOADED, acting_user=user_profile
|
|
|
|
)
|
2017-02-21 03:41:20 +01:00
|
|
|
icon_url = realm_icon_url(user_profile.realm)
|
|
|
|
|
|
|
|
json_result = dict(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
icon_url=icon_url,
|
2017-02-21 03:41:20 +01:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=json_result)
|
2017-02-21 03:41:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2017-10-27 02:18:49 +02:00
|
|
|
def delete_icon_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2017-02-21 03:41:20 +01:00
|
|
|
# We don't actually delete the icon because it might still
|
2020-08-11 01:47:44 +02:00
|
|
|
# be needed if the URL was cached and it is rewritten
|
2017-02-21 03:41:20 +01:00
|
|
|
# in any case after next update.
|
2021-02-12 08:19:30 +01:00
|
|
|
do_change_icon_source(
|
|
|
|
user_profile.realm, user_profile.realm.ICON_FROM_GRAVATAR, acting_user=user_profile
|
|
|
|
)
|
2017-02-21 03:41:20 +01:00
|
|
|
gravatar_url = realm_icon_url(user_profile.realm)
|
|
|
|
json_result = dict(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
icon_url=gravatar_url,
|
2017-02-21 03:41:20 +01:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=json_result)
|
2017-02-21 03:41:20 +01:00
|
|
|
|
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def get_icon_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2017-02-21 03:41:20 +01:00
|
|
|
url = realm_icon_url(user_profile.realm)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# We can rely on the URL already having query parameters. Because
|
2017-02-21 03:41:20 +01:00
|
|
|
# our templates depend on being able to use the ampersand to
|
|
|
|
# add query parameters to our url, get_icon_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"])
|
2017-02-21 03:41:20 +01:00
|
|
|
return redirect(url)
|