2017-03-06 06:22:28 +01:00
|
|
|
from django.conf import settings
|
2017-02-21 03:41:20 +01:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.http import HttpResponse, HttpRequest
|
|
|
|
|
|
|
|
from zerver.decorator import require_realm_admin
|
|
|
|
from zerver.lib.actions import do_change_icon_source
|
|
|
|
from zerver.lib.realm_icon import realm_icon_url
|
|
|
|
from zerver.lib.response import json_error, json_success
|
|
|
|
from zerver.lib.upload import upload_icon_image
|
2020-03-02 19:38:16 +01:00
|
|
|
from zerver.lib.url_encoding import add_query_arg_to_redirect_url
|
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:
|
|
|
|
return json_error(_("You must upload exactly one icon."))
|
|
|
|
|
|
|
|
icon_file = list(request.FILES.values())[0]
|
2017-03-06 06:22:28 +01:00
|
|
|
if ((settings.MAX_ICON_FILE_SIZE * 1024 * 1024) < icon_file.size):
|
|
|
|
return json_error(_("Uploaded file is larger than the allowed limit of %s MB") % (
|
|
|
|
settings.MAX_ICON_FILE_SIZE))
|
2017-02-21 03:41:20 +01:00
|
|
|
upload_icon_image(icon_file, user_profile)
|
|
|
|
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_UPLOADED)
|
|
|
|
icon_url = realm_icon_url(user_profile.realm)
|
|
|
|
|
|
|
|
json_result = dict(
|
|
|
|
icon_url=icon_url
|
|
|
|
)
|
|
|
|
return json_success(json_result)
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
# be needed if the URL was cached and it is rewrited
|
|
|
|
# in any case after next update.
|
|
|
|
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_FROM_GRAVATAR)
|
|
|
|
gravatar_url = realm_icon_url(user_profile.realm)
|
|
|
|
json_result = dict(
|
|
|
|
icon_url=gravatar_url
|
|
|
|
)
|
|
|
|
return json_success(json_result)
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# We can rely on the url already having query parameters. Because
|
|
|
|
# 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.
|
2020-03-02 19:38:16 +01:00
|
|
|
url = add_query_arg_to_redirect_url(url, request.META['QUERY_STRING'])
|
2017-02-21 03:41:20 +01:00
|
|
|
return redirect(url)
|