Enable filtering and sorting on all API views

This commit is contained in:
icovada
2026-01-08 21:33:33 +00:00
parent ffa4644e1b
commit 7687ff81c3
6 changed files with 183 additions and 20 deletions

View File

@@ -433,6 +433,10 @@ REST_FRAMEWORK = {
"apps.api.permissions.NotInDemoMode",
"rest_framework.permissions.DjangoModelPermissions",
],
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
'rest_framework.filters.OrderingFilter',
],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",

View File

@@ -16,9 +16,14 @@ class AccountGroupViewSet(viewsets.ModelViewSet):
queryset = AccountGroup.objects.all()
serializer_class = AccountGroupSerializer
pagination_class = CustomPageNumberPagination
filterset_fields = {
'name': ['exact', 'icontains'],
'owner': ['exact'],
}
search_fields = ['name']
ordering_fields = '__all__'
ordering = ['id']
def get_queryset(self):
return AccountGroup.objects.all().order_by("id")
@extend_schema_view(
@@ -34,11 +39,22 @@ class AccountViewSet(viewsets.ModelViewSet):
queryset = Account.objects.all()
serializer_class = AccountSerializer
pagination_class = CustomPageNumberPagination
filterset_fields = {
'name': ['exact', 'icontains'],
'group': ['exact', 'isnull'],
'currency': ['exact'],
'exchange_currency': ['exact', 'isnull'],
'is_asset': ['exact'],
'is_archived': ['exact'],
'owner': ['exact'],
}
search_fields = ['name']
ordering_fields = '__all__'
ordering = ['id']
def get_queryset(self):
return (
Account.objects.all()
.order_by("id")
.select_related("group", "currency", "exchange_currency")
)

View File

@@ -9,8 +9,28 @@ from apps.currencies.models import ExchangeRate
class CurrencyViewSet(viewsets.ModelViewSet):
queryset = Currency.objects.all()
serializer_class = CurrencySerializer
filterset_fields = {
'name': ['exact', 'icontains'],
'code': ['exact', 'icontains'],
'decimal_places': ['exact', 'gte', 'lte', 'gt', 'lt'],
'prefix': ['exact', 'icontains'],
'suffix': ['exact', 'icontains'],
'exchange_currency': ['exact'],
'is_archived': ['exact'],
}
search_fields = '__all__'
ordering_fields = '__all__'
class ExchangeRateViewSet(viewsets.ModelViewSet):
queryset = ExchangeRate.objects.all()
serializer_class = ExchangeRateSerializer
filterset_fields = {
'from_currency': ['exact'],
'to_currency': ['exact'],
'rate': ['exact', 'gte', 'lte', 'gt', 'lt'],
'date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'automatic': ['exact'],
}
search_fields = '__all__'
ordering_fields = '__all__'

View File

@@ -8,6 +8,16 @@ from apps.api.serializers import DCAStrategySerializer, DCAEntrySerializer
class DCAStrategyViewSet(viewsets.ModelViewSet):
queryset = DCAStrategy.objects.all()
serializer_class = DCAStrategySerializer
filterset_fields = {
'name': ['exact', 'icontains'],
'target_currency': ['exact'],
'payment_currency': ['exact'],
'notes': ['exact', 'icontains'],
'created_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
'updated_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
}
search_fields = ['name', 'notes']
ordering_fields = '__all__'
@action(detail=True, methods=["get"])
def investment_frequency(self, request, pk=None):
@@ -32,6 +42,20 @@ class DCAStrategyViewSet(viewsets.ModelViewSet):
class DCAEntryViewSet(viewsets.ModelViewSet):
queryset = DCAEntry.objects.all()
serializer_class = DCAEntrySerializer
filterset_fields = {
'strategy': ['exact'],
'date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'amount_paid': ['exact', 'gte', 'lte', 'gt', 'lt'],
'amount_received': ['exact', 'gte', 'lte', 'gt', 'lt'],
'expense_transaction': ['exact', 'isnull'],
'income_transaction': ['exact', 'isnull'],
'notes': ['exact', 'icontains'],
'created_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
'updated_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
}
search_fields = ['notes']
ordering_fields = '__all__'
ordering = ['-date']
def get_queryset(self):
queryset = DCAEntry.objects.all()

View File

@@ -28,6 +28,14 @@ class ImportProfileViewSet(viewsets.ReadOnlyModelViewSet):
queryset = ImportProfile.objects.all()
serializer_class = ImportProfileSerializer
permission_classes = [IsAuthenticated]
filterset_fields = {
'name': ['exact', 'icontains'],
'yaml_config': ['exact', 'icontains'],
'version': ['exact'],
}
search_fields = ['name', 'yaml_config']
ordering_fields = '__all__'
ordering = ['name']
@extend_schema_view(
@@ -55,6 +63,22 @@ class ImportRunViewSet(viewsets.ReadOnlyModelViewSet):
queryset = ImportRun.objects.all().order_by("-id")
serializer_class = ImportRunSerializer
permission_classes = [IsAuthenticated]
filterset_fields = {
'status': ['exact'],
'profile': ['exact'],
'file_name': ['exact', 'icontains'],
'logs': ['exact', 'icontains'],
'processed_rows': ['exact', 'gte', 'lte', 'gt', 'lt'],
'total_rows': ['exact', 'gte', 'lte', 'gt', 'lt'],
'successful_rows': ['exact', 'gte', 'lte', 'gt', 'lt'],
'skipped_rows': ['exact', 'gte', 'lte', 'gt', 'lt'],
'failed_rows': ['exact', 'gte', 'lte', 'gt', 'lt'],
'started_at': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'finished_at': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
}
search_fields = ['file_name', 'logs']
ordering_fields = '__all__'
ordering = ['-id']
def get_queryset(self):
queryset = super().get_queryset()

View File

@@ -26,6 +26,31 @@ class TransactionViewSet(viewsets.ModelViewSet):
queryset = Transaction.objects.all()
serializer_class = TransactionSerializer
pagination_class = CustomPageNumberPagination
filterset_fields = {
'account': ['exact'],
'type': ['exact'],
'is_paid': ['exact'],
'date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'reference_date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'mute': ['exact'],
'amount': ['exact', 'gte', 'lte', 'gt', 'lt'],
'description': ['exact', 'icontains'],
'notes': ['exact', 'icontains'],
'category': ['exact', 'isnull'],
'installment_plan': ['exact', 'isnull'],
'installment_id': ['exact', 'gte', 'lte'],
'recurring_transaction': ['exact', 'isnull'],
'internal_note': ['exact', 'icontains'],
'internal_id': ['exact'],
'deleted': ['exact'],
'created_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
'updated_at': ['exact', 'gte', 'lte', 'gt', 'lt'],
'deleted_at': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'owner': ['exact'],
}
search_fields = ['description', 'notes', 'internal_note']
ordering_fields = '__all__'
ordering = ['-id']
def perform_create(self, serializer):
instance = serializer.save()
@@ -40,50 +65,100 @@ class TransactionViewSet(viewsets.ModelViewSet):
kwargs["partial"] = True
return self.update(request, *args, **kwargs)
def get_queryset(self):
return Transaction.objects.all().order_by("-id")
class TransactionCategoryViewSet(viewsets.ModelViewSet):
queryset = TransactionCategory.objects.all()
serializer_class = TransactionCategorySerializer
pagination_class = CustomPageNumberPagination
def get_queryset(self):
return TransactionCategory.objects.all().order_by("id")
filterset_fields = {
'name': ['exact', 'icontains'],
'mute': ['exact'],
'active': ['exact'],
'owner': ['exact'],
}
search_fields = ['name']
ordering_fields = '__all__'
ordering = ['id']
class TransactionTagViewSet(viewsets.ModelViewSet):
queryset = TransactionTag.objects.all()
serializer_class = TransactionTagSerializer
pagination_class = CustomPageNumberPagination
def get_queryset(self):
return TransactionTag.objects.all().order_by("id")
filterset_fields = {
'name': ['exact', 'icontains'],
'active': ['exact'],
'owner': ['exact'],
}
search_fields = ['name']
ordering_fields = '__all__'
ordering = ['id']
class TransactionEntityViewSet(viewsets.ModelViewSet):
queryset = TransactionEntity.objects.all()
serializer_class = TransactionEntitySerializer
pagination_class = CustomPageNumberPagination
def get_queryset(self):
return TransactionEntity.objects.all().order_by("id")
filterset_fields = {
'name': ['exact', 'icontains'],
'active': ['exact'],
'owner': ['exact'],
}
search_fields = ['name']
ordering_fields = '__all__'
ordering = ['id']
class InstallmentPlanViewSet(viewsets.ModelViewSet):
queryset = InstallmentPlan.objects.all()
serializer_class = InstallmentPlanSerializer
pagination_class = CustomPageNumberPagination
def get_queryset(self):
return InstallmentPlan.objects.all().order_by("-id")
filterset_fields = {
'account': ['exact'],
'type': ['exact'],
'description': ['exact', 'icontains'],
'number_of_installments': ['exact', 'gte', 'lte', 'gt', 'lt'],
'installment_start': ['exact', 'gte', 'lte', 'gt', 'lt'],
'installment_total_number': ['exact', 'gte', 'lte', 'gt', 'lt'],
'start_date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'reference_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'end_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'recurrence': ['exact'],
'installment_amount': ['exact', 'gte', 'lte', 'gt', 'lt'],
'category': ['exact', 'isnull'],
'notes': ['exact', 'icontains'],
'add_description_to_transaction': ['exact'],
'add_notes_to_transaction': ['exact'],
}
search_fields = ['description', 'notes']
ordering_fields = '__all__'
ordering = ['-id']
class RecurringTransactionViewSet(viewsets.ModelViewSet):
queryset = RecurringTransaction.objects.all()
serializer_class = RecurringTransactionSerializer
pagination_class = CustomPageNumberPagination
filterset_fields = {
'is_paused': ['exact'],
'account': ['exact'],
'type': ['exact'],
'amount': ['exact', 'gte', 'lte', 'gt', 'lt'],
'description': ['exact', 'icontains'],
'category': ['exact', 'isnull'],
'notes': ['exact', 'icontains'],
'reference_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'start_date': ['exact', 'gte', 'lte', 'gt', 'lt'],
'end_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'recurrence_type': ['exact'],
'recurrence_interval': ['exact', 'gte', 'lte', 'gt', 'lt'],
'keep_at_most': ['exact', 'gte', 'lte', 'gt', 'lt'],
'last_generated_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'last_generated_reference_date': ['exact', 'gte', 'lte', 'gt', 'lt', 'isnull'],
'add_description_to_transaction': ['exact'],
'add_notes_to_transaction': ['exact'],
}
search_fields = ['description', 'notes']
ordering_fields = '__all__'
ordering = ['-id']
def get_queryset(self):
return RecurringTransaction.objects.all().order_by("-id")