예제 #1
0
def check_schema_wrapper_for_serializer_method_field(node: ast.ClassDef,
                                                     file_path: str) -> Errors:
    """SerializerMethodField должны быть обернуты в SchemaWrapper (если функция возвращает не str)."""
    if _is_api_serializer(node, file_path) is False:
        return []

    errors = []

    for assign in get_classdef_assignments(node):
        assign_value = assign.value
        if isinstance(assign_value, ast.Call) is False:
            continue

        if assign_value.func.id != 'SerializerMethodField':  # type: ignore
            continue

        assign_field_name = get_assign_name(assign)
        serializer_field_method = get_serializer_field_method(
            node, assign_field_name)
        if serializer_field_method is None:
            continue

        return_node = serializer_field_method.returns
        if _is_allowed_return_type(return_node) is False:  # type: ignore
            errors.append(
                f':{assign.lineno} {node.name} serializer {assign_field_name} field missing SchemaWrapper'
            )

    return errors
예제 #2
0
def _check_classdef_hasattr(node: ast.ClassDef, attribute: str) -> bool:
    for assign in get_classdef_assignments(node):
        assign_targets_names = [target.id
                                for target in assign.targets]  # type: ignore

        if attribute in assign_targets_names:
            return True

    return False
예제 #3
0
def _check_classdef_hasattr(node: ast.ClassDef, attribute: str) -> bool:
    for assign in get_classdef_assignments(node):
        if isinstance(assign, ast.AnnAssign):
            if attribute == assign.target.id:  # type: ignore
                return True
            continue
        assign_targets_names = [target.id
                                for target in assign.targets]  # type: ignore

        if attribute in assign_targets_names:
            return True

    return False
예제 #4
0
def check_help_text_attribute_in_serializer_fields(node: ast.ClassDef,
                                                   file_path: str) -> Errors:
    """Проверяет, что для атрибутов сериализаторов определен help_text."""
    if _is_api_serializer(node, file_path) is False:
        return []

    errors = []

    for assign in get_classdef_assignments(node):
        assign_value = assign.value
        if isinstance(assign_value, ast.Call) is False:
            continue

        assign_error = _check_help_text(typing.cast(ast.Call, assign_value))
        if assign_error:
            errors.append(assign_error)

    return errors
예제 #5
0
def check_viewset_lookup_field_has_valid_value(node: ast.ClassDef,
                                               *args: typing.Any) -> Errors:
    """Проверяет, что ViewSet.lookup_field != ‘id’."""
    allowed_lookup_fields = ['uuid']
    if _is_api_viewset(node) is False:
        return []

    for assign in get_classdef_assignments(node):
        if get_assign_name(assign) != 'lookup_field':
            continue

        if isinstance(assign.value, ast.Constant) is False:
            continue

        assign_value = assign.value.value  # type: ignore
        if assign_value not in allowed_lookup_fields:
            error = (
                f':{node.lineno} {node.name} viewset has forbidden `lookup_field`. '
                f'Choose from: {allowed_lookup_fields}')
            return [error]

    return []