This backend application is designed for an e-commerce platform that supports user authentication, product management, shopping cart, and order processing. It consists of various modules to manage users, products, carts, and orders. This platform allows users to create accounts, add products to their carts, place orders, and view their order history. The backend is built with Django, Django REST framework (DRF), PyJWT and utilizes a PostgreSQL database.
User Management: Users can register, log in, and manage their profile.Product Management: Admins can add, update, and remove products.Cart Management: Users can add/remove products to/from their cart.Order Management: Users can create and view orders. Admins can also view order data.Security: Authenticated access using JWT tokens.
-
Users:
- Handles user registration, login, and profile management.
- Uses JWT authentication for secure access.
- Admin can also access user records
-
Products:
- Manages product listings, including CRUD (Create, Read, Update, Delete) operations.
- Admin can publish/unpublish products.
-
Cart:
- Users can add/remove products to/from their cart.
- Cart items track the product quantity and price.
-
Orders:
- Users can place orders based on the products in their cart.
- Tracks order items and their quantities.
- Order total is calculated dynamically.
- Admin can access order data
The primary models include:
- User (from Django's AbstractUser)
- Product: Represents items for sale in the store.
- Cart & CartItem: Represents a shopping cart with associated products.
- Order & OrderItem: Represents customer orders and the individual items within those orders.
- UUIDPrimaryKey: All primary keys in this application are UUIDs, ensuring global uniqueness.
- Decimal Fields: Used to represent prices with two decimal places for accuracy.
This project includes a custom management script to aid in project setup and administration.
create_admin.py: This script is used to create a superuser with all privileges. It is useful as it will be the first user in the application, which you can then use to create additional admin users.
- Clone the repository:
git clone https://github.com/faruqt/fashion-store.git
- Create the Environment Variables File:
Create and copy the provided sample file to
.env
cp env.sample .envEdit the .env file to configure your environment-specific variables as explained below.
- Generate a secret key:
Generate a new secret key and update the
APP_SECRET_KEYvariable in your.envfile with it:
python3 -c 'import secrets; print(secrets.token_hex())'-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Database Setup (PostgreSQL) : Execute the following instructions to setup your database locally
- Login to PostgreSQL as a superuser
psql -U postgres
- Create database and user
CREATE DATABASE fashion_store_db; CREATE USER fashion_store_db_user WITH PASSWORD 'fashion_store_db_password';
- Set the user as the database owner
ALTER DATABASE fashion_store_db OWNER TO fashion_store_db_user;- Exit psql
\q- Update your
.envfile accordingly with the required database credentials.
-
Run migrations to set up the database:
python manage.py migrate
-
Create a superuser: Run the following command to create the first superuser with all privileges. This user can be used to create additional admin users.
python scripts/create_admin.py
-
Start the development server:
python manage.py runserver
-
Access the Application: The application will be available at http://127.0.0.1:8000. This URL will serve the application's web interface.
- Build and Run the Docker Image: Ensure Docker and Docker Compose are installed on your machine. Use the following command to build and start the application container:
docker-compose upThis will start the backend service, which includes the main application logic. It will build the Docker image for the backend service (if not already built) and then run it.
- Access the Application: The application will be available at http://127.0.0.1:8000. This URL will serve the application's web interface.
For a production environment, use the following steps to setup the environment:
- Build and Run the Docker Image: In a production environment, you may want to use a different configuration file. Run the following command to build and start the production container:
docker-compose -f docker-compose.prod.yml up-f docker-compose.prod.yml: This flag specifies the use of thedocker-compose.prod.ymlfile, which contains production-specific configurations.
Environment Variables: Make sure to set the appropriate environment variables in your.envfile before running the containers. This file should be configured based on the environment you are working in (development, staging, production).Automatic Reloading: Code changes are automatically reflected due to volume mounts.Service Restart: To restart the service if you make changes to the configuration, run the commands below:
docker-compose down
docker-compose upStopping Containers: To stop the running containers, usedocker-compose down. This will stop and remove the containers but keep the images.
To ensure the quality of the application, we have tests written for each app. The project is set up to use pytest as the testing framework.
- Run Tests: Run the automated test suite. using the following command:
bash run-tests.sh
This will execute a script that automatically discovers and runs all tests in the app folders using pytest.
- Ensure you have Docker Compose installed. If not, follow Docker's installation guide to set it up.
- Adjust Dockerfile or Compose configurations as needed based on your environment and requirements.
- For any issues or contributions, please contact the maintainer.