mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Add annotations for zerver/lib/validator.
This commit is contained in:
		@@ -28,24 +28,32 @@ for any particular type of object.
 | 
			
		||||
from __future__ import absolute_import
 | 
			
		||||
from django.utils.translation import ugettext as _
 | 
			
		||||
import six
 | 
			
		||||
from typing import Any, Callable, Iterable, Optional, Tuple, TypeVar
 | 
			
		||||
 | 
			
		||||
Validator = Callable[[str, Any], Optional[str]]
 | 
			
		||||
 | 
			
		||||
def check_string(var_name, val):
 | 
			
		||||
    # type: (str, Any) -> Optional[str]
 | 
			
		||||
    if not isinstance(val, six.string_types):
 | 
			
		||||
        return _('%s is not a string') % (var_name,)
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
def check_int(var_name, val):
 | 
			
		||||
    # type: (str, Any) -> Optional[str]
 | 
			
		||||
    if not isinstance(val, int):
 | 
			
		||||
        return _('%s is not an integer') % (var_name,)
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
def check_bool(var_name, val):
 | 
			
		||||
    # type: (str, Any) -> Optional[str]
 | 
			
		||||
    if not isinstance(val, bool):
 | 
			
		||||
        return _('%s is not a boolean') % (var_name,)
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
def check_none_or(sub_validator):
 | 
			
		||||
    # type: (Validator) -> Validator
 | 
			
		||||
    def f(var_name, val):
 | 
			
		||||
        # type: (str, Any) -> Optional[str]
 | 
			
		||||
        if val is None:
 | 
			
		||||
            return None
 | 
			
		||||
        else:
 | 
			
		||||
@@ -53,7 +61,9 @@ def check_none_or(sub_validator):
 | 
			
		||||
    return f
 | 
			
		||||
 | 
			
		||||
def check_list(sub_validator, length=None):
 | 
			
		||||
    # type: (Validator, Optional[int]) -> Validator
 | 
			
		||||
    def f(var_name, val):
 | 
			
		||||
        # type: (str, Any) -> Optional[str]
 | 
			
		||||
        if not isinstance(val, list):
 | 
			
		||||
            return _('%s is not a list') % (var_name,)
 | 
			
		||||
 | 
			
		||||
@@ -72,10 +82,9 @@ def check_list(sub_validator, length=None):
 | 
			
		||||
    return f
 | 
			
		||||
 | 
			
		||||
def check_dict(required_keys):
 | 
			
		||||
    # required_keys is a list of tuples of
 | 
			
		||||
    # key_name/validator
 | 
			
		||||
 | 
			
		||||
    # type: (Iterable[Tuple[str, Validator]]) -> Validator
 | 
			
		||||
    def f(var_name, val):
 | 
			
		||||
        # type: (str, Any) -> Optional[str]
 | 
			
		||||
        if not isinstance(val, dict):
 | 
			
		||||
            return _('%s is not a dict') % (var_name,)
 | 
			
		||||
 | 
			
		||||
@@ -93,6 +102,7 @@ def check_dict(required_keys):
 | 
			
		||||
    return f
 | 
			
		||||
 | 
			
		||||
def check_variable_type(allowed_type_funcs):
 | 
			
		||||
    # type: (Iterable[Validator]) -> Validator
 | 
			
		||||
    """
 | 
			
		||||
    Use this validator if an argument is of a variable type (e.g. processing
 | 
			
		||||
    properties that might be strings or booleans).
 | 
			
		||||
@@ -101,6 +111,7 @@ def check_variable_type(allowed_type_funcs):
 | 
			
		||||
    types for this variable.
 | 
			
		||||
    """
 | 
			
		||||
    def enumerated_type_check(var_name, val):
 | 
			
		||||
        # type: (str, Any) -> str
 | 
			
		||||
        for func in allowed_type_funcs:
 | 
			
		||||
            if not func(var_name, val):
 | 
			
		||||
                return None
 | 
			
		||||
@@ -108,7 +119,9 @@ def check_variable_type(allowed_type_funcs):
 | 
			
		||||
    return enumerated_type_check
 | 
			
		||||
 | 
			
		||||
def equals(expected_val):
 | 
			
		||||
    # type: (Any) -> Validator
 | 
			
		||||
    def f(var_name, val):
 | 
			
		||||
        # type: (str, Any) -> Optional[str]
 | 
			
		||||
        if val != expected_val:
 | 
			
		||||
            return (_('%(variable)s != %(expected_value)s (%(value)s is wrong)') %
 | 
			
		||||
                    {'variable': var_name,
 | 
			
		||||
 
 | 
			
		||||
@@ -183,7 +183,7 @@ def list_subscriptions_backend(request, user_profile):
 | 
			
		||||
@has_request_variables
 | 
			
		||||
def update_subscriptions_backend(request, user_profile,
 | 
			
		||||
                                 delete=REQ(validator=check_list(check_string), default=[]),
 | 
			
		||||
                                 add=REQ(validator=check_list(check_dict([['name', check_string]])), default=[])):
 | 
			
		||||
                                 add=REQ(validator=check_list(check_dict([('name', check_string)])), default=[])):
 | 
			
		||||
    # type: (HttpRequest, UserProfile, List[str], List[Dict[str, str]]) -> HttpResponse
 | 
			
		||||
    if not add and not delete:
 | 
			
		||||
        return json_error(_('Nothing to do. Specify at least one of "add" or "delete".'))
 | 
			
		||||
@@ -279,7 +279,7 @@ def stream_button(stream_name):
 | 
			
		||||
@has_request_variables
 | 
			
		||||
def add_subscriptions_backend(request, user_profile,
 | 
			
		||||
                              streams_raw = REQ("subscriptions",
 | 
			
		||||
                              validator=check_list(check_dict([['name', check_string]]))),
 | 
			
		||||
                              validator=check_list(check_dict([('name', check_string)]))),
 | 
			
		||||
                              invite_only = REQ(validator=check_bool, default=False),
 | 
			
		||||
                              announce = REQ(validator=check_bool, default=False),
 | 
			
		||||
                              principals = REQ(validator=check_list(check_string), default=None),
 | 
			
		||||
@@ -464,10 +464,10 @@ def get_subscription_or_die(stream_name, user_profile):
 | 
			
		||||
@has_request_variables
 | 
			
		||||
def json_subscription_property(request, user_profile, subscription_data=REQ(
 | 
			
		||||
        validator=check_list(
 | 
			
		||||
            check_dict([["stream", check_string],
 | 
			
		||||
                        ["property", check_string],
 | 
			
		||||
                        ["value", check_variable_type(
 | 
			
		||||
                            [check_string, check_bool])]])))):
 | 
			
		||||
            check_dict([("stream", check_string),
 | 
			
		||||
                        ("property", check_string),
 | 
			
		||||
                        ("value", check_variable_type(
 | 
			
		||||
                            [check_string, check_bool]))])))):
 | 
			
		||||
    # type: (HttpRequest, UserProfile, List[Dict[str, Any]]) -> HttpResponse
 | 
			
		||||
    """
 | 
			
		||||
    This is the entry point to changing subscription properties. This
 | 
			
		||||
 
 | 
			
		||||
@@ -13,9 +13,9 @@ def api_travis_webhook(request, user_profile, client,
 | 
			
		||||
                       stream=REQ(default='travis'),
 | 
			
		||||
                       topic=REQ(default=None),
 | 
			
		||||
                       message=REQ('payload', validator=check_dict([
 | 
			
		||||
                           ['author_name', check_string],
 | 
			
		||||
                           ['status_message', check_string],
 | 
			
		||||
                           ['compare_url', check_string],
 | 
			
		||||
                           ('author_name', check_string),
 | 
			
		||||
                           ('status_message', check_string),
 | 
			
		||||
                           ('compare_url', check_string),
 | 
			
		||||
                       ]))):
 | 
			
		||||
    author = message['author_name']
 | 
			
		||||
    message_type = message['status_message']
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user