-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17d2157
commit 2e729fc
Showing
20 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UserstatsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "userstats" |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.urls import path | ||
from .views import ExpenseSummaryStats, IncomeSourceSummaryStats | ||
|
||
|
||
urlpatterns = [ | ||
path( | ||
"expense_category_data", | ||
ExpenseSummaryStats.as_view(), | ||
name="expense-category-summary", | ||
), | ||
path( | ||
"income_sources_data", | ||
IncomeSourceSummaryStats.as_view(), | ||
name="income-sources-summary", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from rest_framework.views import APIView | ||
import datetime | ||
from expenses.models import Expense | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from income.models import Income | ||
|
||
|
||
class IncomeSourceSummaryStats(APIView): | ||
def get_amount_for_source(self, income_list, source): | ||
income = income_list.filter(source=source) | ||
amount = 0 | ||
|
||
for i in income: | ||
amount += i.amount | ||
|
||
return {"amount": str(amount)} | ||
|
||
def get_source(self, income): | ||
return income.source | ||
|
||
def get(self, request): | ||
todays_date = datetime.date.today() | ||
a_year_ago = todays_date - datetime.timedelta(days=30 * 12) | ||
income = Income.objects.filter( | ||
owner=request.user, date__gte=a_year_ago, date__lte=todays_date | ||
) | ||
|
||
final = {} | ||
sources = list(set(map(self.get_source, income))) | ||
|
||
for i in income: | ||
for source in sources: | ||
final[source] = self.get_amount_for_source(income, source) | ||
|
||
return Response({"income_source_data": final}, status=status.HTTP_200_OK) | ||
|
||
|
||
class ExpenseSummaryStats(APIView): | ||
def get_amount_for_category(self, expense_list, category): | ||
expenses = expense_list.filter(category=category) | ||
amount = 0 | ||
|
||
for expense in expenses: | ||
amount += expense.amount | ||
|
||
return {"amount": str(amount)} | ||
|
||
def get_category(self, expense): | ||
return expense.category | ||
|
||
def get(self, request): | ||
todays_date = datetime.date.today() | ||
a_year_ago = todays_date - datetime.timedelta(days=30 * 12) | ||
expenses = Expense.objects.filter( | ||
owner=request.user, date__gte=a_year_ago, date__lte=todays_date | ||
) | ||
|
||
final = {} | ||
categories = list(set(map(self.get_category, expenses))) | ||
|
||
for expense in expenses: | ||
for category in categories: | ||
final[category] = self.get_amount_for_category(expenses, category) | ||
|
||
return Response({"category_data": final}, status=status.HTTP_200_OK) |