|
| 1 | +from pydantic import BaseModel, field_validator |
| 2 | +from typing import List, Optional |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +import logging |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | +logging.basicConfig(level=logging.INFO) |
| 8 | +logger.setLevel(logging.INFO) |
| 9 | + |
| 10 | + |
| 11 | +class DisasterType(BaseModel): |
| 12 | + id: int |
| 13 | + name: str |
| 14 | + summary: str |
| 15 | + translation_module_original_language: str |
| 16 | + |
| 17 | +class Country(BaseModel): |
| 18 | + iso: str |
| 19 | + iso3: str |
| 20 | + id: int |
| 21 | + record_type: int |
| 22 | + record_type_display: str |
| 23 | + region: int |
| 24 | + independent: bool |
| 25 | + is_deprecated: bool |
| 26 | + fdrs: Optional[str] |
| 27 | + average_household_size: Optional[float] |
| 28 | + society_name: str |
| 29 | + name: str |
| 30 | + translation_module_original_language: str |
| 31 | + |
| 32 | +class Appeal(BaseModel): |
| 33 | + aid: str |
| 34 | + num_beneficiaries: int |
| 35 | + amount_requested: float |
| 36 | + code: str |
| 37 | + amount_funded: float |
| 38 | + status: int |
| 39 | + status_display: str |
| 40 | + start_date: datetime |
| 41 | + atype: int |
| 42 | + atype_display: str |
| 43 | + id: int |
| 44 | + translation_module_original_language: str |
| 45 | + |
| 46 | +class Contact(BaseModel): |
| 47 | + ctype: str |
| 48 | + name: str |
| 49 | + title: str |
| 50 | + email: str |
| 51 | + phone: str |
| 52 | + id: int |
| 53 | + |
| 54 | +class FieldReport(BaseModel): |
| 55 | + status: int |
| 56 | + contacts: List[Contact] |
| 57 | + countries: List[Country] |
| 58 | + created_at: datetime |
| 59 | + updated_at: datetime |
| 60 | + report_date: datetime |
| 61 | + id: int |
| 62 | + is_covid_report: bool |
| 63 | + num_assisted: Optional[int] |
| 64 | + num_displaced: Optional[int] |
| 65 | + gov_num_dead: Optional[int] |
| 66 | + gov_num_injured: Optional[int] |
| 67 | + other_num_displaced: Optional[int] |
| 68 | + affected_pop_centres: Optional[str] |
| 69 | + gov_affected_pop_centres: Optional[str] |
| 70 | + other_affected_pop_centres: Optional[str] |
| 71 | + description: str |
| 72 | + summary: str |
| 73 | + translation_module_original_language: str |
| 74 | + |
| 75 | +class IFRCsourceValidator(BaseModel): |
| 76 | + dtype: DisasterType |
| 77 | + countries: List[Country] |
| 78 | + num_affected: Optional[int] |
| 79 | + ifrc_severity_level: int |
| 80 | + ifrc_severity_level_display: str |
| 81 | + glide: Optional[str] |
| 82 | + disaster_start_date: datetime |
| 83 | + created_at: datetime |
| 84 | + auto_generated: bool |
| 85 | + appeals: List[Appeal] |
| 86 | + is_featured: bool |
| 87 | + is_featured_region: bool |
| 88 | + field_reports: List[FieldReport] |
| 89 | + updated_at: datetime |
| 90 | + id: int |
| 91 | + slug: Optional[str] |
| 92 | + parent_event: Optional[str] |
| 93 | + tab_one_title: Optional[str] |
| 94 | + tab_two_title: Optional[str] |
| 95 | + tab_three_title: Optional[str] |
| 96 | + emergency_response_contact_email: Optional[str] |
| 97 | + active_deployments: int |
| 98 | + name: str |
| 99 | + summary: str |
| 100 | + translation_module_original_language: str |
| 101 | + |
| 102 | + @field_validator("ifrc_severity_level") |
| 103 | + def validate_severity_level(cls, value): |
| 104 | + if value not in range(0, 4): |
| 105 | + raise ValueError("Invalid severity level, must be between 0 and 3") |
| 106 | + return value |
| 107 | + |
| 108 | + @classmethod |
| 109 | + def validate_event(cls, data: dict) -> bool: |
| 110 | + """Validate the overall data item""" |
| 111 | + try: |
| 112 | + _ = cls(**data) # This will trigger the validators |
| 113 | + except Exception as e: |
| 114 | + logger.error(f"Validation failed: {e}") |
| 115 | + return False |
| 116 | + # If all field validators return True, we consider it valid |
| 117 | + return True |
| 118 | + |
| 119 | + |
0 commit comments