jumpserver/apps/assets/models/node.py

172 lines
5.0 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, transaction
from django.db.models import Q
2018-01-30 11:57:47 +00:00
from django.utils.translation import ugettext_lazy as _
2018-06-01 08:22:52 +00:00
from common.utils import with_cache
2018-01-30 11:57:47 +00:00
__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'
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)
is_node = True
2018-04-10 12:29:06 +00:00
2018-01-30 11:57:47 +00:00
def __str__(self):
2018-04-13 13:26:10 +00:00
return self.full_value
2018-02-09 07:24:44 +00:00
2018-06-01 07:34:08 +00:00
def __eq__(self, other):
return self.key == other.key
def __gt__(self, other):
if self.is_root():
return True
self_key = [int(k) for k in self.key.split(':')]
other_key = [int(k) for k in other.key.split(':')]
if len(self_key) < len(other_key):
return True
elif len(self_key) > len(other_key):
return False
else:
return self_key[-1] < other_key[-1]
2018-02-09 07:24:44 +00:00
@property
def name(self):
return self.value
@property
def full_value(self):
2018-05-31 11:47:57 +00:00
ancestor = [a.value for a in self.get_ancestor(with_self=True)]
if self.is_root():
return self.value
return ' / '.join(ancestor)
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):
2018-05-31 11:47:57 +00:00
with transaction.atomic():
child_key = self.get_next_child_key()
child = self.__class__.objects.create(key=child_key, value=value)
return child
2018-01-30 11:57:47 +00:00
2018-05-31 11:47:57 +00:00
def get_children(self, with_self=False):
pattern = r'^{0}$|^{}:[0-9]+$' if with_self else r'^{}:[0-9]+$'
2018-05-23 07:15:27 +00:00
return self.__class__.objects.filter(
2018-05-31 11:47:57 +00:00
key__regex=pattern.format(self.key)
2018-05-23 07:15:27 +00:00
)
2018-01-30 11:57:47 +00:00
2018-05-31 11:47:57 +00:00
def get_all_children(self, with_self=False):
pattern = r'^{0}$|^{0}:' if with_self else r'^{0}'
2018-05-25 09:26:57 +00:00
return self.__class__.objects.filter(
2018-05-31 11:47:57 +00:00
key__regex=pattern.format(self.key)
2018-05-25 09:26:57 +00:00
)
2018-05-31 11:47:57 +00:00
def get_sibling(self, with_self=False):
key = ':'.join(self.key.split(':')[:-1])
pattern = r'^{}:[0-9]+$'.format(key)
sibling = self.__class__.objects.filter(
key__regex=pattern.format(self.key)
2018-05-25 09:26:57 +00:00
)
2018-05-31 11:47:57 +00:00
if not with_self:
sibling = sibling.exclude(key=self.key)
return sibling
2018-05-25 09:26:57 +00:00
2018-02-07 15:25:15 +00:00
def get_family(self):
2018-05-31 11:47:57 +00:00
ancestor = self.get_ancestor()
2018-05-25 09:26:57 +00:00
children = self.get_all_children()
return [*tuple(ancestor), self, *tuple(children)]
2018-02-07 15:25:15 +00:00
2018-01-30 11:57:47 +00:00
def get_assets(self):
from .asset import Asset
if self.is_root():
assets = Asset.objects.filter(
Q(nodes__id=self.id) | Q(nodes__isnull=True)
)
else:
2018-06-01 07:34:08 +00:00
assets = self.assets.all()
2018-01-30 11:57:47 +00:00
return assets
def get_valid_assets(self):
return self.get_assets().valid()
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:
2018-06-01 07:34:08 +00:00
pattern = r'^{0}$|^{0}:'.format(self.key)
assets = Asset.objects.filter(nodes__key__regex=pattern)
2018-01-30 11:57:47 +00:00
return assets
def get_all_valid_assets(self):
return self.get_all_assets().valid()
2018-02-25 14:36:42 +00:00
def is_root(self):
return self.key == '0'
@property
def parent(self):
2018-05-25 09:26:57 +00:00
if self.key == "0" or 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)
2018-05-25 09:26:57 +00:00
return parent
except Node.DoesNotExist:
return self.__class__.root()
2018-03-01 04:40:41 +00:00
@parent.setter
def parent(self, parent):
if self.is_node:
children = self.get_all_children()
old_key = self.key
with transaction.atomic():
self.key = parent.get_next_child_key()
for child in children:
child.key = child.key.replace(old_key, self.key, 1)
child.save()
self.save()
else:
self.key = parent.key+':fake'
2018-03-01 04:40:41 +00:00
2018-05-31 11:47:57 +00:00
def get_ancestor(self, with_self=False):
2018-05-25 09:26:57 +00:00
if self.is_root():
ancestor = self.__class__.objects.filter(key='0')
2018-05-31 11:47:57 +00:00
return ancestor
_key = self.key.split(':')
if not with_self:
_key.pop()
ancestor_keys = []
for i in range(len(_key)):
ancestor_keys.append(':'.join(_key))
_key.pop()
ancestor = self.__class__.objects.filter(
key__in=ancestor_keys
).order_by('key')
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
)
2018-06-01 08:22:52 +00:00
print(obj)
2018-01-30 11:57:47 +00:00
return obj
2018-05-31 11:47:57 +00:00