Merge pull request 'Fixed bug with the parent categories that shows the others users' (#9) from dev into main
Reviewed-on: #9
This commit is contained in:
commit
9da089f487
@ -87,6 +87,14 @@ class CategoryForm(forms.ModelForm):
|
||||
model = Category
|
||||
fields = ["name", "parent"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
user = kwargs.pop("user")
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if user:
|
||||
self.fields["parent"].queryset = Category.objects.filter(
|
||||
owner=user,
|
||||
)
|
||||
class GoalForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Goal
|
||||
|
||||
@ -69,6 +69,27 @@ class Account(models.Model):
|
||||
def monthly_balance(self, year=None):
|
||||
year = year or date.today().year
|
||||
|
||||
# -------------------------
|
||||
# Previous accumulated balance
|
||||
# -------------------------
|
||||
previous_income = (
|
||||
self.incomes.filter(date__year__lt=year)
|
||||
.aggregate(total=Sum('amount'))['total']
|
||||
or Decimal('0')
|
||||
)
|
||||
|
||||
previous_expenses = (
|
||||
self.expenses.filter(date__year__lt=year)
|
||||
.aggregate(total=Sum('amount'))['total']
|
||||
or Decimal('0')
|
||||
)
|
||||
|
||||
balance = (self.initial_balance + previous_income - previous_income)
|
||||
|
||||
# -------------------------
|
||||
# Current year data
|
||||
# -------------------------
|
||||
|
||||
incomes = (
|
||||
self.incomes.filter(date__year=year)
|
||||
.annotate(month=ExtractMonth("date"))
|
||||
@ -86,7 +107,6 @@ class Account(models.Model):
|
||||
income_map = {i["month"]: i["total"] for i in incomes}
|
||||
expenses_map = {e["month"]: e["total"] for e in expenses}
|
||||
|
||||
balance = self.initial_balance
|
||||
data = []
|
||||
|
||||
for month in range(1, 13):
|
||||
|
||||
@ -830,14 +830,14 @@ def category_list(request):
|
||||
categories = Category.objects.filter(owner=request.user)
|
||||
|
||||
if request.method == "POST":
|
||||
form = CategoryForm(request.POST)
|
||||
form = CategoryForm(request.POST, user=request.user)
|
||||
if form.is_valid():
|
||||
category = form.save(commit=False)
|
||||
category.owner = request.user
|
||||
category.save()
|
||||
return redirect("category_list")
|
||||
else:
|
||||
form = CategoryForm()
|
||||
form = CategoryForm(user=request.user)
|
||||
|
||||
return render(
|
||||
request,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user