-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from thorgate/feature/additions
Feature/additions
- Loading branch information
Showing
26 changed files
with
1,102 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import random | ||
|
||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from companies.models import User, Company, Employment | ||
|
||
|
||
class UserFactory(DjangoModelFactory): | ||
class Meta: | ||
model = User | ||
|
||
password = factory.PostGenerationMethodCall('set_password', 'test') | ||
username = factory.Faker('user_name') | ||
email = factory.Faker('email') | ||
first_name = factory.Faker('first_name') | ||
last_name = factory.Faker('last_name') | ||
|
||
|
||
class CompanyFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Company | ||
|
||
name = factory.Faker('company') | ||
email = factory.Faker('email') | ||
reg_code = factory.Faker('numerify', text='%##-####') | ||
|
||
|
||
def create_full_example_data(): | ||
UserFactory.create_batch(100) | ||
CompanyFactory.create_batch(70) | ||
|
||
users = list(User.objects.all()) | ||
companies = list(Company.objects.all()) | ||
|
||
# Generate 300 unique user-company pairs | ||
user_company_pairs = set() | ||
while len(user_company_pairs): | ||
user_company_pairs.add((random.choice(users), random.choice(companies))) | ||
|
||
Employment.objects.bulk_create([ | ||
Employment(user=user, company=company, | ||
role=Employment.ROLE_ADMIN if random.random() < 0.25 else Employment.ROLE_NORMAL) | ||
for user, company in user_company_pairs | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,10 @@ | ||
import random | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from companies.models import User, Company, Employment | ||
|
||
|
||
def faker_with_max_length(provider: str, max_length: int): | ||
return factory.LazyFunction(lambda: factory.Faker(provider).generate({})[:max_length]) | ||
|
||
|
||
def faker_estonian_phone_number(): | ||
""" Creates valid Estonian phone numbers. | ||
Numbers beginning with '+37250', followed by 5 or 6 digits are always valid. | ||
""" | ||
return factory.LazyFunction(lambda: ('+37250%06d' % random.randint(0, 999999))) | ||
|
||
|
||
class UserFactory(DjangoModelFactory): | ||
class Meta: | ||
model = User | ||
|
||
password = factory.PostGenerationMethodCall('set_password', 'test') | ||
username = factory.Faker('user_name') | ||
email = factory.Faker('email') | ||
first_name = factory.Faker('first_name') | ||
last_name = factory.Faker('last_name') | ||
|
||
|
||
class CompanyFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Company | ||
|
||
name = factory.Faker('company') | ||
email = factory.Faker('email') | ||
from companies.factories import create_full_example_data | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Create test data" | ||
|
||
def handle(self, *args, **options): | ||
UserFactory.create_batch(100) | ||
CompanyFactory.create_batch(70) | ||
|
||
users = list(User.objects.all()) | ||
companies = list(Company.objects.all()) | ||
|
||
Employment.objects.bulk_create([ | ||
Employment(user=random.choice(users), company=random.choice(companies), | ||
role=Employment.ROLE_MANAGER if random.random() < 0.25 else Employment.ROLE_NORMAL) | ||
for _ in range(300) | ||
]) | ||
create_full_example_data() |
Oops, something went wrong.