<commit_before># -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from django.db import transaction
from faker import Faker

from wye.organisations.models import Organisation, Location, State


NUMBER_OF_LOCATIONS = getattr(settings, "NUMBER_OF_LOCATIONS", 10)
NUMBER_OF_ORGANISATIONS = getattr(settings, "NUMBER_OF_ORGANISATIONS", 10)
NUMBER_OF_USERS = getattr(settings, "NUMBER_OF_USERS", 10)


class Command(BaseCommand):
    fake = Faker()

    @transaction.atomic
    def handle(self, *args, **options):
        self.fake.seed(4321)

        print('  Updating domain to localhost:8000')  # Update site url
        site = Site.objects.get_current()
        site.domain, site.name = 'localhost:8000', 'Local'
        site.save()

        print('  Creating Superuser')
        self.create_user(is_superuser=True, username='admin', email='<EMAIL>',
                         is_active=True, is_staff=True, first_name='Admin')

        print('  Creating sample users')
        for i in range(NUMBER_OF_USERS):
            self.create_user()

        print('  Creating sample locations')
        self.create_locations(counter=NUMBER_OF_LOCATIONS)

        print('  Creating sample organisations')
        self.create_organisations(counter=NUMBER_OF_ORGANISATIONS)

    def create_user(self, counter=None, **kwargs):
        params = {
            "first_name": kwargs.get('first_name', self.fake.first_name()),
            "last_name": kwargs.get('last_name', self.fake.last_name()),
            "username": kwargs.get('username', self.fake.user_name()),
            "email": kwargs.get('email', self.fake.email()),
            "is_active": kwargs.get('is_active', self.fake.boolean()),
            "is_superuser": kwargs.get('is_superuser', False),
            "is_staff": kwargs.get('is_staff', kwargs.get('is_superuser', self.fake.boolean())),
        }

        user, created = get_user_model().objects.get_or_create(**params)

        if params['is_superuser']:
            password = '<PASSWORD>'
            user.set_password(password)
            user.save()

            print("SuperUser created with username: {username} and password: {password}".format(
                username=params['username'], password=password)
            )

        return user

    def create_locations(self, counter=None):
        for i in range(counter):
            state, updated = State.objects.update_or_create(name=self.fake.state())
            Location.objects.update_or_create(name=self.fake.city(), state=state)

    def create_organisations(self, counter=None):
        users = get_user_model().objects.all()
        locations = Location.objects.all()

        for i in range(counter):
            number = self.fake.random_digit()
            text = self.fake.text()
            name = self.fake.company()
            org, updated = Organisation.objects.update_or_create(
                name=name,
                location=locations[number],
                organisation_type=number,
                organisation_role=text,
                description=text,
            )
            org.user.add(users[number])<commit_msg>Create sample data for workshop sections<commit_after># -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from django.db import transaction
from faker import Faker

from wye.organisations.models import Location, Organisation, State
from wye.workshops.models import WorkshopSections


NUMBER_OF_USERS = getattr(settings, "NUMBER_OF_USERS", 10)
NUMBER_OF_LOCATIONS = getattr(settings, "NUMBER_OF_LOCATIONS", 10)
NUMBER_OF_ORGANISATIONS = getattr(settings, "NUMBER_OF_ORGANISATIONS", 10)
NUMBER_OF_WORKSHOP_SECTIONS = getattr(settings, "NUMBER_OF_WORKSHOP_SECTIONS", 5)


class Command(BaseCommand):
    fake = Faker()

    @transaction.atomic
    def handle(self, *args, **options):
        self.fake.seed(4321)

        print('  Updating domain to localhost:8000')  # Update site url
        site = Site.objects.get_current()
        site.domain, site.name = 'localhost:8000', 'Local'
        site.save()

        print('  Creating Superuser')
        self.create_user(is_superuser=True, username='admin', email='<EMAIL>',
                         is_active=True, is_staff=True, first_name='Admin')

        print('  Creating sample users')
        for i in range(NUMBER_OF_USERS):
            self.create_user()

        print('  Creating sample locations')
        self.create_locations(counter=NUMBER_OF_LOCATIONS)

        print('  Creating sample organisations')
        self.create_organisations(counter=NUMBER_OF_ORGANISATIONS)

        print('  Creating sample workshop sections')
        self.create_workshop_sections(counter=NUMBER_OF_WORKSHOP_SECTIONS)

    def create_user(self, counter=None, **kwargs):
        params = {
            "first_name": kwargs.get('first_name', self.fake.first_name()),
            "last_name": kwargs.get('last_name', self.fake.last_name()),
            "username": kwargs.get('username', self.fake.user_name()),
            "email": kwargs.get('email', self.fake.email()),
            "is_active": kwargs.get('is_active', self.fake.boolean()),
            "is_superuser": kwargs.get('is_superuser', False),
            "is_staff": kwargs.get('is_staff', kwargs.get('is_superuser', self.fake.boolean())),
        }

        user, created = get_user_model().objects.get_or_create(**params)

        if params['is_superuser']:
            password = '<PASSWORD>'
            user.set_password(password)
            user.save()

            print("SuperUser created with username: {username} and password: {password}".format(
                username=params['username'], password=password)
            )

        return user

    def create_locations(self, counter=None):
        for i in range(counter):
            state, updated = State.objects.update_or_create(name=self.fake.state())
            Location.objects.update_or_create(name=self.fake.city(), state=state)

    def create_organisations(self, counter=None):
        users = get_user_model().objects.all()
        locations = Location.objects.all()

        for i in range(counter):
            number = self.fake.random_digit()
            text = self.fake.text()
            name = self.fake.company()
            org, updated = Organisation.objects.update_or_create(
                name=name,
                location=locations[number],
                organisation_type=number,
                organisation_role=text,
                description=text,
            )
            org.user.add(users[number])

    def create_workshop_sections(self, counter=None):
        for i in range(counter):
            WorkshopSections.objects.update_or_create(name=self.fake.sentence())
{'fraction_non_alphanumeric': 0.11598343093843737, 'fraction_numerical': 0.005570632766747607, 'mean_word_length': 2.8075040783034257, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 7, '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': '5516224', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1939, 'n_words': 394}