Files
zulip/zerver/views/realm_emoji.py
Tim Abbott 44db59bbe0 openapi: Fix handling of parameters passed via the URL/path.
Previously, our OpenAPI documentation validation was failing for some
endpoints because it didn't account for the `in: path` type of
parameter, resulting in a mismatch between what was declared via REQ
and what was declared in the OpenAPI docs.

We fix this by excluding the path type parameters in both places from
what's considered by documentation using the `path_only` flag.

I doubt this is the correct long-term fix; in particular, I don't
think we're actually running the validators for these path-only
parameters.  The examples that exist today are all IDs with validators
for being non-negative numbers, but longer-term I think we'll want to
do something different (possibly at the REQ layer, see the TODO).
2019-08-19 15:06:08 -07:00

56 lines
2.5 KiB
Python

from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from zerver.models import RealmEmoji, UserProfile
from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name
from zerver.lib.request import JsonableError, REQ, has_request_variables
from zerver.lib.response import json_success, json_error
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
from zerver.decorator import require_member_or_admin
def list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
# We don't call check_emoji_admin here because the list of realm
# emoji is public.
return json_success({'emoji': user_profile.realm.get_emoji()})
@require_member_or_admin
@has_request_variables
def upload_emoji(request: HttpRequest, user_profile: UserProfile,
emoji_name: str=REQ(path_only=True)) -> HttpResponse:
emoji_name = emoji_name.strip().replace(' ', '_')
check_valid_emoji_name(emoji_name)
check_emoji_admin(user_profile)
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."))
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))
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."))
return json_success()
def delete_emoji(request: HttpRequest, user_profile: UserProfile,
emoji_name: str) -> HttpResponse:
if not RealmEmoji.objects.filter(realm=user_profile.realm,
name=emoji_name,
deactivated=False).exists():
raise JsonableError(_("Emoji '%s' does not exist") % (emoji_name,))
check_emoji_admin(user_profile, emoji_name)
do_remove_realm_emoji(user_profile.realm, emoji_name)
return json_success()