2016-12-25 00:44:26 +01:00
|
|
|
from __future__ import absolute_import
|
2017-01-17 08:38:25 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
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-12-25 00:44:26 +01:00
|
|
|
from typing import Text
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
from zerver.lib.upload import upload_emoji_image
|
2016-06-02 23:01:28 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-03-13 05:45:50 +01:00
|
|
|
from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name, check_valid_emoji, \
|
|
|
|
get_emoji_file_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
|
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2016-02-12 21:08:56 +01:00
|
|
|
def list_emoji(request, user_profile):
|
2016-06-02 23:01:28 +02:00
|
|
|
# type: (HttpRequest, 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
|
|
|
|
2017-01-17 08:38:25 +01:00
|
|
|
@has_request_variables
|
2017-03-13 05:45:50 +01:00
|
|
|
def upload_emoji(request, user_profile, emoji_name=REQ()):
|
|
|
|
# type: (HttpRequest, UserProfile, Text) -> HttpResponse
|
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)
|
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))
|
|
|
|
emoji_file_name = get_emoji_file_name(emoji_file.name, emoji_name)
|
|
|
|
upload_emoji_image(emoji_file, emoji_file_name, user_profile)
|
|
|
|
try:
|
|
|
|
check_add_realm_emoji(user_profile.realm, emoji_name, emoji_file_name, author=user_profile)
|
|
|
|
except ValidationError as e:
|
|
|
|
return json_error(e.messages[0])
|
2016-02-12 21:08:56 +01:00
|
|
|
return json_success()
|
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2016-02-12 21:08:56 +01:00
|
|
|
def delete_emoji(request, user_profile, emoji_name):
|
2016-12-25 00:44:26 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Text) -> HttpResponse
|
2017-01-17 08:48:07 +01:00
|
|
|
check_valid_emoji(user_profile.realm, 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()
|