2017-03-13 05:45:50 +01:00
|
|
|
from django.conf import settings
|
2016-06-02 23:01:28 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-12-20 15:41:30 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
from zerver.models import RealmEmoji, UserProfile
|
2019-02-02 23:53:22 +01:00
|
|
|
from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name
|
2017-01-17 08:38:25 +01:00
|
|
|
from zerver.lib.request import JsonableError, REQ, has_request_variables
|
2017-03-13 05:45:50 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2016-02-12 21:08:56 +01:00
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
|
2018-06-08 15:30:44 +02:00
|
|
|
from zerver.decorator import require_non_guest_human_user
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2016-12-20 15:41:30 +01:00
|
|
|
|
|
|
|
# We don't call check_emoji_admin here because the list of realm
|
|
|
|
# emoji is public.
|
2016-02-12 21:08:56 +01:00
|
|
|
return json_success({'emoji': user_profile.realm.get_emoji()})
|
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2018-06-08 15:30:44 +02:00
|
|
|
@require_non_guest_human_user
|
2017-01-17 08:38:25 +01:00
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def upload_emoji(request: HttpRequest, user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
emoji_name: str=REQ()) -> HttpResponse:
|
2018-08-04 11:04:11 +02:00
|
|
|
emoji_name = emoji_name.strip().replace(' ', '_')
|
2017-01-17 08:38:25 +01:00
|
|
|
check_valid_emoji_name(emoji_name)
|
2016-12-20 15:41:30 +01:00
|
|
|
check_emoji_admin(user_profile)
|
2018-03-11 18:55:20 +01:00
|
|
|
if RealmEmoji.objects.filter(realm=user_profile.realm,
|
|
|
|
name=emoji_name,
|
|
|
|
deactivated=False).exists():
|
|
|
|
return json_error(_("A custom emoji with this name already exists."))
|
2017-03-13 05:45:50 +01:00
|
|
|
if len(request.FILES) != 1:
|
|
|
|
return json_error(_("You must upload exactly one file."))
|
|
|
|
emoji_file = list(request.FILES.values())[0]
|
|
|
|
if (settings.MAX_EMOJI_FILE_SIZE * 1024 * 1024) < emoji_file.size:
|
|
|
|
return json_error(_("Uploaded file is larger than the allowed limit of %s MB") % (
|
|
|
|
settings.MAX_EMOJI_FILE_SIZE))
|
2018-03-11 18:55:20 +01:00
|
|
|
|
|
|
|
realm_emoji = check_add_realm_emoji(user_profile.realm,
|
|
|
|
emoji_name,
|
|
|
|
user_profile,
|
|
|
|
emoji_file)
|
|
|
|
if realm_emoji is None:
|
|
|
|
return json_error(_("Image file upload failed."))
|
2016-02-12 21:08:56 +01:00
|
|
|
return json_success()
|
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def delete_emoji(request: HttpRequest, user_profile: UserProfile,
|
2018-03-11 18:55:20 +01:00
|
|
|
emoji_name: str) -> HttpResponse:
|
|
|
|
if not RealmEmoji.objects.filter(realm=user_profile.realm,
|
|
|
|
name=emoji_name,
|
|
|
|
deactivated=False).exists():
|
2019-04-20 03:49:03 +02:00
|
|
|
raise JsonableError(_("Emoji '%s' does not exist") % (emoji_name,))
|
2017-05-18 21:53:33 +02:00
|
|
|
check_emoji_admin(user_profile, emoji_name)
|
2016-02-12 21:08:56 +01:00
|
|
|
do_remove_realm_emoji(user_profile.realm, emoji_name)
|
2016-10-21 07:34:04 +02:00
|
|
|
return json_success()
|