From 2583c0b26c885fe6a903a6137b1d62a8542c8124 Mon Sep 17 00:00:00 2001 From: BaiJiangjie Date: Thu, 10 May 2018 20:48:16 +0800 Subject: [PATCH] =?UTF-8?q?[Bugfix]=20=E8=B5=84=E4=BA=A7=E6=A0=91=E8=8A=82?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=8F=96=E6=B6=88model=20value=20unique?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E5=94=AF=E4=B8=80=E6=80=A7?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E8=AE=A1=E6=95=B0=E5=99=A8=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 29 ++++++++++++++----- apps/assets/models/node.py | 7 +++-- apps/assets/serializers/node.py | 9 ++++++ .../templates/assets/_asset_list_modal.html | 5 +++- .../assets/templates/assets/asset_detail.html | 4 +-- apps/assets/templates/assets/asset_list.html | 14 +++++---- .../templates/users/user_granted_asset.html | 2 +- 7 files changed, 50 insertions(+), 20 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 939444ccc..2ea2d112c 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -45,16 +45,15 @@ class NodeViewSet(BulkModelViewSet): def get_serializer_class(self): show_current_asset = self.request.query_params.get('show_current_asset') - print(show_current_asset) if show_current_asset: return serializers.NodeCurrentSerializer else: return serializers.NodeSerializer - def perform_create(self, serializer): - child_key = Node.root().get_next_child_key() - serializer.validated_data["key"] = child_key - serializer.save() + # def perform_create(self, serializer): + # child_key = Node.root().get_next_child_key() + # serializer.validated_data["key"] = child_key + # serializer.save() class NodeWithAssetsApi(generics.ListAPIView): @@ -91,16 +90,27 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): serializer_class = serializers.NodeSerializer instance = None + def counter(self): + values = [ + child.value[child.value.rfind(' '):] + for child in self.get_object().get_children() + if child.value.startswith("新节点 ") + ] + values = [int(value) for value in values if value.strip().isdigit()] + count = max(values)+1 if values else 1 + return count + def post(self, request, *args, **kwargs): if not request.data.get("value"): - request.data["value"] = _("New node {}").format( - Node.root().get_next_child_key().split(":")[-1] - ) + request.data["value"] = _("New node {}").format(self.counter()) return super().post(request, *args, **kwargs) def create(self, request, *args, **kwargs): instance = self.get_object() value = request.data.get("value") + values = [child.value for child in instance.get_children()] + if value in values: + raise AssertionError('The same level node name cannot be the same') node = instance.create_child(value=value) return Response( {"id": node.id, "key": node.key, "value": node.value}, @@ -199,6 +209,9 @@ class NodeRemoveAssetsApi(generics.UpdateAPIView): instance = self.get_object() if instance != Node.root(): instance.assets.remove(*tuple(assets)) + else: + assets = [asset for asset in assets if asset.nodes.count() > 1] + instance.assets.remove(*tuple(assets)) class NodeReplaceAssetsApi(generics.UpdateAPIView): diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 89e4a6b70..14ac775fc 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -12,7 +12,10 @@ __all__ = ['Node'] class Node(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) key = models.CharField(unique=True, max_length=64, verbose_name=_("Key")) # '1:1:1:1' - value = models.CharField(max_length=128, unique=True, verbose_name=_("Value")) + # value = models.CharField( + # max_length=128, unique=True, verbose_name=_("Value") + # ) + value = models.CharField(max_length=128, verbose_name=_("Value")) child_mark = models.IntegerField(default=0) date_create = models.DateTimeField(auto_now_add=True) @@ -48,7 +51,7 @@ class Node(models.Model): return child def get_children(self): - return self.__class__.objects.filter(key__regex=r'{}:[0-9]+$'.format(self.key)) + return self.__class__.objects.filter(key__regex=r'^{}:[0-9]+$'.format(self.key)) def get_all_children(self): return self.__class__.objects.filter(key__startswith='{}:'.format(self.key)) diff --git a/apps/assets/serializers/node.py b/apps/assets/serializers/node.py index 225d5655a..9f04fd465 100644 --- a/apps/assets/serializers/node.py +++ b/apps/assets/serializers/node.py @@ -51,6 +51,15 @@ class NodeSerializer(serializers.ModelSerializer): fields = ['id', 'key', 'value', 'parent', 'assets_amount', 'is_node'] list_serializer_class = BulkListSerializer + def update(self, instance, validated_data): + value = validated_data.get('value') + if not instance.is_root(): + children = instance.parent.get_children().exclude(key=instance.key) + values = [child.value for child in children] + if value in values: + raise AssertionError('The same level node name cannot be the same') + return super().update(instance, validated_data) + @staticmethod def get_parent(obj): return obj.parent.id diff --git a/apps/assets/templates/assets/_asset_list_modal.html b/apps/assets/templates/assets/_asset_list_modal.html index c5cd48857..db7d165e0 100644 --- a/apps/assets/templates/assets/_asset_list_modal.html +++ b/apps/assets/templates/assets/_asset_list_modal.html @@ -98,7 +98,10 @@ function initTree2() { $.get("{% url 'api-assets:node-list' %}", function(data, status){ $.each(data, function (index, value) { value["pId"] = value["parent"]; - value["open"] = true; + {#value["open"] = true;#} + if (value["key"] === "0") { + value["open"] = true; + } value["name"] = value["value"] + ' (' + value['assets_amount'] + ')'; value['value'] = value['value']; }); diff --git a/apps/assets/templates/assets/asset_detail.html b/apps/assets/templates/assets/asset_detail.html index 0b1b2865a..b07a7c348 100644 --- a/apps/assets/templates/assets/asset_detail.html +++ b/apps/assets/templates/assets/asset_detail.html @@ -190,7 +190,7 @@ @@ -204,7 +204,7 @@ {% for node in asset.nodes.all %} - {{ node.name }} + {{ node }} diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index 98679694f..0100e1e7f 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -211,6 +211,8 @@ function addTreeNode() { }; newNode.checked = zTree.getSelectedNodes()[0].checked; zTree.addNodes(parentNode, 0, newNode); + var node = zTree.getNodeByParam('id', newNode.id, parentNode) + zTree.editName(node); } else { alert("{% trans 'Create node failed' %}") } @@ -241,9 +243,9 @@ function removeTreeNode() { function editTreeNode() { hideRMenu(); - var current_node = zTree.getSelectedNodes()[0]; - if (!current_node){ - return + var current_node = zTree.getSelectedNodes()[0]; + if (!current_node){ + return } if (current_node.value) { current_node.name = current_node.value; @@ -398,9 +400,9 @@ function initTree() { $.get("{% url 'api-assets:node-list' %}", query_params, function(data, status){ $.each(data, function (index, value) { value["pId"] = value["parent"]; - {#if (value["key"] === "0") {#} - value["open"] = true; - {# }#} + if (value["key"] === "0") { + value["open"] = true; + } value["name"] = value["value"] + ' (' + value['assets_amount'] + ')'; value['value'] = value['value']; }); diff --git a/apps/users/templates/users/user_granted_asset.html b/apps/users/templates/users/user_granted_asset.html index ec807aec5..299c33e07 100644 --- a/apps/users/templates/users/user_granted_asset.html +++ b/apps/users/templates/users/user_granted_asset.html @@ -68,7 +68,7 @@ var asset_table; function initTable() { if (inited){ - return + return asset_table } else { inited = true; }