jumpserver/apps/assets/models/node.py

120 lines
3.2 KiB
Python
Raw Normal View History

2018-01-30 11:57:47 +00:00
# -*- coding: utf-8 -*-
#
2018-02-01 09:14:15 +00:00
import uuid
2018-01-30 11:57:47 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
__all__ = ['Node']
class Node(models.Model):
2018-02-01 09:14:15 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
key = models.CharField(unique=True, max_length=64, verbose_name=_("Key")) # '1:1:1:1'
2018-03-07 04:13:03 +00:00
value = models.CharField(max_length=128, verbose_name=_("Value"))
2018-01-30 11:57:47 +00:00
child_mark = models.IntegerField(default=0)
date_create = models.DateTimeField(auto_now_add=True)
def __str__(self):
2018-02-09 07:24:44 +00:00
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)
2018-01-30 11:57:47 +00:00
@property
def level(self):
2018-02-01 09:14:15 +00:00
return len(self.key.split(':'))
2018-01-30 11:57:47 +00:00
2018-02-01 09:14:15 +00:00
def get_next_child_key(self):
2018-01-30 11:57:47 +00:00
mark = self.child_mark
self.child_mark += 1
self.save()
2018-02-01 09:14:15 +00:00
return "{}:{}".format(self.key, mark)
2018-01-30 11:57:47 +00:00
2018-02-01 09:14:15 +00:00
def create_child(self, value):
child_key = self.get_next_child_key()
child = self.__class__.objects.create(key=child_key, value=value)
2018-01-30 11:57:47 +00:00
return child
def get_children(self):
2018-02-01 09:14:15 +00:00
return self.__class__.objects.filter(key__regex=r'{}:[0-9]+$'.format(self.key))
2018-01-30 11:57:47 +00:00
def get_all_children(self):
2018-02-01 09:14:15 +00:00
return self.__class__.objects.filter(key__startswith='{}:'.format(self.key))
2018-01-30 11:57:47 +00:00
2018-02-07 15:25:15 +00:00
def get_family(self):
children = list(self.get_all_children())
children.append(self)
return children
2018-01-30 11:57:47 +00:00
def get_assets(self):
from .asset import Asset
2018-02-07 15:25:15 +00:00
assets = Asset.objects.filter(nodes__id=self.id)
2018-01-30 11:57:47 +00:00
return assets
def get_active_assets(self):
return self.get_assets().filter(is_active=True)
2018-01-30 11:57:47 +00:00
def get_all_assets(self):
from .asset import Asset
2018-02-25 14:36:42 +00:00
if self.is_root():
assets = Asset.objects.all()
else:
nodes = self.get_family()
assets = Asset.objects.filter(nodes__in=nodes)
2018-01-30 11:57:47 +00:00
return assets
def get_all_active_assets(self):
return self.get_all_assets().filter(is_active=True)
2018-02-25 14:36:42 +00:00
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
2018-03-01 04:40:41 +00:00
@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
2018-01-30 11:57:47 +00:00
@classmethod
2018-02-01 07:06:44 +00:00
def root(cls):
2018-01-30 11:57:47 +00:00
obj, created = cls.objects.get_or_create(
2018-02-01 09:14:15 +00:00
key='0', defaults={"key": '0', 'value': "ROOT"}
2018-01-30 11:57:47 +00:00
)
return obj