Skip to content

Commit

Permalink
支持用户收藏项目 (#7211)
Browse files Browse the repository at this point in the history
* feature: 支持用户收藏项目

* optimization: 支持用户收藏项目复用用户收藏表
  • Loading branch information
normal-wls authored Dec 6, 2023
1 parent e8c272a commit dec7e2c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 18 deletions.
30 changes: 20 additions & 10 deletions gcloud/contrib/collection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class CollectionManager(models.Manager):
def cascade_delete(self, category, instance_id):
self.filter(category=category, instance_id=instance_id).delete()

def add_user_favorite_project(self, username, project_id):
return self.get_or_create(category="project", username=username, instance_id=project_id)

def remove_user_favorite_project(self, username, project_id):
return self.filter(category="project", username=username, instance_id=project_id).delete()

def get_user_favorite_projects(self, username):
return self.filter(category="project", username=username).values_list("instance_id", flat=True)

def get_user_project_collection_category_ids(self, username, project_id, category):
user_collections = self.filter(category=category, username=username).values()
collection_template_ids = []
Expand Down Expand Up @@ -55,21 +64,22 @@ def get_user_project_collection_instance_info(self, project_id: int, username: s

class Collection(models.Model):
COLLECTION_TYPE = (
("flow", _(u"项目流程")),
("common_flow", _(u"公共流程")),
("periodic_task", _(u"周期任务")),
("mini_app", _(u"轻应用")),
("project", _("项目")),
("flow", _("项目流程")),
("common_flow", _("公共流程")),
("periodic_task", _("周期任务")),
("mini_app", _("轻应用")),
)
username = models.CharField(_(u"用户名"), max_length=255)
category = models.CharField(_(u"收藏对象类型"), max_length=255, choices=COLLECTION_TYPE)
instance_id = models.IntegerField(_(u"收藏对象ID"))
extra_info = models.TextField(_(u"额外信息"), blank=True)
username = models.CharField(_("用户名"), max_length=255)
category = models.CharField(_("收藏对象类型"), max_length=255, choices=COLLECTION_TYPE)
instance_id = models.IntegerField(_("收藏对象ID"))
extra_info = models.TextField(_("额外信息"), blank=True)

objects = CollectionManager()

class Meta:
verbose_name = _(u"用户收藏 Collection")
verbose_name_plural = _(u"用户收藏 Collection")
verbose_name = _("用户收藏 Collection")
verbose_name_plural = _("用户收藏 Collection")
index_together = ["category", "instance_id"]

def __unicode__(self):
Expand Down
5 changes: 5 additions & 0 deletions gcloud/core/apis/drf/serilaziers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ class Meta:
"bk_biz_id",
"relate_business",
]


class ProjectWithFavSerializer(ProjectSerializer):
is_fav = serializers.BooleanField(read_only=True)
is_user_project = serializers.BooleanField(read_only=True)
47 changes: 40 additions & 7 deletions gcloud/core/apis/drf/viewsets/user_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@
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 django.db import IntegrityError
from django.db.models import BooleanField, ExpressionWrapper, Q
from rest_framework import permissions
from rest_framework.decorators import action
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response

from gcloud.iam_auth import IAMMeta, res_factory
from gcloud.iam_auth.utils import get_user_projects

from gcloud.core.models import Project
from gcloud.contrib.collection.models import Collection
from gcloud.core.apis.drf.filtersets import ALL_LOOKUP, AllLookupSupportFilterSet
from gcloud.core.apis.drf.serilaziers import ProjectSerializer
from gcloud.core.apis.drf.resource_helpers import ViewSetResourceHelper
from gcloud.core.apis.drf.serilaziers import ProjectWithFavSerializer
from gcloud.core.models import Project
from gcloud.iam_auth import IAMMeta, res_factory
from gcloud.iam_auth.utils import get_user_projects

from .base import GcloudListViewSet

logger = logging.getLogger("root")


class UserProjectFilter(AllLookupSupportFilterSet):
class Meta:
Expand All @@ -39,7 +47,7 @@ class Meta:

class UserProjectSetViewSet(GcloudListViewSet):
queryset = Project.objects.all().order_by("-id")
serializer_class = ProjectSerializer
serializer_class = ProjectWithFavSerializer
permission_classes = [permissions.IsAuthenticated]
filterset_class = UserProjectFilter
pagination_class = LimitOffsetPagination
Expand All @@ -56,5 +64,30 @@ class UserProjectSetViewSet(GcloudListViewSet):
)

def list(self, request, *args, **kwargs):
self.queryset = get_user_projects(request.user.username)
user_project_ids = list(get_user_projects(request.user.username).values_list("id", flat=True))
user_fav_project_ids = list(Collection.objects.get_user_favorite_projects(request.user.username))
self.list_queryset = (
Project.objects.all()
.annotate(
is_fav=ExpressionWrapper(Q(id__in=user_fav_project_ids), output_field=BooleanField()),
is_user_project=ExpressionWrapper(Q(id__in=user_project_ids), output_field=BooleanField()),
)
.order_by("-is_fav", "-is_user_project", "id")
)
return super(UserProjectSetViewSet, self).list(request, *args, **kwargs)

@action(methods=["post"], detail=True)
def favor(self, request, *args, **kwargs):
project_id = kwargs["pk"]
try:
Collection.objects.add_user_favorite_project(request.user.username, project_id)
except IntegrityError as e:
logger.exception(e)
return Response({"result": False, "data": None, "message": "该用户已收藏该项目"}, status=400)
return Response({"result": True, "data": "success", "message": ""})

@action(methods=["delete"], detail=True)
def cancel_favor(self, request, *args, **kwargs):
project_id = kwargs["pk"]
Collection.objects.remove_user_favorite_project(request.user.username, project_id)
return Response({"result": True, "data": "success", "message": ""})
2 changes: 1 addition & 1 deletion gcloud/taskflow3/apis/django/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def detail(request, project_id):
"ex_data": "节点错误信息(string)"
}
],
"auto_retry_info": {"node_id": "act1", "auto_retry_times": 3, "max_auto_retry_times": 10},
"auto_retry_info": "自动重试信息, node_id auto_retry_times max_auto_retry_times 三个 key (dict)",
"inputs": "节点输入数据, include_data 为 1 时返回(object or null)",
"outputs": "节点输出数据, include_data 为 1 时返回(list)",
"ex_data": "节点错误信息, include_data 为 1 时返回(string)"
Expand Down
41 changes: 41 additions & 0 deletions gcloud/tests/contrib/collection/test_user_favorite_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017 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 django.test import TestCase

from gcloud.contrib.collection.models import Collection


class UserFavoriteProjectTestCase(TestCase):
def setUp(self):
self.username = "user"
self.project_id = 1

def tearDown(self):
Collection.objects.all().delete()

def test_add_user_favorite_project(self):
self.assertEqual(Collection.objects.count(), 0)
Collection.objects.add_user_favorite_project(self.username, self.project_id)
self.assertEqual(Collection.objects.count(), 1)

def test_remove_user_favorite_project(self):
Collection.objects.create(category="project", username=self.username, project_id=self.project_id)
self.assertEqual(Collection.objects.count(), 1)
Collection.objects.remove_user_favorite_project(self.username, self.project_id)
self.assertEqual(Collection.objects.count(), 0)

def test_get_user_favorite_projects(self):
Collection.objects.create(category="project", username=self.username, project_id=1)
Collection.objects.create(category="project", username=self.username, project_id=2)
self.assertEqual(list(Collection.objects.get_user_favorite_projects(self.username)), [1, 2])

0 comments on commit dec7e2c

Please sign in to comment.