Skip to content

Commit

Permalink
Merge pull request #56 from IMBlues/dev_blues
Browse files Browse the repository at this point in the history
fix: adding query_serializer for profiles batch retrieve #26
  • Loading branch information
IMBlues authored Sep 6, 2021
2 parents 51072dd + 76ef817 commit 831f0bf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/api/bkuser_core/categories/plugins/ldap/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
specific language governing permissions and limitations under the License.
"""
import logging
from typing import Optional

from bkuser_core.categories.constants import CategoryType
from bkuser_core.categories.loader import get_plugin_by_category
Expand All @@ -26,7 +25,7 @@


@receiver(post_category_create)
def create_sync_tasks(sender, category, creator: Optional[str], **kwargs):
def create_sync_tasks(sender, category, creator: str, **kwargs):
if category.type not in [CategoryType.LDAP.value, CategoryType.MAD.value]:
return

Expand All @@ -48,7 +47,7 @@ def delete_sync_tasks(sender, category, **kwargs):


@receiver(post_setting_create_or_update)
def update_sync_tasks(sender, setting, operator: Optional[str], **kwargs):
def update_sync_tasks(sender, setting, operator: str, **kwargs):
if setting.category.type not in [CategoryType.LDAP.value, CategoryType.MAD.value]:
return

Expand Down
1 change: 1 addition & 0 deletions src/api/bkuser_core/common/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_raw_exception_response(exc, context):
data = {"code": "PERMISSION_DENIED", "detail": exc.extra_info}
return Response(data, status=exc.status_code, headers={})
elif isinstance(exc, ProgrammingError):
logger.exception("occur some programming errors")
data = {"code": "PROGRAMMING_ERROR", "detail": UNKNOWN_ERROR_HINT}
return Response(data, status=HTTP_400_BAD_REQUEST, headers={})

Expand Down
7 changes: 7 additions & 0 deletions src/api/bkuser_core/common/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,10 @@ def to_internal_value(self, value) -> datetime.timedelta:

def to_representation(self, value: datetime.timedelta):
return value.total_seconds()


#########
# Batch #
#########
class BatchRetrieveSerializer(serializers.Serializer):
query_ids = serializers.CharField(help_text="查询 id 列表,以 , 分隔")
4 changes: 2 additions & 2 deletions src/api/bkuser_core/common/viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ class AdvancedBatchOperateViewSet(viewsets.ModelViewSet, DynamicFieldsMixin):
@method_decorator(cache_page(settings.GLOBAL_CACHES_TIMEOUT))
def multiple_retrieve(self, request):
"""批量获取"""
ids = self._get_list_query_param(field_name="query_ids")
instances = self.queryset.filter(id__in=ids)
ids = self._get_list_query_param(field_name="query_ids") or []
instances = self.queryset.filter(pk__in=ids)
return Response(self.serializer_class(instances, many=True).data)

@method_decorator(clear_cache_if_succeed)
Expand Down
10 changes: 7 additions & 3 deletions src/api/bkuser_core/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
from bkuser_core.common.constants import LOOKUP_FIELD_NAME, LOOKUP_PARAM
from bkuser_core.common.error_codes import error_codes
from bkuser_core.common.kits import force_str_2_bool
from bkuser_core.common.serializers import AdvancedListSerializer, AdvancedRetrieveSerialzier, EmptySerializer
from bkuser_core.common.serializers import (
AdvancedListSerializer,
AdvancedRetrieveSerialzier,
BatchRetrieveSerializer,
EmptySerializer,
)
from bkuser_core.common.viewset import AdvancedBatchOperateViewSet, AdvancedListAPIView, AdvancedModelViewSet
from bkuser_core.departments import serializers as department_serializer
from bkuser_core.user_settings.loader import ConfigProvider
Expand Down Expand Up @@ -466,8 +471,7 @@ def get_serializer_class(self):
return self.serializer_class

@swagger_auto_schema(
manual_parameters=[],
responses={"200": local_serializers.ProfileSerializer(many=True)},
query_serializer=BatchRetrieveSerializer(), responses={"200": local_serializers.ProfileSerializer(many=True)}
)
def multiple_retrieve(self, request):
"""批量获取用户"""
Expand Down
2 changes: 1 addition & 1 deletion src/api/bkuser_core/user_settings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"""
import django

post_setting_create_or_update = django.dispatch.Signal(providing_args=["setting"])
post_setting_create_or_update = django.dispatch.Signal(providing_args=["setting", "operator"])
6 changes: 3 additions & 3 deletions src/api/bkuser_core/user_settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def create(self, request, *args, **kwargs):
logger.exception("cannot create setting")
raise error_codes.CANNOT_CREATE_SETTING

post_setting_create_or_update.send(sender=setting, setting=setting)
post_setting_create_or_update.send(sender=setting, setting=setting, operator=request.operator)
return Response(serializers.SettingSerializer(setting).data, status=status.HTTP_201_CREATED)

@swagger_auto_schema(
Expand All @@ -101,7 +101,7 @@ def create(self, request, *args, **kwargs):
)
def update(self, request, *args, **kwargs):
result = super().update(request, *args, **kwargs)
post_setting_create_or_update.send(sender=self, setting=self.get_object())
post_setting_create_or_update.send(sender=self, setting=self.get_object(), operator=request.operator)
return result

@swagger_auto_schema(
Expand All @@ -110,7 +110,7 @@ def update(self, request, *args, **kwargs):
)
def partial_update(self, request, *args, **kwargs):
result = super().partial_update(request, *args, **kwargs)
post_setting_create_or_update.send(sender=self, setting=self.get_object())
post_setting_create_or_update.send(sender=self, setting=self.get_object(), operator=request.operator)
return result


Expand Down

0 comments on commit 831f0bf

Please sign in to comment.