[Feature] tree

pull/1043/head
ibuler 7 years ago
parent 9e09a962af
commit 460fa8e8a9

@ -5,5 +5,6 @@ from .user import AdminUser, SystemUser
from .label import Label
from .cluster import *
from .group import *
from .tree import *
from .asset import *
from .utils import *

@ -36,6 +36,7 @@ class Asset(models.Model):
port = models.IntegerField(default=22, verbose_name=_('Port'))
groups = models.ManyToManyField(AssetGroup, blank=True, related_name='assets', verbose_name=_('Asset groups'))
cluster = models.ForeignKey(Cluster, related_name='assets', default=default_cluster, on_delete=models.SET_DEFAULT, verbose_name=_('Cluster'))
nodes = models.ManyToManyField('assets.Node', blank=True, related_name='assets', verbose_name=_("Nodes"))
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
# Auth

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
#
from django.db import models
from django.utils.translation import ugettext_lazy as _
__all__ = ['Node']
class Node(models.Model):
id = models.CharField(primary_key=True, max_length=64) # '1:1:1:1'
name = models.CharField(max_length=128, verbose_name=_("Name"))
child_mark = models.IntegerField(default=0)
date_create = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
@property
def level(self):
return len(self.id.split(':'))
def get_next_child_id(self):
mark = self.child_mark
self.child_mark += 1
self.save()
return "{}:{}".format(self.id, mark)
def create_child(self, name):
child_id = self.get_next_child_id()
child = self.__class__.objects.create(id=child_id, name=name)
return child
def get_children(self):
return self.__class__.objects.filter(id__regex=r'{}:[0-9]+$'.format(self.id))
def get_all_children(self):
return self.__class__.objects.filter(id__startswith='{}:'.format(self.id))
def get_assets(self):
from .asset import Asset
children = self.get_children()
assets = Asset.objects.filter(nodes__in=children)
return assets
def get_all_assets(self):
from .asset import Asset
children = self.get_all_children()
assets = Asset.objects.filter(nodes__in=children)
return assets
@classmethod
def root(cls):
obj, created = cls.objects.get_or_create(
id='0', defaults={"id": '0', 'name': "ROOT"}
)
return obj
Loading…
Cancel
Save