def filter(self, request, queryset, view): order_by = get_ordering(request) if order_by not in ('current_cost', '-current_cost'): return queryset year, month = invoice_utils.parse_period(request.query_params) invoices = invoice_models.Invoice.objects.filter( year=year, month=month, customer=OuterRef('pk')) queryset = queryset.annotate( current_cost=Subquery(invoices.values('current_cost')[:1])) return order_with_nulls(queryset, order_by)
def filter(self, request, queryset, view): order_by = request.query_params.get(api_settings.ORDERING_PARAM) if order_by not in ('current_cost', '-current_cost'): return queryset year, month = invoice_utils.parse_period(request.query_params) invoices = invoice_models.Invoice.objects.filter( year=year, month=month, customer=OuterRef('pk')) queryset = queryset.annotate( current_cost=Subquery(invoices.values('current_cost')[:1])) queryset = queryset.order_by(order_by) return queryset
def get(self, request, format=None): if not self.request.user.is_staff and not request.user.is_support: raise exceptions.PermissionDenied() customers = structure_models.Customer.objects.all() customers = structure_filters.AccountingStartDateFilter().filter_queryset(request, customers, self) name = request.query_params.get('name', '') if name: customers = customers.filter(name__icontains=name) year, month = invoice_utils.parse_period(request.query_params) invoices = invoices_models.Invoice.objects.filter(customer__in=customers) invoices = invoices.filter(year=year, month=month) total = sum(invoice.total for invoice in invoices) return response.Response({'total': total}, status=status.HTTP_200_OK)