2016-12-25 00:44:26 +01:00
|
|
|
from __future__ import absolute_import
|
2016-06-02 23:01:28 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-02-12 21:08:56 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
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
|
|
|
|
2016-06-02 23:01:28 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-12-20 15:41:30 +01:00
|
|
|
from zerver.lib.request import JsonableError
|
2016-02-12 21:08:56 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
|
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
def check_emoji_admin(user_profile):
|
|
|
|
# type: (UserProfile) -> None
|
|
|
|
if user_profile.realm.add_emoji_by_admins_only and not user_profile.is_realm_admin:
|
|
|
|
raise JsonableError(_("Must be a realm administrator"))
|
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()})
|
|
|
|
|
|
|
|
def upload_emoji(request, user_profile):
|
2016-06-02 23:01:28 +02:00
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2016-12-20 15:41:30 +01:00
|
|
|
check_emoji_admin(user_profile)
|
2016-02-12 21:08:56 +01:00
|
|
|
emoji_name = request.POST.get('name', None)
|
|
|
|
emoji_url = request.POST.get('url', None)
|
|
|
|
try:
|
2016-12-20 09:22:00 +01:00
|
|
|
check_add_realm_emoji(user_profile.realm, emoji_name, emoji_url, author=user_profile)
|
2016-02-12 21:08:56 +01:00
|
|
|
except ValidationError as e:
|
2016-06-04 08:02:40 +02:00
|
|
|
return json_error(e.messages[0])
|
2016-02-12 21:08:56 +01:00
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def delete_emoji(request, user_profile, emoji_name):
|
2016-12-25 00:44:26 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Text) -> HttpResponse
|
2016-12-20 15:41:30 +01:00
|
|
|
check_emoji_admin(user_profile)
|
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()
|