Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support in query in v3 apis #261

Merged
merged 12 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/api/bkuser_core/apis/v3/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""


class QueryTooLong(Exception):
"""Query too long"""
109 changes: 109 additions & 0 deletions src/api/bkuser_core/apis/v3/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import logging
from dataclasses import dataclass, field
from typing import ClassVar, Generator, List, Optional

from django.db.models import CharField, DateTimeField, FloatField, IntegerField, TextField
from rest_framework import filters, viewsets

from .exceptions import QueryTooLong

logger = logging.getLogger(__name__)


@dataclass
class FieldFilter:
view_set: viewsets.ViewSet
limit: ClassVar[Optional[int]] = None

def extract_fields(self, queryset) -> Generator[str, None, None]:
raise NotImplementedError

def get_key(self, key: str) -> str:
raise NotImplementedError

def gen_query(self, queryset, params: dict) -> dict:

query = {}
for key, value in params.items():
if key not in list(self.extract_fields(queryset)):
continue

if self.limit and len(value) > self.limit:
raise QueryTooLong(f"当前过滤长度[{len(value)}]超过字段[{key}]限制[{self.limit}]")

query[self.get_key(key)] = value

return query


class ForeignFieldFilter(FieldFilter):
limit = 100

def extract_fields(self, queryset) -> Generator[str, None, None]:
return (x for x in getattr(self.view_set, "foreign_fields", []))

def get_key(self, key: str) -> str:
return f"{key}__in"


class IContainFieldFilter(FieldFilter):
limit = 100

def extract_fields(self, queryset) -> Generator[str, None, None]:
available_fields = queryset.model._meta.get_fields()
for f in available_fields:
if isinstance(f, (CharField, TextField, DateTimeField)):
yield f.name

def get_key(self, key) -> str:
return f"{key}__icontains"

IMBlues marked this conversation as resolved.
Show resolved Hide resolved

class PlainFieldFilter(FieldFilter):
def extract_fields(self, queryset) -> Generator[str, None, None]:
available_fields = queryset.model._meta.get_fields()
for f in available_fields:
if isinstance(f, (IntegerField, FloatField)):
yield f.name

def get_key(self, key) -> str:
return key


class InFieldFilter(FieldFilter):
limit = 1000

def extract_fields(self, queryset) -> Generator[str, None, None]:
return (x for x in getattr(self.view_set, "in_fields", []))

def get_key(self, key: str) -> str:
return key


@dataclass
class MultipleFieldFilter(filters.SearchFilter):
"""多字段过滤器, 同时支持标准和非标准过滤"""

field_filters: List = field(
default_factory=lambda: [ForeignFieldFilter, IContainFieldFilter, PlainFieldFilter, InFieldFilter]
)

def filter_by_params(self, queryset, params: dict, view):
"""根据不同字段类型进行多字段过滤"""

query = {}
for f_filter in self.field_filters:
query.update(f_filter(view).gen_query(queryset, params))

# in operator on many-to-many fields may cause duplicate results, so we use distinct
logger.debug(f"trying to query via multiple fields: {query}")
return queryset.filter(**query).distinct()
2 changes: 1 addition & 1 deletion src/api/bkuser_core/apis/v3/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def __init__(self, **kwargs):
def to_internal_value(self, data):
# convert string to list
data = super().to_internal_value(data)
return data.split(self.delimiter)
return [x for x in data.split(self.delimiter) if x]
2 changes: 1 addition & 1 deletion src/api/bkuser_core/bkiam/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from bkuser_core.categories.models import ProfileCategory
from bkuser_core.categories.serializers import CategorySerializer
from bkuser_core.departments.models import Department
from bkuser_core.departments.serializers import DepartmentSerializer
from bkuser_core.departments.v2.serializers import DepartmentSerializer
from bkuser_core.profiles.models import DynamicFieldInfo
from bkuser_core.profiles.v2.serializers import DynamicFieldsSerializer

Expand Down
1 change: 1 addition & 0 deletions src/api/bkuser_core/common/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __getattr__(self, code_name):
# 通用
ErrorCode("FIELDS_NOT_SUPPORTED_YET", _("存在不支持动态返回的字段")),
ErrorCode("QUERY_PARAMS_ERROR", _("查询参数错误,请检查")),
ErrorCode("QUERY_TOO_LONG", _("查询参数过长")),
ErrorCode("USERNAME_MISSING", _("用户名信息缺失")),
ErrorCode("RESOURCE_ALREADY_ENABLED", _("资源数据未被删除")),
ErrorCode("RESOURCE_RESTORATION_FAILED", _("资源恢复失败")),
Expand Down
92 changes: 3 additions & 89 deletions src/api/bkuser_core/departments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,8 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from bkuser_core.apis.v2.constants import LOOKUP_FIELD_NAME
from django.conf.urls import url

from . import views
from bkuser_core.departments.v2.urls import urlpatterns as v2_urlpatterns
from bkuser_core.departments.v3.urls import urlpatterns as v3_urlpatterns

PVAR_DEPARTMENT_ID = r"(?P<%s>[\w\-\.]+)" % LOOKUP_FIELD_NAME

urlpatterns = [
url(
r"^api/v2/departments/$",
views.DepartmentViewSet.as_view(
{
"get": "list",
"post": "create",
}
),
name="departments",
),
url(
r"^api/v2/departments/%s/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "retrieve",
"post": "update",
"delete": "destroy",
"patch": "partial_update",
}
),
name="departments.action",
),
url(
r"^api/v2/departments/%s/restoration/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"post": "restoration",
}
),
name="departments.restoration",
),
url(
r"^api/v2/departments/%s/ancestors/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "get_ancestor",
}
),
name="departments.ancestors",
),
url(
r"^api/v2/departments/%s/children/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "get_children",
}
),
name="departments.children",
),
url(
r"^api/v2/departments/%s/profiles/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view({"get": "get_profiles", "post": "add_profiles"}),
name="departments.profiles",
),
#########
# Batch #
#########
url(
r"^api/v2/batch/departments/profiles/$",
views.BatchDepartmentsViewSet.as_view(
{
"get": "multiple_retrieve_profiles",
}
),
name="department.batch",
),
########
# Edge #
########
url(
r"^api/v2/edges/department_profile/$",
views.DepartmentProfileEdgeViewSet.as_view({"get": "list"}),
name="edge.department_profile",
),
#############
# shortcuts #
#############
url(
r"^api/v2/shortcuts/departments/tops/$",
views.DepartmentViewSet.as_view({"get": "list_tops"}),
name="shortcuts.departments.list.tops",
),
]
urlpatterns = v2_urlpatterns + v3_urlpatterns
10 changes: 10 additions & 0 deletions src/api/bkuser_core/departments/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
CustomFieldsMixin,
CustomFieldsModelSerializer,
)
from bkuser_core.departments.models import Department
from django.utils.translation import ugettext as _
from rest_framework import serializers

from .models import Department

# ===============================================================================
# Response
# ===============================================================================
Expand Down
100 changes: 100 additions & 0 deletions src/api/bkuser_core/departments/v2/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from bkuser_core.apis.v2.constants import LOOKUP_FIELD_NAME
from bkuser_core.departments.v2 import views
from django.conf.urls import url

PVAR_DEPARTMENT_ID = r"(?P<%s>[\w\-\.]+)" % LOOKUP_FIELD_NAME

urlpatterns = [
url(
r"^api/v2/departments/$",
views.DepartmentViewSet.as_view(
{
"get": "list",
"post": "create",
}
),
name="departments",
),
url(
r"^api/v2/departments/%s/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "retrieve",
"post": "update",
"delete": "destroy",
"patch": "partial_update",
}
),
name="departments.action",
),
url(
r"^api/v2/departments/%s/restoration/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"post": "restoration",
}
),
name="departments.restoration",
),
url(
r"^api/v2/departments/%s/ancestors/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "get_ancestor",
}
),
name="departments.ancestors",
),
url(
r"^api/v2/departments/%s/children/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view(
{
"get": "get_children",
}
),
name="departments.children",
),
url(
r"^api/v2/departments/%s/profiles/$" % PVAR_DEPARTMENT_ID,
views.DepartmentViewSet.as_view({"get": "get_profiles", "post": "add_profiles"}),
name="departments.profiles",
),
#########
# Batch #
#########
url(
r"^api/v2/batch/departments/profiles/$",
views.BatchDepartmentsViewSet.as_view(
{
"get": "multiple_retrieve_profiles",
}
),
name="department.batch",
),
########
# Edge #
########
url(
r"^api/v2/edges/department_profile/$",
views.DepartmentProfileEdgeViewSet.as_view({"get": "list"}),
name="edge.department_profile",
),
#############
# shortcuts #
#############
url(
r"^api/v2/shortcuts/departments/tops/$",
views.DepartmentViewSet.as_view({"get": "list_tops"}),
name="shortcuts.departments.list.tops",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from bkuser_core.categories.models import ProfileCategory
from bkuser_core.common.cache import clear_cache_if_succeed
from bkuser_core.common.error_codes import error_codes
from bkuser_core.departments import serializers as local_serializers
from bkuser_core.departments.models import Department, DepartmentThroughModel
from bkuser_core.departments.signals import post_department_create
from bkuser_core.departments.v2 import serializers as local_serializers
from bkuser_core.profiles.models import DynamicFieldInfo, Profile
from bkuser_core.profiles.utils import force_use_raw_username
from bkuser_core.profiles.v2.serializers import ProfileMinimalSerializer, ProfileSerializer, RapidProfileSerializer
Expand Down
10 changes: 10 additions & 0 deletions src/api/bkuser_core/departments/v3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
Loading