feat: Updated endpoint endpoints/contact.post.py via AI
This commit is contained in:
parent
28e201a5bf
commit
1bc11fdb15
19
alembic/versions/20250415_183218_cf957b31_update_contact.py
Normal file
19
alembic/versions/20250415_183218_cf957b31_update_contact.py
Normal 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')
|
@ -7,10 +7,10 @@ from helpers.contact_helpers import create_contact, sanitize_contact_data, forma
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/contact", status_code=status.HTTP_201_CREATED, response_model=ContactSchema)
|
@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,
|
contact_data: ContactCreate,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
sanitized_data = sanitize_contact_data(contact_data)
|
sanitized_data = sanitize_contact_data(contact_data)
|
||||||
new_contact = create_contact(db=db, contact_data=sanitized_data)
|
db_contact = create_contact(db=db, contact_data=sanitized_data)
|
||||||
return format_contact_response(new_contact)
|
return format_contact_response(db_contact)
|
@ -43,6 +43,10 @@ def validate_contact_data(contact_data: ContactCreate) -> Dict[str, str]:
|
|||||||
if not (10 <= len(cleaned_phone) <= 15):
|
if not (10 <= len(cleaned_phone) <= 15):
|
||||||
errors["phone_number"] = "Phone number must be between 10 and 15 digits"
|
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
|
return errors
|
||||||
|
|
||||||
def create_contact(db: Session, contact_data: ContactCreate) -> Contact:
|
def create_contact(db: Session, contact_data: ContactCreate) -> Contact:
|
||||||
@ -109,12 +113,18 @@ def sanitize_contact_data(contact_data: ContactCreate) -> ContactCreate:
|
|||||||
else:
|
else:
|
||||||
phone_number = ''.join(filter(str.isdigit, phone_number))
|
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
|
# Create a new dict with sanitized values
|
||||||
sanitized_data = ContactCreate(
|
sanitized_data = ContactCreate(
|
||||||
name=contact_data.name.strip(),
|
name=contact_data.name.strip(),
|
||||||
email=email,
|
email=email,
|
||||||
phone_number=phone_number,
|
phone_number=phone_number,
|
||||||
message=contact_data.message.strip()
|
message=contact_data.message.strip(),
|
||||||
|
country=country
|
||||||
)
|
)
|
||||||
return sanitized_data
|
return sanitized_data
|
||||||
except EmailNotValidError as e:
|
except EmailNotValidError as e:
|
||||||
|
@ -12,6 +12,7 @@ class Contact(Base):
|
|||||||
email = Column(String, nullable=False, index=True)
|
email = Column(String, nullable=False, index=True)
|
||||||
phone_number = Column(String, nullable=True)
|
phone_number = Column(String, nullable=True)
|
||||||
message = Column(Text, nullable=False)
|
message = Column(Text, nullable=False)
|
||||||
|
country = Column(String, nullable=True)
|
||||||
|
|
||||||
created_at = Column(DateTime, default=func.now())
|
created_at = Column(DateTime, default=func.now())
|
||||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
@ -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,}$")
|
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")
|
phone_number: Optional[str] = Field(None, description="Contact phone number")
|
||||||
message: str = Field(..., min_length=1, description="Contact message")
|
message: str = Field(..., min_length=1, description="Contact message")
|
||||||
|
country: Optional[str] = Field(None, description="Contact country")
|
||||||
|
|
||||||
class ContactCreate(ContactBase):
|
class ContactCreate(ContactBase):
|
||||||
pass
|
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,}$")
|
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")
|
phone_number: Optional[str] = Field(None, description="Contact phone number")
|
||||||
message: Optional[str] = Field(None, min_length=1, description="Contact message")
|
message: Optional[str] = Field(None, min_length=1, description="Contact message")
|
||||||
|
country: Optional[str] = Field(None, description="Contact country")
|
||||||
|
|
||||||
class ContactSchema(ContactBase):
|
class ContactSchema(ContactBase):
|
||||||
id: UUID
|
id: UUID
|
||||||
@ -32,6 +34,7 @@ class ContactSchema(ContactBase):
|
|||||||
"email": "john.doe@example.com",
|
"email": "john.doe@example.com",
|
||||||
"phone_number": "+1234567890",
|
"phone_number": "+1234567890",
|
||||||
"message": "Hello, I would like to get in touch.",
|
"message": "Hello, I would like to get in touch.",
|
||||||
|
"country": "United States",
|
||||||
"created_at": "2023-01-01T12:00:00",
|
"created_at": "2023-01-01T12:00:00",
|
||||||
"updated_at": "2023-01-01T12:00:00"
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user