feat: Updated endpoint endpoints/contact.post.py via AI
This commit is contained in:
parent
46c049cb06
commit
28e201a5bf
19
alembic/versions/20250415_182746_3f75cae3_update_contact.py
Normal file
19
alembic/versions/20250415_182746_3f75cae3_update_contact.py
Normal file
@ -0,0 +1,19 @@
|
||||
"""add phone_number field to contacts table
|
||||
Revision ID: 8f3a91d2e4c5
|
||||
Revises: 0002
|
||||
Create Date: 2024-01-30 10:00:00.000000
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '8f3a91d2e4c5'
|
||||
down_revision = '0002'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.add_column('contacts', sa.Column('phone_number', sa.String(), nullable=True))
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('contacts', 'phone_number')
|
@ -11,12 +11,6 @@ async def create_contact_submission(
|
||||
contact_data: ContactCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Create a new contact submission with email validation"""
|
||||
# Sanitize input data
|
||||
sanitized_data = sanitize_contact_data(contact_data)
|
||||
|
||||
# Create contact using helper function
|
||||
contact = create_contact(db=db, contact_data=sanitized_data)
|
||||
|
||||
# Format response
|
||||
return format_contact_response(contact)
|
||||
new_contact = create_contact(db=db, contact_data=sanitized_data)
|
||||
return format_contact_response(new_contact)
|
@ -35,6 +35,14 @@ def validate_contact_data(contact_data: ContactCreate) -> Dict[str, str]:
|
||||
if not contact_data.message or not contact_data.message.strip():
|
||||
errors["message"] = "Message is required"
|
||||
|
||||
# Validate phone number format if provided
|
||||
if contact_data.phone_number:
|
||||
# Basic phone number format validation
|
||||
# Remove any spaces, dashes, or parentheses
|
||||
cleaned_phone = ''.join(filter(str.isdigit, contact_data.phone_number))
|
||||
if not (10 <= len(cleaned_phone) <= 15):
|
||||
errors["phone_number"] = "Phone number must be between 10 and 15 digits"
|
||||
|
||||
return errors
|
||||
|
||||
def create_contact(db: Session, contact_data: ContactCreate) -> Contact:
|
||||
@ -91,10 +99,21 @@ def sanitize_contact_data(contact_data: ContactCreate) -> ContactCreate:
|
||||
email = contact_data.email.strip().lower()
|
||||
validate_email(email)
|
||||
|
||||
# Sanitize phone number if provided
|
||||
phone_number = None
|
||||
if contact_data.phone_number:
|
||||
# Remove any non-digit characters except '+' at the start
|
||||
phone_number = contact_data.phone_number.strip()
|
||||
if phone_number.startswith('+'):
|
||||
phone_number = '+' + ''.join(filter(str.isdigit, phone_number[1:]))
|
||||
else:
|
||||
phone_number = ''.join(filter(str.isdigit, phone_number))
|
||||
|
||||
# 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()
|
||||
)
|
||||
return sanitized_data
|
||||
|
@ -10,6 +10,7 @@ class Contact(Base):
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False)
|
||||
email = Column(String, nullable=False, index=True)
|
||||
phone_number = Column(String, nullable=True)
|
||||
message = Column(Text, nullable=False)
|
||||
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
|
@ -6,6 +6,7 @@ from uuid import UUID
|
||||
class ContactBase(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=255, description="Contact name")
|
||||
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")
|
||||
|
||||
class ContactCreate(ContactBase):
|
||||
@ -14,6 +15,7 @@ class ContactCreate(ContactBase):
|
||||
class ContactUpdate(BaseModel):
|
||||
name: Optional[str] = Field(None, min_length=1, max_length=255, description="Contact name")
|
||||
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")
|
||||
|
||||
class ContactSchema(ContactBase):
|
||||
@ -27,7 +29,8 @@ class ContactSchema(ContactBase):
|
||||
"example": {
|
||||
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||
"name": "John Doe",
|
||||
"email": "john.doe@example.com",
|
||||
"email": "john.doe@example.com",
|
||||
"phone_number": "+1234567890",
|
||||
"message": "Hello, I would like to get in touch.",
|
||||
"created_at": "2023-01-01T12:00:00",
|
||||
"updated_at": "2023-01-01T12:00:00"
|
||||
|
Loading…
x
Reference in New Issue
Block a user