# 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
    def _get_last_month(self):
        "Returns random date in last month"
        today = timezone.now()
        first_month_day = today.replace(day=1)
        last_month = first_month_day - timedelta(days=1)
        return datetime(last_month.year,
                        last_month.month,
                        random.randint(1, 28),
                        tzinfo=pytz.utc)

    def _get_this_year(self):
        "Returns random date in this year"
        today = timezone.now()
        return datetime(today.year,
                        random.randint(1, today.month),
                        random.randint(1, today.day),
                        tzinfo=pytz.utc)

    def _get_all_time(self):
        "Returns random date"
        today = timezone.now()
        return datetime(random.randint(2000, today.year-1),
                        random.randint(1, 12),
                        random.randint(1, 28),
                        tzinfo=pytz.utc)

    def create_transactions(self):
        categories = [Transaction.EXPENSE, Transaction.INCOME]

        # create now

# 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

    def _get_last_month(self):
        "Returns random date in last month"
        today = timezone.now()
        first_month_day = today.replace(day=1)
        last_month = first_month_day - timedelta(days=1)
        return datetime(last_month.year,
                        last_month.month,
                        random.randint(1, 28),
                        tzinfo=pytz.utc)

    def _get_this_year(self):
        "Returns random date in this year"
        today = timezone.now()
        return datetime(today.year,
                        random.randint(1, today.month),
                        random.randint(1, today.day),
                        tzinfo=pytz.utc)

    def _get_all_time(self):
        "Returns random date"
        today = timezone.now()
        return datetime(random.randint(2000, today.year-1),
                        random.randint(1, 12),
                        random.randint(1, 28),
                        tzinfo=pytz.utc)

    def create_transactions(self):
        categories = [Transaction.EXPENSE, Transaction.INCOME]

        # create now

        TransactionFactory.create_batch(