Skip to content

Commit b37f7dc

Browse files
Merge pull request #34 from Kalgoc/feat/shared-expenses
Feat - Filter for expenses list implemented
2 parents 8bca1cc + b87d29f commit b37f7dc

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 5.0.6 on 2024-07-02 01:47
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("expenses", "0003_merge_20240701_2317"),
9+
]
10+
11+
operations = [
12+
migrations.AlterField(
13+
model_name="expense",
14+
name="username",
15+
field=models.UUIDField(),
16+
),
17+
]

expenses/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Expense(models.Model):
1010
id = models.AutoField(primary_key=True)
11-
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, to_field="user_id")
11+
username = models.UUIDField()
1212
description = models.CharField(max_length=255, blank=True, null=True)
1313
user_expense_type = models.ForeignKey(UserExpenseType, on_delete=models.CASCADE)
1414
category = models.ForeignKey(Category, on_delete=models.CASCADE)

expenses/serializers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
class ExpenseSerializer(serializers.Serializer):
1010
id = serializers.IntegerField(read_only=True)
11-
username = serializers.PrimaryKeyRelatedField(queryset=get_user_model().objects.all())
11+
username = serializers.UUIDField()
1212
user_expense_type = serializers.PrimaryKeyRelatedField(queryset=UserExpenseType.objects.all(), required=False)
1313
category = serializers.PrimaryKeyRelatedField(queryset=Category.objects.all())
1414
bankcard_id = serializers.PrimaryKeyRelatedField(queryset=BankCard.objects.all())
1515
amount = serializers.IntegerField()
1616
description = serializers.CharField(max_length=255, allow_blank=True, allow_null=True)
17+
created_at = serializers.DateTimeField(read_only=True)
1718

1819
def validate(self, data):
1920
user_expense_type = data.get("user_expense_type")

expenses/views.py

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def list(self, request):
3737
try:
3838
username = get_user_id_from_token(request)
3939
expenses = Expense.objects.filter(username=username)
40+
41+
start_date = request.query_params.get("start_date")
42+
end_date = request.query_params.get("end_date")
43+
44+
if start_date and end_date:
45+
expenses = expenses.filter(created_at__range=[start_date, end_date])
46+
elif start_date:
47+
expenses = expenses.filter(created_at__gte=start_date)
48+
elif end_date:
49+
expenses = expenses.filter(created_at__lte=end_date)
4050
serializer = ExpenseSerializer(expenses, many=True)
4151
return Response(serializer.data)
4252
except Expense.DoesNotExist:

0 commit comments

Comments
 (0)