✨ feat: Add new endpoints/contact.post.py endpoint for Contact_form 🚀 📦 with updated dependencies ✅ (auto-linted)
This commit is contained in:
parent
b6218253ba
commit
971645e32a
@ -0,0 +1,31 @@
|
|||||||
|
"""create table for contact_forms
|
||||||
|
|
||||||
|
Revision ID: 2b9e7c8a3f0e
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-24 12:34:56
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2b9e7c8a3f0e'
|
||||||
|
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(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('contact_forms')
|
@ -0,0 +1,19 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException, status
|
||||||
|
from schemas.contact_form import ContactFormCreate, ContactFormSchema
|
||||||
|
from helpers.contact_form_helpers import create_contact_form, validate_email
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/contact", status_code=status.HTTP_201_CREATED, response_model=ContactFormSchema)
|
||||||
|
async def create_contact(
|
||||||
|
contact_form: ContactFormCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
if not validate_email(contact_form.email):
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid email format")
|
||||||
|
|
||||||
|
created_contact_form = create_contact_form(db=db, contact_form_data=contact_form)
|
||||||
|
return created_contact_form
|
50
helpers/contact_form_helpers.py
Normal file
50
helpers/contact_form_helpers.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.contact_form import ContactForm
|
||||||
|
from schemas.contact_form import ContactFormCreate
|
||||||
|
from fastapi import HTTPException
|
||||||
|
import email_validator
|
||||||
|
|
||||||
|
def validate_email(email: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validates the provided email address using the email_validator package.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
email (str): The email address to validate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the email is valid, False otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
email_validator.validate_email(email)
|
||||||
|
return True
|
||||||
|
except email_validator.EmailNotValidError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPException: If any required field is missing or the email is invalid.
|
||||||
|
"""
|
||||||
|
if not contact_form_data.name:
|
||||||
|
raise HTTPException(status_code=400, detail="Name is required")
|
||||||
|
if not contact_form_data.email:
|
||||||
|
raise HTTPException(status_code=400, detail="Email is required")
|
||||||
|
if not validate_email(contact_form_data.email):
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid email format")
|
||||||
|
if not contact_form_data.message:
|
||||||
|
raise HTTPException(status_code=400, detail="Message is required")
|
||||||
|
|
||||||
|
db_contact_form = ContactForm(**contact_form_data.dict())
|
||||||
|
db.add(db_contact_form)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_contact_form)
|
||||||
|
return db_contact_form
|
15
models/contact_form.py
Normal file
15
models/contact_form.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, 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, EmailStr, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class ContactFormBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Contact name")
|
||||||
|
email: EmailStr = Field(..., description="Contact email address")
|
||||||
|
message: str = Field(..., description="Contact message")
|
||||||
|
|
||||||
|
class ContactFormCreate(ContactFormBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ContactFormUpdate(ContactFormBase):
|
||||||
|
name: Optional[str] = Field(None, description="Contact name")
|
||||||
|
email: Optional[EmailStr] = Field(None, description="Contact email address")
|
||||||
|
message: Optional[str] = Field(None, description="Contact message")
|
||||||
|
|
||||||
|
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