From c9e8e55aad4754f0f90227b763339d59ccdf1c26 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 28 Apr 2025 21:13:03 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20Add=20contact=20form=20submission=20end?= =?UTF-8?q?point=20with=20validation=20=E2=9C=85=20(auto-linted)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...428_211240_5971bf19_update_contact_form.py | 33 ++++++++++ endpoints/contact.post.py | 18 ++++++ helpers/contact_form_helpers.py | 63 +++++++++++++++++++ models/contact_form.py | 26 ++++++++ requirements.txt | 4 ++ schemas/contact_form.py | 25 ++++++++ 6 files changed, 169 insertions(+) create mode 100644 alembic/versions/20250428_211240_5971bf19_update_contact_form.py create mode 100644 helpers/contact_form_helpers.py create mode 100644 models/contact_form.py create mode 100644 schemas/contact_form.py diff --git a/alembic/versions/20250428_211240_5971bf19_update_contact_form.py b/alembic/versions/20250428_211240_5971bf19_update_contact_form.py new file mode 100644 index 0000000..fd8c470 --- /dev/null +++ b/alembic/versions/20250428_211240_5971bf19_update_contact_form.py @@ -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') \ No newline at end of file diff --git a/endpoints/contact.post.py b/endpoints/contact.post.py index e69de29..ad54782 100644 --- a/endpoints/contact.post.py +++ b/endpoints/contact.post.py @@ -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 \ No newline at end of file diff --git a/helpers/contact_form_helpers.py b/helpers/contact_form_helpers.py new file mode 100644 index 0000000..3e5b454 --- /dev/null +++ b/helpers/contact_form_helpers.py @@ -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)) \ No newline at end of file diff --git a/models/contact_form.py b/models/contact_form.py new file mode 100644 index 0000000..0aa0bcd --- /dev/null +++ b/models/contact_form.py @@ -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(), + ) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..ec13e88 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/schemas/contact_form.py b/schemas/contact_form.py new file mode 100644 index 0000000..653bbd9 --- /dev/null +++ b/schemas/contact_form.py @@ -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 \ No newline at end of file