Skip to content

Commit

Permalink
Created chart data endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjaychukwu committed Aug 22, 2022
1 parent 17d2157 commit 2e729fc
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 0 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified expense_project/__pycache__/settings.cpython-38.pyc
Binary file not shown.
Binary file modified expense_project/__pycache__/urls.cpython-38.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions expense_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@
"django.contrib.messages",
"django.contrib.staticfiles",
# 3rd party apps
"corsheaders",
"rest_framework",
"drf_yasg",
# Local
"authentication",
"expenses",
"income",
"userstats",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down Expand Up @@ -119,6 +122,7 @@

USE_TZ = True

CORS_ALLOW_ALL_ORIGINS = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
Expand Down
1 change: 1 addition & 0 deletions expense_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
path("auth/", include("authentication.urls")),
path("expenses/", include("expenses.urls")),
path("income/", include("income.urls")),
path("userstats/", include("userstats.urls")),
path(
"",
schema_view.with_ui("swagger", cache_timeout=0),
Expand Down
Empty file added userstats/__init__.py
Empty file.
Binary file added userstats/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added userstats/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added userstats/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file added userstats/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file added userstats/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added userstats/__pycache__/views.cpython-38.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions userstats/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions userstats/apps.py
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.
3 changes: 3 additions & 0 deletions userstats/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions userstats/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
16 changes: 16 additions & 0 deletions userstats/urls.py
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",
),
]
66 changes: 66 additions & 0 deletions userstats/views.py
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)

0 comments on commit 2e729fc

Please sign in to comment.