You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/assets/models/node.py

120 lines
3.2 KiB

7 years ago
# -*- coding: utf-8 -*-
#
7 years ago
import uuid
7 years ago
from django.db import models
from django.utils.translation import ugettext_lazy as _
__all__ = ['Node']
class Node(models.Model):
7 years ago
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"))
7 years ago
child_mark = models.IntegerField(default=0)
date_create = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.value
@property
def name(self):
return self.value
@property
def full_value(self):
if self == self.__class__.root():
return self.value
else:
return '{}/{}'.format(self.value, self.parent.full_value)
7 years ago
@property
def level(self):
7 years ago
return len(self.key.split(':'))
7 years ago
7 years ago
def get_next_child_key(self):
7 years ago
mark = self.child_mark
self.child_mark += 1
self.save()
7 years ago
return "{}:{}".format(self.key, mark)
7 years ago
7 years ago
def create_child(self, value):
child_key = self.get_next_child_key()
child = self.__class__.objects.create(key=child_key, value=value)
7 years ago
return child
def get_children(self):
7 years ago
return self.__class__.objects.filter(key__regex=r'{}:[0-9]+$'.format(self.key))
7 years ago
def get_all_children(self):
7 years ago
return self.__class__.objects.filter(key__startswith='{}:'.format(self.key))
7 years ago
def get_family(self):
children = list(self.get_all_children())
children.append(self)
return children
7 years ago
def get_assets(self):
from .asset import Asset
assets = Asset.objects.filter(nodes__id=self.id)
7 years ago
return assets
def get_active_assets(self):
return self.get_assets().filter(is_active=True)
7 years ago
def get_all_assets(self):
from .asset import Asset
if self.is_root():
assets = Asset.objects.all()
else:
nodes = self.get_family()
assets = Asset.objects.filter(nodes__in=nodes)
7 years ago
return assets
def get_all_active_assets(self):
return self.get_all_assets().filter(is_active=True)
def is_root(self):
return self.key == '0'
@property
def parent(self):
if self.key == "0":
return self.__class__.root()
elif not self.key.startswith("0"):
return self.__class__.root()
parent_key = ":".join(self.key.split(":")[:-1])
try:
parent = self.__class__.objects.get(key=parent_key)
except Node.DoesNotExist:
return self.__class__.root()
else:
return parent
@parent.setter
def parent(self, parent):
self.key = parent.get_next_child_key()
@property
def ancestor(self):
if self.parent == self.__class__.root():
return [self.__class__.root()]
else:
return [self.parent, *tuple(self.parent.ancestor)]
@property
def ancestor_with_node(self):
ancestor = self.ancestor
ancestor.insert(0, self)
return ancestor
7 years ago
@classmethod
def root(cls):
7 years ago
obj, created = cls.objects.get_or_create(
7 years ago
key='0', defaults={"key": '0', 'value': "ROOT"}
7 years ago
)
return obj