Skip to content

Commit 6f34551

Browse files
Merge pull request #43 from Kalgoc/fix/expense-in-order
Fix/expense in order
2 parents c8c87ee + 1ee5efb commit 6f34551

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

AI/AI.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from textwrap import dedent
55
from dotenv import load_dotenv
66
from categories.models import Category
7+
from categories.exceptions import InvalidCategoryError
78

89

910
GPT_MODEL = "gpt-4-turbo"
@@ -77,5 +78,5 @@ def category_matched_with_id(category):
7778
"Inversión",
7879
]
7980
if category not in categories_allowed:
80-
raise ValueError(f"Categoría no permitida: {category}")
81+
raise InvalidCategoryError(category)
8182
return category

AI/tests.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from unittest.mock import patch
44
from AI import classify_text, category_matched_with_id
55
from textwrap import dedent
6+
from categories.exceptions import InvalidCategoryError
67

78

89
class TestGastosClassifier(unittest.TestCase):
@@ -54,7 +55,7 @@ def test_clean_category(self):
5455
self.assertEqual(category_matched_with_id("vivienda."), "Vivienda")
5556

5657
# Prueba de una categoría no permitida
57-
with self.assertRaises(ValueError):
58+
with self.assertRaises(InvalidCategoryError):
5859
category_matched_with_id("Categoría: Viajes.")
5960

6061

categories/exceptions.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class InvalidCategoryError(Exception):
2+
def __init__(self, category):
3+
self.category = category
4+
self.message = (
5+
"El texto proporcionado no proporciona información suficiente para "
6+
"clasificar el gasto en una categoría. Por favor, se un poco mas descriptivo."
7+
)
8+
self.code = 400
9+
super().__init__(self.message)

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ services:
2020
web:
2121
build:
2222
context: .
23-
dockerfile: Dockerfile
23+
dockerfile: Dev.Dockerfile
2424
container_name: web
2525
volumes:
2626
- .:/app
@@ -37,7 +37,7 @@ services:
3737
POSTGRES_PORT: ${DB_PORT}
3838
POSTGRES_HOST: ${DB_HOST}
3939
OPENAI_API_KEY: ${OPENAI_API_KEY}
40-
DJANGO_SETTINGS_MODULE: piggywallet.settings.prod
40+
DJANGO_SETTINGS_MODULE: piggywallet.settings.dev
4141

4242
networks:
4343
app_network:

expenses/services/categorize_expense.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from rest_framework import status
33
from categories.models import Category
44
from AI.AI import get_category_name_from_description
5+
from categories.exceptions import InvalidCategoryError
56

67

78
def categorize_expense_description(data):
@@ -13,5 +14,7 @@ def categorize_expense_description(data):
1314
return data, None
1415
except ValueError as e:
1516
return None, Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
17+
except InvalidCategoryError as e:
18+
return None, Response({"error": str(e)}, status=e.code)
1619
else:
1720
return None, Response({"error": "Description is required"}, status=status.HTTP_400_BAD_REQUEST)

expenses/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_user_id_from_token(self, request):
3636
def list(self, request):
3737
try:
3838
username = get_user_id_from_token(request)
39-
expenses = Expense.objects.filter(username=username)
39+
expenses = Expense.objects.filter(username=username).order_by("-created_at")
4040

4141
start_date = request.query_params.get("start_date")
4242
end_date = request.query_params.get("end_date")

0 commit comments

Comments
 (0)