backend/app/blockchain/models.py

69 lines
1.8 KiB
Python

from django.core.validators import MinValueValidator
from django.db import models
# Create your models here.
from users.models import User
class Transaction(models.Model):
user_from = models.ForeignKey(
User, related_name="transactions_from", on_delete=models.CASCADE
)
user_to = models.ForeignKey(
User, related_name="transactions_to", on_delete=models.CASCADE
)
amount = models.IntegerField(validators=[MinValueValidator(1)])
created = models.DateTimeField(auto_now_add=True)
hash = models.CharField(max_length=256, unique=True)
@property
def t_type(self):
return False
@property
def user_from_username(self):
return self.user_from.username
@property
def user_to_username(self):
return self.user_to.username
def __str__(self):
return f"transaction from {self.user_from} to {self.user_to}"
class AdminTransaction(models.Model):
class TransactionType(models.TextChoices):
FROM = "PAYMENT", "payment"
TO = "SALARY", "salary"
type = models.CharField(max_length=7, choices=TransactionType.choices)
amount = models.IntegerField(validators=[MinValueValidator(1)])
user = models.ForeignKey(
User, related_name="admin_transactions", on_delete=models.CASCADE
)
created = models.DateTimeField(auto_now_add=True)
hash = models.CharField(max_length=256, unique=True)
@property
def user_from_username(self):
return "system" if self.type == self.TransactionType.TO else self.user.username
@property
def t_type(self):
return self.type.lower()
@property
def user_to_username(self):
return (
"system" if self.type == self.TransactionType.FROM else self.user.username
)
def __str__(self):
return str(self.amount)