Fixed bug with the parent categories that shows the others users
This commit is contained in:
parent
600e0c78ce
commit
6c51b5a9b2
@ -87,6 +87,14 @@ class CategoryForm(forms.ModelForm):
|
|||||||
model = Category
|
model = Category
|
||||||
fields = ["name", "parent"]
|
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 GoalForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Goal
|
model = Goal
|
||||||
|
|||||||
@ -69,6 +69,27 @@ class Account(models.Model):
|
|||||||
def monthly_balance(self, year=None):
|
def monthly_balance(self, year=None):
|
||||||
year = year or date.today().year
|
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 = (
|
incomes = (
|
||||||
self.incomes.filter(date__year=year)
|
self.incomes.filter(date__year=year)
|
||||||
.annotate(month=ExtractMonth("date"))
|
.annotate(month=ExtractMonth("date"))
|
||||||
@ -86,7 +107,6 @@ class Account(models.Model):
|
|||||||
income_map = {i["month"]: i["total"] for i in incomes}
|
income_map = {i["month"]: i["total"] for i in incomes}
|
||||||
expenses_map = {e["month"]: e["total"] for e in expenses}
|
expenses_map = {e["month"]: e["total"] for e in expenses}
|
||||||
|
|
||||||
balance = self.initial_balance
|
|
||||||
data = []
|
data = []
|
||||||
|
|
||||||
for month in range(1, 13):
|
for month in range(1, 13):
|
||||||
|
|||||||
@ -830,14 +830,14 @@ def category_list(request):
|
|||||||
categories = Category.objects.filter(owner=request.user)
|
categories = Category.objects.filter(owner=request.user)
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = CategoryForm(request.POST)
|
form = CategoryForm(request.POST, user=request.user)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
category = form.save(commit=False)
|
category = form.save(commit=False)
|
||||||
category.owner = request.user
|
category.owner = request.user
|
||||||
category.save()
|
category.save()
|
||||||
return redirect("category_list")
|
return redirect("category_list")
|
||||||
else:
|
else:
|
||||||
form = CategoryForm()
|
form = CategoryForm(user=request.user)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user