models.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """
  2. Transaction Modles
  3. """
  4. from django.core.exceptions import ValidationError
  5. from django.db import models
  6. from django.utils.translation import ugettext_lazy as _
  7. from django.contrib.auth import get_user_model
  8. User = get_user_model()
  9. def validate_type(value):
  10. """Validate type value"""
  11. if value not in ('inflow', 'outflow'):
  12. raise ValidationError(
  13. _('%(value)s is not an even number'),
  14. params={'value': value},
  15. )
  16. class Transaction(models.Model):
  17. """
  18. Transaction core model
  19. """
  20. user = models.ForeignKey(User,related_name='transactions', on_delete=models.CASCADE)
  21. reference = models.CharField(_('Transaction reference'), max_length=8, unique=True,
  22. help_text=_('Unique value'))
  23. account = models.CharField(_('Account'), max_length=8, help_text=_('Account Number'))
  24. type = models.CharField(_('Type'), max_length=8,
  25. choices=(('inflow', 'inflow'), ('outflow', 'outflow')),
  26. validators=[validate_type],
  27. help_text=_('Choice "outflow" or "inflow"'))
  28. amount = models.FloatField(_('Amount'), max_length=255,
  29. help_text=_('''Only negative numbers for "outflow" and
  30. positive numbers for "inflow"'''))
  31. category = models.CharField(_('Category'), max_length=255, help_text=_('Category Name'))
  32. date = models.DateField('Date',help_text=_('Transaction date. YYYY-MM-DD format.'))