feat: Add contact form submission endpoint with validation ✅ (auto-linted)
This commit is contained in:
parent
9ddef2a154
commit
c9e8e55aad
@ -0,0 +1,33 @@
|
|||||||
|
"""create table for contact_forms
|
||||||
|
|
||||||
|
Revision ID: 9a7d63f4b8f2
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-23 16:38:48.896277
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '9a7d63f4b8f2'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'contact_forms',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('email', sa.String(), nullable=False),
|
||||||
|
sa.Column('message', sa.Text(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('contact_forms')
|
@ -0,0 +1,18 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.contact_form import ContactFormCreate, ContactFormSchema
|
||||||
|
from helpers.contact_form_helpers import create_contact_form
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/contact", status_code=status.HTTP_201_CREATED, response_model=ContactFormSchema)
|
||||||
|
async def create_contact_form_endpoint(
|
||||||
|
contact_form_data: ContactFormCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
new_contact_form = create_contact_form(db=db, contact_form_data=contact_form_data)
|
||||||
|
return new_contact_form
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
63
helpers/contact_form_helpers.py
Normal file
63
helpers/contact_form_helpers.py
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import re
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from models.contact_form import ContactForm
|
||||||
|
from schemas.contact_form import ContactFormCreate
|
||||||
|
import email_validator
|
||||||
|
|
||||||
|
def create_contact_form(db: Session, contact_form_data: ContactFormCreate) -> ContactForm:
|
||||||
|
"""
|
||||||
|
Creates a new contact form in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
contact_form_data (ContactFormCreate): The data for the contact form to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
ContactForm: The newly created contact form object.
|
||||||
|
"""
|
||||||
|
validate_contact_form_data(contact_form_data)
|
||||||
|
db_contact_form = ContactForm(**contact_form_data.dict())
|
||||||
|
db.add(db_contact_form)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_contact_form)
|
||||||
|
return db_contact_form
|
||||||
|
|
||||||
|
def validate_contact_form_data(contact_form_data: ContactFormCreate):
|
||||||
|
"""
|
||||||
|
Validates the contact form data.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
contact_form_data (ContactFormCreate): The contact form data to validate.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPException: If any validation fails.
|
||||||
|
"""
|
||||||
|
name = contact_form_data.name
|
||||||
|
email = contact_form_data.email
|
||||||
|
message = contact_form_data.message
|
||||||
|
|
||||||
|
if not name:
|
||||||
|
raise HTTPException(status_code=400, detail="Name is required")
|
||||||
|
if not email:
|
||||||
|
raise HTTPException(status_code=400, detail="Email is required")
|
||||||
|
if not message:
|
||||||
|
raise HTTPException(status_code=400, detail="Message is required")
|
||||||
|
|
||||||
|
try:
|
||||||
|
email_validator.validate_email(email)
|
||||||
|
except email_validator.EmailNotValidError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
|
||||||
|
def validate_email(email: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validates an email address using a regular expression.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
email (str): The email address to validate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the email is valid, False otherwise.
|
||||||
|
"""
|
||||||
|
email_regex = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
||||||
|
return bool(re.match(email_regex, email))
|
26
models/contact_form.py
Normal file
26
models/contact_form.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from sqlalchemy import Column, String, Text, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from core.database import Base
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class ContactForm(Base):
|
||||||
|
__tablename__ = "contact_forms"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False)
|
||||||
|
email = Column(String, nullable=False)
|
||||||
|
message = Column(Text, nullable=False)
|
||||||
|
created_at = Column(
|
||||||
|
"created_at",
|
||||||
|
DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=func.now(),
|
||||||
|
)
|
||||||
|
updated_at = Column(
|
||||||
|
"updated_at",
|
||||||
|
DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=func.now(),
|
||||||
|
onupdate=func.now(),
|
||||||
|
)
|
@ -7,3 +7,7 @@ sqlalchemy>=1.4.0
|
|||||||
python-dotenv>=0.19.0
|
python-dotenv>=0.19.0
|
||||||
bcrypt>=3.2.0
|
bcrypt>=3.2.0
|
||||||
alembic>=1.13.1
|
alembic>=1.13.1
|
||||||
|
email_validator
|
||||||
|
jose
|
||||||
|
passlib
|
||||||
|
pydantic
|
||||||
|
25
schemas/contact_form.py
Normal file
25
schemas/contact_form.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from pydantic import BaseModel, Field, EmailStr
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class ContactFormBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the contact form submitter")
|
||||||
|
email: EmailStr = Field(..., description="Email of the contact form submitter")
|
||||||
|
message: str = Field(..., description="Message from the contact form submitter")
|
||||||
|
|
||||||
|
class ContactFormCreate(ContactFormBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ContactFormUpdate(ContactFormBase):
|
||||||
|
name: Optional[str] = Field(None, description="Updated name of the contact form submitter")
|
||||||
|
email: Optional[EmailStr] = Field(None, description="Updated email of the contact form submitter")
|
||||||
|
message: Optional[str] = Field(None, description="Updated message from the contact form submitter")
|
||||||
|
|
||||||
|
class ContactFormSchema(ContactFormBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user