<commit_before>#! /usr/bin/env python2
import sys
import string
import random

from bson import DBRef
from pymongo import MongoClient

from userskonker.migrate_user_pwd import get_hashed_password
from userskonker.migrate_user_roles import update_user_roles, update_to_version_0_1

def db_connect(host='localhost', port=27017):
    try:
        client = MongoClient("mongodb://%s:%d" % (host, port))
    except Exception as e:
        print(e)
        sys.exit(1)
    return client.registry


def user_find(username):
    db = db_connect()
    try:
        user = db.users.find_one({"_id": username})
    except Exception as e:
        print(e)
        sys.exit(1)
    return user


def domain_generator(size = 8):
    chars = string.ascii_uppercase + string.ascii_lowercase
    allChars = chars + string.digits
    # first character must be a letter
    return ''.join(random.SystemRandom().choice(chars)) + ''.join(random.SystemRandom().choice(allChars) for _ in range(size - 1))


def tenant_find(tenant_name):
    db = db_connect()
    try:
        tenant = db.tenants.find_one({"name": tenant_name})
    except Exception as e:
        print(e)
        sys.exit(1)
    return tenant


def tenant_find_by_domain(domain_name):
    db = db_connect()
    try:
        tenant = db.tenants.find_one({"domainName": domain_name})
    except Exception as e:
        print(e)
        sys.exit(1)
    return tenant


def create_tenant(args, name):
    db = db_connect()
    if args.org:
        org = args.org
    else:
        org = domain_generator()
    tenant = tenant_find_by_domain(org)
    print("Info: Organization name = " + org)
    if tenant is None:
        try:
            inserted_id = db.tenants.insert_one({"name": name, "domainName": org}).inserted_id
            return inserted_id
        except Exception as e:
            print(e)
            sys.exit(1)
    else:
        return tenant['_id']


def create_user(args):
    db = db_connect()
    user = user_find(args.user)
    if user is None:
        username = args.user
        tenant_id = create_tenant(args, username)

        new_user = {
            "_id": username,
            "language": "PT_BR",
            "dateFormat": "DDMMYYYY",
            "zoneId": "AMERICA_SAO_PAULO",
            "password": get_hashed_password(args.password.encode(encoding='UTF-8')).decode(),
            "name": username,
            "phone": "",
            "notificationViaEmail": False,
            "tenant": DBRef("tenants", tenant_id)
        }

        try:<commit_msg>add application when create a tenant<commit_after>#! /usr/bin/env python2
import sys
import string
import random

from bson import DBRef
from pymongo import MongoClient

from userskonker.migrate_user_pwd import get_hashed_password
from userskonker.migrate_user_roles import update_user_roles, update_to_version_0_1

def db_connect(host='localhost', port=27017):
    try:
        client = MongoClient("mongodb://%s:%d" % (host, port))
    except Exception as e:
        print(e)
        sys.exit(1)
    return client.registry


def user_find(username):
    db = db_connect()
    try:
        user = db.users.find_one({"_id": username})
    except Exception as e:
        print(e)
        sys.exit(1)
    return user


def domain_generator(size = 8):
    chars = string.ascii_uppercase + string.ascii_lowercase
    allChars = chars + string.digits
    # first character must be a letter
    return ''.join(random.SystemRandom().choice(chars)) + ''.join(random.SystemRandom().choice(allChars) for _ in range(size - 1))


def tenant_find(tenant_name):
    db = db_connect()
    try:
        tenant = db.tenants.find_one({"name": tenant_name})
    except Exception as e:
        print(e)
        sys.exit(1)
    return tenant


def tenant_find_by_domain(domain_name):
    db = db_connect()
    try:
        tenant = db.tenants.find_one({"domainName": domain_name})
    except Exception as e:
        print(e)
        sys.exit(1)
    return tenant


def create_tenant(args, name):
    db = db_connect()
    if args.org:
        org = args.org
    else:
        org = domain_generator()
    tenant = tenant_find_by_domain(org)
    print("Info: Organization name = " + org)
    if tenant is None:
        try:
            inserted_id = db.tenants.insert_one({"name": name, "domainName": org}).inserted_id
            db.applications.insert_one({
                "_id" : org,
                "friendlyName" : org,
                "description" : "",
                "qualifier" : "brsp01a",
                "registrationDate" : int(datetime.datetime.now().strftime("%s")) * 1000,
                "tenant" : DBRef("tenants", inserted_id)
            })
            return inserted_id
        except Exception as e:
            print(e)
            sys.exit(1)
    else:
        return tenant['_id']


def create_user(args):
    db = db_connect()
    user = user_find(args.user)
    if user is None:
        username = args.user
        tenant_id = create_tenant(args, username)

        new_user = {
            "_id": username,
            "language": "PT_BR",
            "dateFormat": "DDMMYYYY",
            "zoneId": "AMERICA_SAO_PAULO",
            "password": get_hashed_password(args.password.encode(encoding='UTF-8')).decode(),
            "name": username,
            "phone": "",
            "notificationViaEmail": False,
            "tenant": DBRef("tenants", tenant_id)
        }

        try:
{'fraction_non_alphanumeric': 0.1140593329648056, 'fraction_numerical': 0.007002026902524415, 'mean_word_length': 2.3925, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4640063', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1696, 'n_words': 390}