feat: Updated endpoint endpoints/contact.post.py via AI

This commit is contained in:
Backend IM Bot 2025-04-15 18:32:24 +00:00
parent 28e201a5bf
commit 1bc11fdb15
5 changed files with 37 additions and 4 deletions

View File

@ -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')

View File

@ -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)
db_contact = create_contact(db=db, contact_data=sanitized_data)
return format_contact_response(db_contact)

View File

@ -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:

View File

@ -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())

View File

@ -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"
}