-
Notifications
You must be signed in to change notification settings - Fork 0
/
organisations.py
63 lines (51 loc) · 1.93 KB
/
organisations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from mimesis.enums import *
from html import escape
from random import randint
import random
import jsonpickle as json
import datetime as dt
# Generates orgs
class OrganisationGenerator:
def __init__(self, provider, types = []):
self.provider = provider
# Use dishes for easy to remember org IDs.
self.business = provider.business
self.crypto = provider.cryptographic
self.text = provider.text
self.datetime = provider.datetime
self.types = types if types else OrganisationGenerator.DEFAULT_TYPES
# the generated org IDs
ids = {}
def keys(self):
return list(self.ids.keys())
# org type classifications - TODO: Support config file
DEFAULT_TYPES = ['university', 'faculty', 'department']
def create_id(self):
return self.crypto.hash(Algorithm.MD5)
def create(self, type = 'university', parent_id = None, complex = True):
id = self.create_id()
# associate the created id with a type, so we can filter IDs on org types if needed
self.ids[id] = type
# create an org with a random name
org = {
"id": id,
"parent_id": parent_id,
"type": type,
"name": escape(self.business.company()),
"start_date": self.datetime.date(start = 1975).strftime("%d-%m-%Y"),
"children": [] }
return org
# takes a root org and builds a random tree-based hierarchy following the order of types
def create_hierarchy(self, root = None, depth = 1, min_children = 1, max_children = 10):
if root is None:
root = self.create()
# set seed to 1, so we get at least 1 child per layer - otherwise we can get very small structures
new_max = randint(min_children, max_children)
# add number of desired children to each level
for x in range(0, new_max):
child = self.create(self.types[depth], root['id'])
root["children"].append(child)
# if we have more levels to go, make recursive call, same minimum
if(depth + 1 < len(self.types)):
self.create_hierarchy(child, depth + 1, min_children, new_max)
return root