def get(self, request, pk, fy=0, format=None):
     fy = int(fy) or SubmissionAttributes.latest_available_fy()
     queryset = AppropriationAccountBalances.objects.filter(
         submission__is_final_balances_for_fy=True,
         treasury_account_identifier__federal_account_id=int(pk)).filter(
             submission__reporting_fiscal_year=fy)
     queryset = queryset.aggregate(
         outlay=Sum("gross_outlay_amount_by_tas_cpe"),
         budget_authority=Sum("total_budgetary_resources_amount_cpe"),
         obligated=Sum("obligations_incurred_total_by_tas_cpe"),
         unobligated=Sum("unobligated_balance_cpe"),
         balance_brought_forward=Sum(
             F("budget_authority_unobligated_balance_brought_forward_fyb") +
             F("adjustments_to_unobligated_balance_brought_forward_cpe")),
         other_budgetary_resources=Sum(
             "other_budgetary_resources_amount_cpe"),
         appropriations=Sum("budget_authority_appropriated_amount_cpe"),
     )
     if queryset["outlay"] is not None:
         name = FederalAccount.objects.filter(
             id=int(pk)).values("account_title").first()["account_title"]
         queryset["name"] = name
         return Response({"results": queryset})
     else:
         return Response({})
    def _parse_and_validate_request(self, request_dict):
        """ Validate the Request object includes the required fields """
        fy_range = [
            str(i) for i in range(2001,
                                  FiscalDateTime.today().year + 1)
        ]
        last_fy = str(SubmissionAttributes.latest_available_fy()) or str(
            FiscalDateTime.today().year)
        request_settings = [
            {
                "key": "sort",
                "name": "sort",
                "type": "object",
                "optional": True,
                "object_keys": {
                    "field": {
                        "type":
                        "enum",
                        "enum_values": [
                            "budgetary_resources", "managing_agency",
                            "account_name", "account_number"
                        ],
                        "optional":
                        True,
                        "default":
                        "budgetary_resources",
                    },
                    "direction": {
                        "type": "enum",
                        "enum_values": ["asc", "desc"],
                        "optional": True,
                        "default": "asc"
                    },
                },
                "default": {
                    "field": "budgetary_resources",
                    "direction": "asc"
                },
            },
            {
                "key": "page",
                "name": "page",
                "type": "integer",
                "default": 1,
                "min": 1,
                "optional": True
            },
            {
                "key": "limit",
                "name": "limit",
                "type": "integer",
                "default": 10,
                "min": 1,
                "max": 100,
                "optional": True
            },
            {
                "key": "filters",
                "name": "filters",
                "type": "object",
                "optional": True,
                "object_keys": {
                    "agency_identifier": {
                        "name": "agency_identifier",
                        "type": "text",
                        "text_type": "search",
                        "optional": True,
                    },
                    "fy": {
                        "type": "enum",
                        "enum_values": fy_range,
                        "optional": True,
                        "default": last_fy
                    },
                },
                "default": {
                    "fy": last_fy
                },
            },
            {
                "key": "keyword",
                "name": "keyword",
                "type": "text",
                "text_type": "search",
                "optional": True
            },
        ]

        validated_request_data = TinyShield(request_settings).block(
            request_dict)
        validated_request_data["filters"]["fy"] = validated_request_data[
            "filters"].get("fy", last_fy)
        return validated_request_data