major claude changes

This commit is contained in:
2026-01-28 21:55:10 -05:00
parent 3f311980db
commit 2dbd3ea53f
41 changed files with 1235 additions and 278 deletions

4
app/schemas/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
# Validation schemas for API requests
from .customer import CreateCustomerSchema, UpdateCustomerSchema, UpdateDescriptionSchema
from .auth import LoginSchema, RegisterSchema, ChangePasswordSchema
from .utils import validate_request, validate_json

56
app/schemas/auth.py Normal file
View File

@@ -0,0 +1,56 @@
from marshmallow import Schema, fields, validate, EXCLUDE
class LoginSchema(Schema):
"""Validation schema for user login"""
class Meta:
unknown = EXCLUDE
username = fields.Str(
required=True,
validate=validate.Length(min=3, max=100),
error_messages={"required": "Username is required"}
)
password = fields.Str(
required=True,
validate=validate.Length(min=6, max=200),
error_messages={"required": "Password is required"}
)
class RegisterSchema(Schema):
"""Validation schema for user registration"""
class Meta:
unknown = EXCLUDE
username = fields.Str(
required=True,
validate=validate.Length(min=3, max=100),
error_messages={"required": "Username is required"}
)
password = fields.Str(
required=True,
validate=validate.Length(min=6, max=200),
error_messages={"required": "Password is required"}
)
email = fields.Email(
required=True,
error_messages={"required": "Email is required"}
)
class ChangePasswordSchema(Schema):
"""Validation schema for password change"""
class Meta:
unknown = EXCLUDE
new_password = fields.Str(
required=True,
validate=validate.Length(min=6, max=200),
error_messages={"required": "New password is required"}
)
password_confirm = fields.Str(
required=True,
validate=validate.Length(min=6, max=200),
error_messages={"required": "Password confirmation is required"}
)

126
app/schemas/customer.py Normal file
View File

@@ -0,0 +1,126 @@
from marshmallow import Schema, fields, validate, EXCLUDE
class CreateCustomerSchema(Schema):
"""Validation schema for creating a new customer"""
class Meta:
unknown = EXCLUDE
customer_last_name = fields.Str(
required=True,
validate=validate.Length(min=1, max=250),
error_messages={"required": "Last name is required"}
)
customer_first_name = fields.Str(
required=True,
validate=validate.Length(min=1, max=250),
error_messages={"required": "First name is required"}
)
customer_town = fields.Str(
required=True,
validate=validate.Length(min=1, max=140),
error_messages={"required": "Town is required"}
)
customer_state = fields.Int(
required=True,
validate=validate.Range(min=0, max=50),
error_messages={"required": "State is required"}
)
customer_zip = fields.Str(
required=True,
validate=validate.Length(min=5, max=10),
error_messages={"required": "Zip code is required"}
)
customer_email = fields.Email(
allow_none=True,
load_default=None
)
customer_home_type = fields.Int(
required=True,
validate=validate.Range(min=0, max=10),
error_messages={"required": "Home type is required"}
)
customer_phone_number = fields.Str(
allow_none=True,
validate=validate.Length(max=25),
load_default=None
)
customer_address = fields.Str(
required=True,
validate=validate.Length(min=1, max=1000),
error_messages={"required": "Address is required"}
)
customer_apt = fields.Str(
allow_none=True,
validate=validate.Length(max=140),
load_default=None
)
customer_description = fields.Str(
allow_none=True,
validate=validate.Length(max=2000),
load_default=None
)
class UpdateCustomerSchema(Schema):
"""Validation schema for updating an existing customer"""
class Meta:
unknown = EXCLUDE
customer_last_name = fields.Str(
validate=validate.Length(min=1, max=250)
)
customer_first_name = fields.Str(
validate=validate.Length(min=1, max=250)
)
customer_town = fields.Str(
validate=validate.Length(min=1, max=140)
)
customer_state = fields.Int(
validate=validate.Range(min=0, max=50)
)
customer_zip = fields.Str(
validate=validate.Length(min=5, max=10)
)
customer_email = fields.Email(
allow_none=True
)
customer_home_type = fields.Int(
validate=validate.Range(min=0, max=10)
)
customer_phone_number = fields.Str(
allow_none=True,
validate=validate.Length(max=25)
)
customer_address = fields.Str(
validate=validate.Length(min=1, max=1000)
)
customer_apt = fields.Str(
allow_none=True,
validate=validate.Length(max=140)
)
customer_automatic = fields.Int(
validate=validate.Range(min=0, max=10)
)
customer_description = fields.Str(
allow_none=True,
validate=validate.Length(max=2000)
)
customer_fill_location = fields.Int(
allow_none=True,
validate=validate.Range(min=0, max=10)
)
class UpdateDescriptionSchema(Schema):
"""Validation schema for updating customer description"""
class Meta:
unknown = EXCLUDE
description = fields.Str(
allow_none=True,
validate=validate.Length(max=2000)
)
fill_location = fields.Int(
allow_none=True
)

56
app/schemas/utils.py Normal file
View File

@@ -0,0 +1,56 @@
from flask import jsonify, request
from functools import wraps
from marshmallow import ValidationError
def validate_request(schema_class):
"""
Decorator to validate incoming JSON request data against a marshmallow schema.
Usage:
@customer.route("/create", methods=["POST"])
@validate_request(CreateCustomerSchema)
def create_customer():
data = request.validated_data # Access validated data
...
"""
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
# Check if request has JSON data
if not request.is_json:
return jsonify({"error": "Request must be JSON"}), 400
json_data = request.get_json()
if json_data is None:
return jsonify({"error": "Invalid JSON data"}), 400
# Validate the data
schema = schema_class()
try:
validated_data = schema.load(json_data)
# Attach validated data to request object for easy access
request.validated_data = validated_data
except ValidationError as err:
return jsonify({"error": "Validation failed", "details": err.messages}), 400
return f(*args, **kwargs)
return decorated_function
return decorator
def validate_json(schema_class, data):
"""
Validate data against a schema and return (validated_data, errors).
Usage:
data, errors = validate_json(CreateCustomerSchema, request.get_json())
if errors:
return jsonify({"error": "Validation failed", "details": errors}), 400
"""
schema = schema_class()
try:
validated_data = schema.load(data or {})
return validated_data, None
except ValidationError as err:
return None, err.messages