From 1bc11fdb155148f91def41d32fb6d46fa5a5f87d Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 15 Apr 2025 18:32:24 +0000 Subject: [PATCH] feat: Updated endpoint endpoints/contact.post.py via AI --- ...20250415_183218_cf957b31_update_contact.py | 19 +++++++++++++++++++ endpoints/contact.post.py | 6 +++--- helpers/contact_helpers.py | 12 +++++++++++- models/contact.py | 1 + schemas/contact.py | 3 +++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 alembic/versions/20250415_183218_cf957b31_update_contact.py diff --git a/alembic/versions/20250415_183218_cf957b31_update_contact.py b/alembic/versions/20250415_183218_cf957b31_update_contact.py new file mode 100644 index 0000000..f95df12 --- /dev/null +++ b/alembic/versions/20250415_183218_cf957b31_update_contact.py @@ -0,0 +1,19 @@ +"""add country field to contacts table +Revision ID: 9d4b82f3a1e7 +Revises: 8f3a91d2e4c5 +Create Date: 2024-01-18 10:23:45.123456 +""" +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = '9d4b82f3a1e7' +down_revision = '8f3a91d2e4c5' +branch_labels = None +depends_on = None + +def upgrade(): + op.add_column('contacts', sa.Column('country', sa.String(), nullable=True)) + +def downgrade(): + op.drop_column('contacts', 'country') \ No newline at end of file diff --git a/endpoints/contact.post.py b/endpoints/contact.post.py index 88260b6..28bfad0 100644 --- a/endpoints/contact.post.py +++ b/endpoints/contact.post.py @@ -7,10 +7,10 @@ from helpers.contact_helpers import create_contact, sanitize_contact_data, forma router = APIRouter() @router.post("/contact", status_code=status.HTTP_201_CREATED, response_model=ContactSchema) -async def create_contact_submission( +async def create_contact_form( contact_data: ContactCreate, db: Session = Depends(get_db) ): sanitized_data = sanitize_contact_data(contact_data) - new_contact = create_contact(db=db, contact_data=sanitized_data) - return format_contact_response(new_contact) \ No newline at end of file + db_contact = create_contact(db=db, contact_data=sanitized_data) + return format_contact_response(db_contact) \ No newline at end of file diff --git a/helpers/contact_helpers.py b/helpers/contact_helpers.py index 4ea9a03..3f23b74 100644 --- a/helpers/contact_helpers.py +++ b/helpers/contact_helpers.py @@ -43,6 +43,10 @@ def validate_contact_data(contact_data: ContactCreate) -> Dict[str, str]: if not (10 <= len(cleaned_phone) <= 15): errors["phone_number"] = "Phone number must be between 10 and 15 digits" + # Validate country if provided + if contact_data.country and len(contact_data.country.strip()) > 255: + errors["country"] = "Country name must not exceed 255 characters" + return errors def create_contact(db: Session, contact_data: ContactCreate) -> Contact: @@ -109,12 +113,18 @@ def sanitize_contact_data(contact_data: ContactCreate) -> ContactCreate: else: phone_number = ''.join(filter(str.isdigit, phone_number)) + # Sanitize country if provided + country = None + if contact_data.country: + country = contact_data.country.strip() + # Create a new dict with sanitized values sanitized_data = ContactCreate( name=contact_data.name.strip(), email=email, phone_number=phone_number, - message=contact_data.message.strip() + message=contact_data.message.strip(), + country=country ) return sanitized_data except EmailNotValidError as e: diff --git a/models/contact.py b/models/contact.py index 1d8b0c2..88f62a5 100644 --- a/models/contact.py +++ b/models/contact.py @@ -12,6 +12,7 @@ class Contact(Base): email = Column(String, nullable=False, index=True) phone_number = Column(String, nullable=True) message = Column(Text, nullable=False) + country = Column(String, nullable=True) created_at = Column(DateTime, default=func.now()) updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/schemas/contact.py b/schemas/contact.py index 64b4fe3..8a3fc65 100644 --- a/schemas/contact.py +++ b/schemas/contact.py @@ -8,6 +8,7 @@ class ContactBase(BaseModel): email: EmailStr = Field(..., description="Contact email address", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$") phone_number: Optional[str] = Field(None, description="Contact phone number") message: str = Field(..., min_length=1, description="Contact message") + country: Optional[str] = Field(None, description="Contact country") class ContactCreate(ContactBase): pass @@ -17,6 +18,7 @@ class ContactUpdate(BaseModel): email: Optional[EmailStr] = Field(None, description="Contact email address", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$") phone_number: Optional[str] = Field(None, description="Contact phone number") message: Optional[str] = Field(None, min_length=1, description="Contact message") + country: Optional[str] = Field(None, description="Contact country") class ContactSchema(ContactBase): id: UUID @@ -32,6 +34,7 @@ class ContactSchema(ContactBase): "email": "john.doe@example.com", "phone_number": "+1234567890", "message": "Hello, I would like to get in touch.", + "country": "United States", "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00" }