-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
99 lines (76 loc) · 2.83 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Q
from django.db.models.base import Model
from django.db.models.enums import Choices
# Create your models here.
class User(AbstractUser):
all_money = models.DecimalField(max_digits=10, decimal_places=2, default=0)
class Account(models.Model):
ruser = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
name = models.CharField(max_length=64)
all_money = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return f"{self.name}"
def save(self, *args, **kwargs):
super(Account, self).save(*args, **kwargs)
self.ruser.all_money += self.all_money
self.ruser.save()
def other_save(self, p, *args, **kwargs):
self.ruser.all_money += p
self.ruser.save()
super(Account, self).save(*args, **kwargs)
def delete(self):
self.ruser.all_money -= self.all_money
self.ruser.save()
super(Account, self).delete()
class Transaction(models.Model):
ruser = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
raccount = models.ForeignKey(Account, on_delete=models.CASCADE)
transfer_account = models.ForeignKey(Account, on_delete=models.CASCADE, null=True, blank=True, related_name="transfer")
CASH = 'Cash'
CREDIT = 'Credit'
FAMILY = 'Family'
FOOD = 'Food'
FRIENDS = 'Friends'
TRAVEL = 'Travel'
GROCERIES = 'Groceries'
SHOPPING = 'Shopping'
SAVINGS = 'Savings'
SCHOOL = 'School'
HOUSEHOLD = 'Household'
WORK = 'Work'
OTHER = 'Other'
CHOICES = [
(CASH, 'Cash'),
(CREDIT, 'Credit'),
(FAMILY, 'Family'),
(FOOD, 'Food'),
(FRIENDS, 'Friends'),
(TRAVEL, 'Travel'),
(GROCERIES, 'Groceries'),
(SHOPPING, 'Shopping'),
(SAVINGS, 'Savings'),
(SCHOOL, 'School'),
(HOUSEHOLD, 'Household'),
(WORK, 'Work'),
(OTHER, 'Other')
]
name = models.CharField(max_length=64)
description = models.TextField(max_length=1000, blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
time = models.DateTimeField(auto_now_add=True)
important = models.BooleanField(default=True)
category = models.CharField(max_length=12, choices=CHOICES, default=OTHER)
def __str__(self):
return f"{self.name} : {self.price}"
def save(self,*args, **kwargs):
super(Transaction, self).save(*args, **kwargs)
self.raccount.all_money += self.price
self.raccount.other_save(p=self.price)
def delete(self):
self.raccount.all_money -= self.price
self.raccount.other_save(p= -(self.price))
super(Transaction, self).delete()
# python3 manage.py makemigrations finances
# python3 manage.py migrate