# Path: accounts/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     username = factory.Sequence(lambda n: 'admin%d' % n)
#     password = 'secret'
#     email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.username)
# 
#     class Meta:
#         model = User
# 
#     @classmethod
#     def _create(cls, model_class, *args, **kwargs):
#         manager = cls._get_manager(model_class)
#         return manager.create_user(*args, **kwargs)
# 
# Path: books/factories.py
# class DebtLoanFactory(factory.DjangoModelFactory):
#     with_who = "ACME co."
#     title = factory.Sequence(lambda n: 'debt_loan_%d' % n)
#     amount = 42
#     category = models.DebtLoan.DEBT
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.DebtLoan
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     title = factory.Sequence(lambda n: 'transaction_%d' % n)
#     amount = 10
#     category = models.Transaction.EXPENSE
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.Transaction
# 
# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()


from datetime import datetime
from datetime import timedelta
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.utils import timezone
from accounts.factories import UserFactory
from books.factories import DebtLoanFactory
from books.factories import TransactionFactory
from books.models import DebtLoan
from books.models import Transaction
import random
import factory
import pytz






class Command(BaseCommand):
    help = "Popoulates databse with dummy data"

    def handle(self, *args, **options):
        if not User.objects.filter(username='admin'):
            self.create_admin()
        else:
            self.admin = User.objects.get(username='admin')
            print("admin user already exists")

        self.create_transactions()
        self.create_debt_loans()

    def create_admin(self):
        # Factory creates simple user, so ``is_staff`` is set later

# Path: accounts/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     username = factory.Sequence(lambda n: 'admin%d' % n)
#     password = 'secret'
#     email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.username)
# 
#     class Meta:
#         model = User
# 
#     @classmethod
#     def _create(cls, model_class, *args, **kwargs):
#         manager = cls._get_manager(model_class)
#         return manager.create_user(*args, **kwargs)
# 
# Path: books/factories.py
# class DebtLoanFactory(factory.DjangoModelFactory):
#     with_who = "ACME co."
#     title = factory.Sequence(lambda n: 'debt_loan_%d' % n)
#     amount = 42
#     category = models.DebtLoan.DEBT
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.DebtLoan
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     title = factory.Sequence(lambda n: 'transaction_%d' % n)
#     amount = 10
#     category = models.Transaction.EXPENSE
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.Transaction
# 
# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()


# Path: finance/management/commands/populate.py
from datetime import datetime
from datetime import timedelta
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.utils import timezone
from accounts.factories import UserFactory
from books.factories import DebtLoanFactory
from books.factories import TransactionFactory
from books.models import DebtLoan
from books.models import Transaction
import random
import factory
import pytz







class Command(BaseCommand):
    help = "Popoulates databse with dummy data"

    def handle(self, *args, **options):
        if not User.objects.filter(username='admin'):
            self.create_admin()
        else:
            self.admin = User.objects.get(username='admin')
            print("admin user already exists")

        self.create_transactions()
        self.create_debt_loans()

    def create_admin(self):
        # Factory creates simple user, so ``is_staff`` is set later

        self.admin = UserFactory(username='admin', password='asdasd')