""" PHONE CSV Importer """ from typing import Dict, List, Any from sqlalchemy.orm import Session from .base import BaseCSVImporter, ImportValidationError from app.models.rolodex import Phone, Rolodex class PhoneCSVImporter(BaseCSVImporter): """CSV importer for PHONE table""" @property def table_name(self) -> str: return "phone" @property def required_fields(self) -> List[str]: return ["rolodex_id", "phone"] # rolodex_id and phone number are required @property def field_mapping(self) -> Dict[str, str]: """Map CSV headers to database field names""" return { "rolodex_id": "rolodex_id", "location": "location", "phone": "phone" } def create_model_instance(self, row_data: Dict[str, Any]) -> Phone: """Create a Phone instance from processed row data""" # Validate required fields if not row_data.get("rolodex_id"): raise ImportValidationError("Rolodex ID is required") if not row_data.get("phone"): raise ImportValidationError("Phone number is required") # Validate foreign key exists rolodex_exists = self.db_session.query(Rolodex).filter_by( id=row_data["rolodex_id"] ).first() if not rolodex_exists: raise ImportValidationError(f"Rolodex ID '{row_data['rolodex_id']}' does not exist") # Create instance with field length validation phone = Phone( rolodex_id=self.normalize_string(row_data["rolodex_id"], 80), location=self.normalize_string(row_data.get("location", ""), 45), phone=self.normalize_string(row_data["phone"], 45) ) return phone