✨ feat: Add new endpoints/contact.post.py endpoint for Contact_form 🚀 📦 with updated dependencies
This commit is contained in:
parent
e41f15f2db
commit
b13dd2b0ec
@ -0,0 +1,29 @@
|
||||
"""create table for contact_forms
|
||||
Revision ID: 6b8d2c9d1234
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-15 12:34:56
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '6b8d2c9d1234'
|
||||
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=func.uuid_generate_v4()),
|
||||
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.Text(), nullable=False, default=func.now()),
|
||||
sa.Column('updated_at', sa.Text(), nullable=False, default=func.now(), onupdate=func.now())
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('contact_forms')
|
@ -0,0 +1,22 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from schemas.contact_form import ContactFormCreate
|
||||
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=ContactFormCreate)
|
||||
async def create_contact(contact_form: ContactFormCreate, db: Session = Depends(get_db)):
|
||||
if not contact_form.name:
|
||||
raise HTTPException(status_code=400, detail="Name is required")
|
||||
if not contact_form.email:
|
||||
raise HTTPException(status_code=400, detail="Email is required")
|
||||
if not validate_email(contact_form.email):
|
||||
raise HTTPException(status_code=400, detail="Invalid email format")
|
||||
if not contact_form.message:
|
||||
raise HTTPException(status_code=400, detail="Message is required")
|
||||
|
||||
new_contact_form = create_contact_form(db=db, contact_form_data=contact_form)
|
||||
return new_contact_form
|
46
helpers/contact_form_helpers.py
Normal file
46
helpers/contact_form_helpers.py
Normal file
@ -0,0 +1,46 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from models.contact_form import ContactForm
|
||||
from schemas.contact_form import ContactFormCreate
|
||||
import email_validator
|
||||
|
||||
def validate_email(email: str) -> bool:
|
||||
"""
|
||||
Validates if the given email is in a valid format.
|
||||
|
||||
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.
|
||||
"""
|
||||
if not contact_form_data.name:
|
||||
raise ValueError("Name is required")
|
||||
if not contact_form_data.email:
|
||||
raise ValueError("Email is required")
|
||||
if not validate_email(contact_form_data.email):
|
||||
raise ValueError("Invalid email format")
|
||||
if not contact_form_data.message:
|
||||
raise ValueError("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
|
||||
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(Text, default=func.now())
|
||||
updated_at = Column(Text, default=func.now(), onupdate=func.now())
|
@ -7,3 +7,7 @@ sqlalchemy>=1.4.0
|
||||
python-dotenv>=0.19.0
|
||||
bcrypt>=3.2.0
|
||||
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")
|
||||
email: EmailStr = Field(..., description="Email of the contact")
|
||||
message: str = Field(..., description="Message from the contact")
|
||||
|
||||
class ContactFormCreate(ContactFormBase):
|
||||
pass
|
||||
|
||||
class ContactFormUpdate(ContactFormBase):
|
||||
name: Optional[str] = Field(None, description="Name of the contact")
|
||||
email: Optional[EmailStr] = Field(None, description="Email of the contact")
|
||||
message: Optional[str] = Field(None, description="Message from the contact")
|
||||
|
||||
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