python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -34,13 +34,17 @@ from zerver.lib.validator import (
from zerver.models import CustomProfileField, UserProfile, custom_profile_fields_for_realm
def list_realm_custom_profile_fields(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
def list_realm_custom_profile_fields(
request: HttpRequest, user_profile: UserProfile
) -> HttpResponse:
fields = custom_profile_fields_for_realm(user_profile.realm_id)
return json_success({'custom_fields': [f.as_dict() for f in fields]})
hint_validator = check_capped_string(CustomProfileField.HINT_MAX_LENGTH)
name_validator = check_capped_string(CustomProfileField.NAME_MAX_LENGTH)
def validate_field_name_and_hint(name: str, hint: str) -> None:
if not name.strip():
raise JsonableError(_("Label cannot be blank."))
@@ -51,8 +55,8 @@ def validate_field_name_and_hint(name: str, hint: str) -> None:
except ValidationError as error:
raise JsonableError(error.message)
def validate_custom_field_data(field_type: int,
field_data: ProfileFieldData) -> None:
def validate_custom_field_data(field_type: int, field_data: ProfileFieldData) -> None:
try:
if field_type == CustomProfileField.CHOICE:
# Choice type field must have at least have one choice
@@ -64,6 +68,7 @@ def validate_custom_field_data(field_type: int,
except ValidationError as error:
raise JsonableError(error.message)
def is_default_external_field(field_type: int, field_data: ProfileFieldData) -> bool:
if field_type != CustomProfileField.EXTERNAL_ACCOUNT:
return False
@@ -71,8 +76,10 @@ def is_default_external_field(field_type: int, field_data: ProfileFieldData) ->
return False
return True
def validate_custom_profile_field(name: str, hint: str, field_type: int,
field_data: ProfileFieldData) -> None:
def validate_custom_profile_field(
name: str, hint: str, field_type: int, field_data: ProfileFieldData
) -> None:
# Validate field data
validate_custom_field_data(field_type, field_data)
@@ -86,15 +93,17 @@ def validate_custom_profile_field(name: str, hint: str, field_type: int,
if field_type not in field_types:
raise JsonableError(_("Invalid field type."))
@require_realm_admin
@has_request_variables
def create_realm_custom_profile_field(request: HttpRequest,
user_profile: UserProfile,
name: str=REQ(default='', converter=lambda x: x.strip()),
hint: str=REQ(default=''),
field_data: ProfileFieldData=REQ(default={},
converter=orjson.loads),
field_type: int=REQ(validator=check_int)) -> HttpResponse:
def create_realm_custom_profile_field(
request: HttpRequest,
user_profile: UserProfile,
name: str = REQ(default='', converter=lambda x: x.strip()),
hint: str = REQ(default=''),
field_data: ProfileFieldData = REQ(default={}, converter=orjson.loads),
field_type: int = REQ(validator=check_int),
) -> HttpResponse:
validate_custom_profile_field(name, hint, field_type, field_data)
try:
if is_default_external_field(field_type, field_data):
@@ -117,27 +126,30 @@ def create_realm_custom_profile_field(request: HttpRequest,
except IntegrityError:
return json_error(_("A field with that label already exists."))
@require_realm_admin
def delete_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile,
field_id: int) -> HttpResponse:
def delete_realm_custom_profile_field(
request: HttpRequest, user_profile: UserProfile, field_id: int
) -> HttpResponse:
try:
field = CustomProfileField.objects.get(id=field_id)
except CustomProfileField.DoesNotExist:
return json_error(_('Field id {id} not found.').format(id=field_id))
do_remove_realm_custom_profile_field(realm=user_profile.realm,
field=field)
do_remove_realm_custom_profile_field(realm=user_profile.realm, field=field)
return json_success()
@require_realm_admin
@has_request_variables
def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile,
field_id: int,
name: str=REQ(default='', converter=lambda x: x.strip()),
hint: str=REQ(default=''),
field_data: ProfileFieldData=REQ(default={},
converter=orjson.loads),
) -> HttpResponse:
def update_realm_custom_profile_field(
request: HttpRequest,
user_profile: UserProfile,
field_id: int,
name: str = REQ(default='', converter=lambda x: x.strip()),
hint: str = REQ(default=''),
field_data: ProfileFieldData = REQ(default={}, converter=orjson.loads),
) -> HttpResponse:
realm = user_profile.realm
try:
field = CustomProfileField.objects.get(realm=realm, id=field_id)
@@ -150,29 +162,35 @@ def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserPr
validate_custom_profile_field(name, hint, field.field_type, field_data)
try:
try_update_realm_custom_profile_field(realm, field, name, hint=hint,
field_data=field_data)
try_update_realm_custom_profile_field(realm, field, name, hint=hint, field_data=field_data)
except IntegrityError:
return json_error(_('A field with that label already exists.'))
return json_success()
@require_realm_admin
@has_request_variables
def reorder_realm_custom_profile_fields(request: HttpRequest, user_profile: UserProfile,
order: List[int]=REQ(validator=check_list(
check_int))) -> HttpResponse:
def reorder_realm_custom_profile_fields(
request: HttpRequest,
user_profile: UserProfile,
order: List[int] = REQ(validator=check_list(check_int)),
) -> HttpResponse:
try_reorder_realm_custom_profile_fields(user_profile.realm, order)
return json_success()
@human_users_only
@has_request_variables
def remove_user_custom_profile_data(request: HttpRequest, user_profile: UserProfile,
data: List[int]=REQ(validator=check_list(
check_int))) -> HttpResponse:
def remove_user_custom_profile_data(
request: HttpRequest,
user_profile: UserProfile,
data: List[int] = REQ(validator=check_list(check_int)),
) -> HttpResponse:
for field_id in data:
check_remove_custom_profile_field_value(user_profile, field_id)
return json_success()
@human_users_only
@has_request_variables
def update_user_custom_profile_data(
@@ -180,10 +198,12 @@ def update_user_custom_profile_data(
user_profile: UserProfile,
data: List[Dict[str, Union[int, str, List[int]]]] = REQ(
validator=check_list(
check_dict_only([
('id', check_int),
('value', check_union([check_int, check_string, check_list(check_int)])),
]),
check_dict_only(
[
('id', check_int),
('value', check_union([check_int, check_string, check_list(check_int)])),
]
),
)
),
) -> HttpResponse: