2020-10-12 04:44:30 +00:00
|
|
|
from random import choice, sample
|
|
|
|
import forgery_py
|
|
|
|
|
|
|
|
from .base import FakeDataGenerator
|
|
|
|
|
|
|
|
from users.models import *
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGenerator(FakeDataGenerator):
|
|
|
|
resource = 'usergroup'
|
|
|
|
|
|
|
|
def do_generate(self, batch, batch_size):
|
|
|
|
groups = []
|
|
|
|
for i in batch:
|
|
|
|
group_name = forgery_py.name.job_title()
|
|
|
|
groups.append(UserGroup(name=group_name, org_id=self.org.id))
|
|
|
|
UserGroup.objects.bulk_create(groups, ignore_conflicts=True)
|
|
|
|
|
|
|
|
|
|
|
|
class UserGenerator(FakeDataGenerator):
|
|
|
|
resource = 'user'
|
|
|
|
roles: list
|
2021-03-08 02:08:51 +00:00
|
|
|
group_ids: list
|
2020-10-12 04:44:30 +00:00
|
|
|
|
|
|
|
def pre_generate(self):
|
2021-03-08 02:08:51 +00:00
|
|
|
self.group_ids = list(UserGroup.objects.all().values_list('id', flat=True))
|
2020-10-12 04:44:30 +00:00
|
|
|
|
|
|
|
def set_org(self, users):
|
|
|
|
relations = []
|
|
|
|
for u in users:
|
|
|
|
relations.append(OrganizationMember(
|
|
|
|
org_id=self.org.id,
|
|
|
|
user_id=u.id,
|
|
|
|
))
|
|
|
|
OrganizationMember.objects.bulk_create(relations, ignore_conflicts=True)
|
|
|
|
|
|
|
|
def set_groups(self, users):
|
|
|
|
relations = []
|
|
|
|
for i in users:
|
2021-03-08 02:08:51 +00:00
|
|
|
groups_to_join = sample(self.group_ids, 3)
|
2020-10-12 04:44:30 +00:00
|
|
|
_relations = [User.groups.through(user_id=i.id, usergroup_id=gid) for gid in groups_to_join]
|
|
|
|
relations.extend(_relations)
|
|
|
|
User.groups.through.objects.bulk_create(relations, ignore_conflicts=True)
|
|
|
|
|
|
|
|
def do_generate(self, batch, batch_size):
|
|
|
|
users = []
|
|
|
|
for i in batch:
|
2020-12-08 06:26:18 +00:00
|
|
|
username = forgery_py.internet.user_name(True) + '-' + str(i)
|
2020-10-12 04:44:30 +00:00
|
|
|
email = forgery_py.internet.email_address()
|
|
|
|
u = User(
|
|
|
|
username=username,
|
|
|
|
email=email,
|
|
|
|
name=username.title(),
|
|
|
|
created_by='Faker'
|
|
|
|
)
|
|
|
|
users.append(u)
|
|
|
|
users = User.objects.bulk_create(users, ignore_conflicts=True)
|
|
|
|
self.set_org(users)
|
|
|
|
self.set_groups(users)
|