[Update] 添加资产授权规则 generate_fake 方法

pull/3257/head
BaiJiangJie 5 years ago
parent 02944d407c
commit fad842a8a0

@ -1,12 +1,13 @@
import uuid import uuid
import logging
from functools import reduce from functools import reduce
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.utils import date_expired_default from orgs.models import Organization
from orgs.mixins.models import OrgModelMixin from orgs.utils import get_current_org
from assets.models import Asset, SystemUser, Node from assets.models import Asset, SystemUser, Node
from .base import BasePermission from .base import BasePermission
@ -15,6 +16,7 @@ from .base import BasePermission
__all__ = [ __all__ = [
'AssetPermission', 'Action', 'AssetPermission', 'Action',
] ]
logger = logging.getLogger(__name__)
class Action: class Action:
@ -106,3 +108,52 @@ class AssetPermission(BasePermission):
args = reduce(lambda x, y: x | y, args) args = reduce(lambda x, y: x | y, args)
assets = Asset.objects.filter(args).distinct() assets = Asset.objects.filter(args).distinct()
return assets return assets
@classmethod
def generate_fake(cls, count=100):
from ..hands import User, Node, SystemUser
import random
org = get_current_org()
if not org or not org.is_real():
Organization.default().change_to()
nodes = list(Node.objects.all())
assets = list(Asset.objects.all())
system_users = list(SystemUser.objects.all())
users = User.objects.filter(username='admin')
for i in range(count):
name = "fake_perm_to_admin_{}".format(str(uuid.uuid4())[:6])
perm = cls(name=name)
try:
perm.save()
perm.users.set(users)
if system_users and len(system_users) > 3:
_system_users = random.sample(system_users, 3)
elif system_users:
_system_users = [system_users[0]]
else:
_system_users = []
perm.system_users.set(_system_users)
if nodes and len(nodes) > 3:
_nodes = random.sample(nodes, 3)
else:
_nodes = [Node.default_node()]
perm.nodes.set(_nodes)
if assets and len(assets) > 3:
_assets = random.sample(assets, 3)
elif assets:
_assets = [assets[0]]
else:
_assets = []
perm.assets.set(_assets)
logger.debug('Generate fake perm: %s' % perm.name)
except Exception as e:
print('Error continue')
continue

Loading…
Cancel
Save