Skip to content

Commit

Permalink
feat(backend): 全局搜索支持短码 #7961
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 authored and iSecloud committed Nov 15, 2024
1 parent c9801fb commit c1c0710
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 9 additions & 2 deletions dbm-ui/backend/db_services/quick_search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ class QuickSearchSerializer(serializers.Serializer):
allow_empty=True,
)
filter_type = serializers.ChoiceField(help_text=_("检索类型"), choices=FilterType.get_choices(), required=False)
keyword = serializers.CharField(help_text=_("关键字过滤"), required=True)
limit = serializers.IntegerField(help_text=_("分页大小"), required=False, default=10, min_value=1, max_value=1000)
keyword = serializers.CharField(help_text=_("关键字过滤"), required=False)
short_code = serializers.CharField(help_text=_("短码"), required=False)
limit = serializers.IntegerField(help_text=_("分页大小"), required=False, default=10)

def validate(self, attrs):
# keyword 和 short_code 不能同时为空
if not attrs.get("keyword") and not attrs.get("short_code"):
raise serializers.ValidationError(_("keyword 和 short_code 不能同时为空"))
return attrs
13 changes: 12 additions & 1 deletion dbm-ui/backend/db_services/quick_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
specific language governing permissions and limitations under the License.
"""
import logging
from hashlib import md5

from django.core.cache import cache
from django.utils.translation import ugettext_lazy as _
from rest_framework.decorators import action
from rest_framework.response import Response
Expand All @@ -35,6 +37,15 @@ class QuickSearchViewSet(viewsets.SystemViewSet):
@action(methods=["POST"], detail=False, serializer_class=QuickSearchSerializer)
def search(self, request, *args, **kwargs):
params = self.params_validate(self.get_serializer_class())
keyword = params.pop("keyword")
keyword = params.pop("keyword", "")
short_code = params.pop("short_code", "")
# 优先使用短码
if short_code:
keyword = cache.get(f"shot_code_{short_code}")
else:
short_code = md5(keyword.encode("utf-8")).hexdigest()
cache.set(f"shot_code_{short_code}", keyword, 60 * 60 * 24)

result = QSearchHandler(**params).search(keyword)
result.update({"short_code": short_code, "keyword": keyword})
return Response(result)

0 comments on commit c1c0710

Please sign in to comment.