def validate_disaster_recipient_request(request_data): _validate_required_parameters(request_data, ["filters"]) model = [ { "key": "filters|def_codes", "name": "def_codes", "type": "array", "array_type": "enum", "enum_values": sorted( DisasterEmergencyFundCode.objects.values_list("code", flat=True)), "allow_nulls": False, "optional": False, }, { "key": "filters|query", "name": "query", "type": "text", "text_type": "search", "allow_nulls": False, "optional": True, }, { "key": "filters|award_type_codes", "name": "award_type_codes", "type": "array", "array_type": "enum", "enum_values": sorted(award_type_mapping.keys()), "allow_nulls": False, "optional": True, }, ] filters = TinyShield(model).block(request_data)["filters"] # Determine what to use in the filename based on "award_type_codes" filter; # Also add "face_value_of_loans" column if only loan types award_category = "All-Awards" award_type_codes = set( filters.get("award_type_codes", award_type_mapping.keys())) columns = [ "recipient", "award_obligations", "award_outlays", "number_of_awards" ] if award_type_codes <= set(contract_type_mapping.keys()): award_category = "Contracts" elif award_type_codes <= set(idv_type_mapping.keys()): award_category = "Contract-IDVs" elif award_type_codes <= set(grant_type_mapping.keys()): award_category = "Grants" elif award_type_codes <= set(loan_type_mapping.keys()): award_category = "Loans" columns.insert(3, "face_value_of_loans") elif award_type_codes <= set(direct_payment_type_mapping.keys()): award_category = "Direct-Payments" elif award_type_codes <= set(other_type_mapping.keys()): award_category = "Other-Financial-Assistance" # Need to specify the field to use "query" filter on if present query_text = filters.pop("query", None) if query_text: filters["query"] = {"text": query_text, "fields": ["recipient_name"]} json_request = { "award_category": award_category, "columns": tuple(columns), "download_types": ["disaster_recipient"], "file_format": str(request_data.get("file_format", "csv")).lower(), "filters": filters, } _validate_file_format(json_request) return json_request
def __init__(self, request_data: dict): super().__init__(request_data) self.tinyshield_models.extend([ { "key": "filters|def_codes", "name": "def_codes", "type": "array", "array_type": "enum", "enum_values": sorted( DisasterEmergencyFundCode.objects.values_list("code", flat=True)), "allow_nulls": False, "optional": False, }, { "key": "filters|query", "name": "query", "type": "text", "text_type": "search", "allow_nulls": False, "optional": True, }, { "key": "filters|award_type_codes", "name": "award_type_codes", "type": "array", "array_type": "enum", "enum_values": sorted(award_type_mapping.keys()), "allow_nulls": False, "optional": True, }, ]) self._json_request["filters"] = request_data.get("filters") self._json_request = self.get_validated_request() self._json_request["download_types"] = [self.name] # Determine what to use in the filename based on "award_type_codes" filter; # Also add "face_value_of_loans" column if only loan types award_category = "All-Awards" award_type_codes = set(self._json_request["filters"].get( "award_type_codes", award_type_mapping.keys())) columns = [ "recipient", "award_obligations", "award_outlays", "number_of_awards" ] if award_type_codes <= set(contract_type_mapping.keys()): award_category = "Contracts" elif award_type_codes <= set(idv_type_mapping.keys()): award_category = "Contract-IDVs" elif award_type_codes <= set(grant_type_mapping.keys()): award_category = "Grants" elif award_type_codes <= set(loan_type_mapping.keys()): award_category = "Loans" columns.insert(3, "face_value_of_loans") elif award_type_codes <= set(direct_payment_type_mapping.keys()): award_category = "Direct-Payments" elif award_type_codes <= set(other_type_mapping.keys()): award_category = "Other-Financial-Assistance" self._json_request["award_category"] = award_category self._json_request["columns"] = self._json_request.get( "columns") or tuple(columns) # Need to specify the field to use "query" filter on if present query_text = self._json_request["filters"].pop("query", None) if query_text: self._json_request["filters"]["query"] = { "text": query_text, "fields": ["recipient_name"] }
idv_type_mapping, loan_type_mapping, other_type_mapping, procurement_type_mapping, ) TYPES_TO_QUOTE_IN_SQL = (str, date) CATEGORY_TO_MODEL = { "contracts": { "model": ContractAwardSearchMatview, "types": set(contract_type_mapping.keys()) }, "direct_payments": { "model": DirectPaymentAwardSearchMatview, "types": set(direct_payment_type_mapping.keys()) }, "grants": { "model": GrantAwardSearchMatview, "types": set(grant_type_mapping.keys()) }, "idvs": { "model": IDVAwardSearchMatview, "types": set(idv_type_mapping.keys()) }, "loans": { "model": LoanAwardSearchMatview, "types": set(loan_type_mapping.keys()) }, "other": { "model": OtherAwardSearchMatview,
contract_type_mapping, direct_payment_type_mapping, grant_type_mapping, idv_type_mapping, loan_type_mapping, other_type_mapping, procurement_type_mapping, ) TYPES_TO_QUOTE_IN_SQL = (str, date) CATEGORY_TO_MODEL = { "contracts": {"model": ContractAwardSearchMatview, "types": set(contract_type_mapping.keys())}, "direct_payments": {"model": DirectPaymentAwardSearchMatview, "types": set(direct_payment_type_mapping.keys())}, "grants": {"model": GrantAwardSearchMatview, "types": set(grant_type_mapping.keys())}, "idvs": {"model": IDVAwardSearchMatview, "types": set(idv_type_mapping.keys())}, "loans": {"model": LoanAwardSearchMatview, "types": set(loan_type_mapping.keys())}, "other": {"model": OtherAwardSearchMatview, "types": set(other_type_mapping.keys())}, } class AwardGroupsException(Exception): """Custom Exception for a specific event""" class FiscalMonth(Func): function = "EXTRACT" template = "%(function)s(MONTH from (%(expressions)s) + INTERVAL '3 months')" output_field = IntegerField()