Beispiel #1
0
def upload_emoji(
    request: HttpRequest,
    user_profile: UserProfile,
    emoji_name: str = REQ(path_only=True)
) -> HttpResponse:
    emoji_name = emoji_name.strip().replace(" ", "_")
    valid_built_in_emoji = name_to_codepoint.keys()
    check_valid_emoji_name(emoji_name)

    if not user_profile.can_add_custom_emoji():
        raise JsonableError(_("Insufficient permission"))

    if RealmEmoji.objects.filter(realm=user_profile.realm,
                                 name=emoji_name,
                                 deactivated=False).exists():
        raise JsonableError(_("A custom emoji with this name already exists."))
    if len(request.FILES) != 1:
        raise JsonableError(_("You must upload exactly one file."))
    if emoji_name in valid_built_in_emoji:
        if not user_profile.is_realm_admin:
            raise JsonableError(
                _("Only administrators can override built-in emoji."))
    emoji_file = list(request.FILES.values())[0]
    assert isinstance(emoji_file, UploadedFile)
    assert emoji_file.size is not None
    if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:
        raise JsonableError(
            _("Uploaded file is larger than the allowed limit of {} MiB").
            format(settings.MAX_EMOJI_FILE_SIZE_MIB, ))

    check_add_realm_emoji(user_profile.realm, emoji_name, user_profile,
                          emoji_file)
    return json_success(request)
Beispiel #2
0
 def validation_func(user_profile: UserProfile) -> bool:
     user_profile.refresh_from_db()
     return user_profile.can_add_custom_emoji()