You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.6 KiB
67 lines
2.6 KiB
from django.core.validators import RegexValidator |
|
from django.db import models |
|
from django.db.models import Q |
|
|
|
from .base import TimestampedModel |
|
|
|
|
|
class Account(TimestampedModel): |
|
class Kind(models.TextChoices): |
|
BANK = "BANK", "Bank" |
|
CREDIT_CARD = "CREDIT_CARD", "Credit card" |
|
|
|
name = models.CharField(max_length=255, unique=True) |
|
kind = models.CharField(max_length=16, choices=Kind.choices) |
|
currency = models.CharField(max_length=3, default="BRL", validators=[ |
|
RegexValidator(r"\A[A-Z]{3}\Z", "Use three uppercase ASCII letters.")]) |
|
archived = models.BooleanField(default=False) |
|
|
|
class Meta: |
|
constraints = [ |
|
models.CheckConstraint(condition=Q(kind__in=["BANK", "CREDIT_CARD"]), name="account_valid_kind"), |
|
models.CheckConstraint(condition=Q(currency__regex=r"^[A-Z]{3}$"), name="account_valid_currency"), |
|
models.CheckConstraint(condition=Q(name__regex=r"\S"), name="account_nonblank_name"), |
|
] |
|
|
|
|
|
class Payee(TimestampedModel): |
|
name = models.CharField(max_length=255, unique=True) |
|
archived = models.BooleanField(default=False) |
|
|
|
class Meta: |
|
constraints = [models.CheckConstraint(condition=Q(name__regex=r"\S"), name="payee_nonblank_name")] |
|
|
|
|
|
class CategoryGroup(TimestampedModel): |
|
name = models.CharField(max_length=255, unique=True) |
|
archived = models.BooleanField(default=False) |
|
|
|
class Meta: |
|
constraints = [models.CheckConstraint(condition=Q(name__regex=r"\S"), name="category_group_nonblank_name")] |
|
|
|
|
|
class Category(TimestampedModel): |
|
class Kind(models.TextChoices): |
|
INCOME = "INCOME", "Income" |
|
EXPENSE = "EXPENSE", "Expense" |
|
|
|
name = models.CharField(max_length=255) |
|
kind = models.CharField(max_length=16, choices=Kind.choices) |
|
group = models.ForeignKey(CategoryGroup, on_delete=models.PROTECT, related_name="categories", null=True, blank=True) |
|
archived = models.BooleanField(default=False) |
|
|
|
class Meta: |
|
constraints = [ |
|
models.CheckConstraint(condition=Q(kind__in=["INCOME", "EXPENSE"]), name="category_valid_kind"), |
|
models.CheckConstraint(condition=Q(name__regex=r"\S"), name="category_nonblank_name"), |
|
models.UniqueConstraint(fields=["group", "name"], name="category_group_name_unique"), |
|
models.UniqueConstraint(fields=["name"], condition=Q(group__isnull=True), name="category_ungrouped_name_unique"), |
|
] |
|
|
|
|
|
class Tag(TimestampedModel): |
|
name = models.CharField(max_length=255, unique=True) |
|
archived = models.BooleanField(default=False) |
|
|
|
class Meta: |
|
constraints = [models.CheckConstraint(condition=Q(name__regex=r"\S"), name="tag_nonblank_name")]
|
|
|