Programmatically generate seed data
This commit is contained in:
		@@ -1,51 +1,71 @@
 | 
			
		||||
from server.api.models import Transaction, Account, User, Stack
 | 
			
		||||
from django.core.management.base import BaseCommand
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
import itertools
 | 
			
		||||
import random
 | 
			
		||||
random.seed(123)
 | 
			
		||||
 | 
			
		||||
class Command(BaseCommand):
 | 
			
		||||
    help = "Seed database for dev"
 | 
			
		||||
    help = 'Seed database for dev'
 | 
			
		||||
 | 
			
		||||
    def handle(self, *args, **options):
 | 
			
		||||
        self.stdout.write('clearing and seeding database...')
 | 
			
		||||
        self.stdout.write('Clearing and seeding database...')
 | 
			
		||||
 | 
			
		||||
        # create test users
 | 
			
		||||
        User.objects.all().delete()
 | 
			
		||||
        elijah = User(username="elijah",
 | 
			
		||||
                      email="elijah@westwinds.io",
 | 
			
		||||
                      password="toffee15").save()
 | 
			
		||||
        ievgen = User(username="ievgen",
 | 
			
		||||
                      email="ievgen@westwinds.io",
 | 
			
		||||
                      password="toffee15").save()
 | 
			
		||||
        tanner = User(username="tanner",
 | 
			
		||||
                      email="tanner@westwinds.io",
 | 
			
		||||
                      password="toffee15").save()
 | 
			
		||||
        elijah = User.objects.create(username='elijah', email='elijah@westwinds.io', password='toffee15')
 | 
			
		||||
        ievgen = User.objects.create(username='ievgen', email='ievgen@westwinds.io', password='toffee15')
 | 
			
		||||
        tanner = User.objects.create(username='tanner', email='tanner@westwinds.io', password='toffee15')
 | 
			
		||||
 | 
			
		||||
        self.stdout.write(str(User.objects.all()))
 | 
			
		||||
        self.stdout.write('Users Created!')
 | 
			
		||||
        self.stdout.write('{} users created!'.format(User.objects.count()))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        # create an account that all 3 users are attached to
 | 
			
		||||
        Account.objects.all().delete()
 | 
			
		||||
        home = Account(name="home", details="home stacks",
 | 
			
		||||
                       income=4000, expenses=2000).save()
 | 
			
		||||
 | 
			
		||||
        self.stdout.write(str(Account.objects.all()))
 | 
			
		||||
        self.stdout.write('Accounts Created!')
 | 
			
		||||
 | 
			
		||||
        # create test stacks for account
 | 
			
		||||
        Stack.objects.all().delete()
 | 
			
		||||
        groceries = Stack(name="Groceries",
 | 
			
		||||
                          details="",
 | 
			
		||||
                          amount=800).save()
 | 
			
		||||
 | 
			
		||||
        takeout = Stack(name="Date Nights",
 | 
			
		||||
                        details="",
 | 
			
		||||
                        amount=400).save()
 | 
			
		||||
 | 
			
		||||
        shit = Stack(name="Crap",
 | 
			
		||||
                     details="",
 | 
			
		||||
                     amount=500).save()
 | 
			
		||||
 | 
			
		||||
        Transaction.objects.all().delete()
 | 
			
		||||
        # generate transaction history
 | 
			
		||||
        account_count = 1
 | 
			
		||||
 | 
			
		||||
        self.stdout.ending = ''
 | 
			
		||||
        self.stdout.write('Populating data')
 | 
			
		||||
 | 
			
		||||
        for length in range(1,4):
 | 
			
		||||
            for users in itertools.combinations([elijah, ievgen, tanner], length):
 | 
			
		||||
                income = random.randint(2000,4000)
 | 
			
		||||
                expenses = random.randint(1000, income)
 | 
			
		||||
 | 
			
		||||
                account = Account.objects.create(
 | 
			
		||||
                    name='Account #{}'.format(account_count),
 | 
			
		||||
                    details='Belongs to {}'.format(', '.join([u.username for u in users])),
 | 
			
		||||
                    income=income,
 | 
			
		||||
                    expenses=expenses,
 | 
			
		||||
                )
 | 
			
		||||
                account.users.set(users)
 | 
			
		||||
                account.save()
 | 
			
		||||
 | 
			
		||||
                for stack_num in range(1, random.randint(2, 7)):
 | 
			
		||||
                    stack = Stack.objects.create(
 | 
			
		||||
                        account=account,
 | 
			
		||||
                        name='Stack #{}'.format(stack_num),
 | 
			
		||||
                        details='Belongs to account #{}'.format(account_count),
 | 
			
		||||
                        amount=random.randint(100, expenses),
 | 
			
		||||
                    )
 | 
			
		||||
 | 
			
		||||
                    for tx_num in range(1, random.randint(2, 50)):
 | 
			
		||||
                        tx = Transaction.objects.create(
 | 
			
		||||
                            stack=stack,
 | 
			
		||||
                            #name='Transaction #{}'.format(tx_num),
 | 
			
		||||
                            details='Belongs to stack #{}'.format(stack_num),
 | 
			
		||||
                            amount=random.randint(1, 100),
 | 
			
		||||
                        )
 | 
			
		||||
 | 
			
		||||
                        self.stdout.write('.')
 | 
			
		||||
                        sys.stdout.flush()
 | 
			
		||||
 | 
			
		||||
                account_count += 1
 | 
			
		||||
 | 
			
		||||
        self.stdout.ending = '\n'
 | 
			
		||||
        self.stdout.write('')
 | 
			
		||||
        self.stdout.write('{} accounts created!'.format(Account.objects.count()))
 | 
			
		||||
 | 
			
		||||
        self.stdout.write('done!')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user