Overview
The Python Flask service is a RESTful API microservice that handles core e-commerce functionality including user authentication, product management, order processing, and payment calculations. It uses Flask with SQLAlchemy ORM and PostgreSQL for production environments.Technology Stack
- Framework: Flask 2.3+
- ORM: SQLAlchemy 3.1
- Database: PostgreSQL (SQLite for testing)
- Authentication: JWT (Flask-JWT-Extended)
- Password Hashing: bcrypt
- CORS: Flask-CORS
- Testing: pytest with pytest-flask
Setup and Installation
Project Structure
Configuration
The service supports multiple environments configured inapp/config.py:1:
app/config.py
FLASK_ENV environment variable in app/config.py:61.
Database Models
User Model
Defined inapp/models/user.py:5:
app/models/user.py
Product Model
Defined inapp/models/product.py:5:
app/models/product.py
Order Model
Defined inapp/models/order.py:5:
app/models/order.py
OrderItem Model
Defined inapp/models/order.py:39:
app/models/order.py
API Routes
Authentication Routes
Registered at/api/auth in app/__init__.py:47:
POST /api/auth/register
Register a new user with email, password, and name inapp/routes/auth.py:11:
app/routes/auth.py
POST /api/auth/login
Authenticate user and return JWT token inapp/routes/auth.py:42:
app/routes/auth.py
GET /api/auth/me
Get current authenticated user (requires JWT) inapp/routes/auth.py:69.
Product Routes
Registered at/api/products in app/__init__.py:48:
GET /api/products
List products with pagination and optional category filter inapp/routes/products.py:10:
app/routes/products.py
GET /api/products/:id
Get a single product by ID inapp/routes/products.py:33.
POST /api/products
Create a new product (requires JWT) inapp/routes/products.py:66.
GET /api/products/search
Search products by name or description inapp/routes/products.py:42.
Order Routes
Registered at/api/orders in app/__init__.py:49:
GET /api/orders
List all orders for the authenticated user inapp/routes/orders.py:11.
GET /api/orders/:id
Get a specific order with items inapp/routes/orders.py:35.
POST /api/orders
Create a new order inapp/routes/orders.py:47:
app/routes/orders.py
Payment Routes
Registered at/api/payments in app/__init__.py:50:
POST /api/payments/calculate
Calculate total with tax and discounts inapp/routes/payments.py:11.
POST /api/payments/checkout
Process payment for an order inapp/routes/payments.py:42.
Testing
The service uses pytest with fixtures for comprehensive testing.Running Tests
Test Fixtures
Defined intests/conftest.py:9:
tests/conftest.py
Example Test
Fromtests/test_auth.py:9:
tests/test_auth.py
Application Factory
The service uses the application factory pattern inapp/__init__.py:26:
app/__init__.py
Dependencies
Fromrequirements.txt:
requirements.txt