Management Commands & Utility Scripts
Relevant source files
The following files were used as context for generating this wiki page: - `seed_product_types.py` - `reset_migrations.py` - `reset_production_db.py` - `create_test_user.py` - `check_stock.py` - `pbp-deploy.sh` - `IMPLEMENTATION.md` - `README.md`The becathlon project includes several standalone Python scripts and a shell script located in the root directory. These are not integrated as Django management commands (i.e., run via python manage.py <command>) but are executed directly (e.g., python seed_product_types.py). They serve critical functions for database seeding, maintenance, local development setup, and deployment automation.
These scripts are essential for managing the application's lifecycle, from initializing the database with necessary data to resetting it for testing or redeployment. They directly interact with the Django ORM by setting up the Django environment, allowing them to manipulate models and data.
Scripts Overview
This table summarizes the key utility scripts found in the project's root directory.
| Script Name | Purpose | Key Dependencies |
|---|---|---|
seed_product_types.py | Populates the ProductType table with a predefined list of sport categories. | apps.main.models.ProductType |
create_test_user.py | Creates a superuser with a default username and password for testing/development. | django.contrib.auth.models.User |
reset_migrations.py | Deletes all migration files within the apps directory for a clean migration history. | os, shutil |
reset_production_db.py | DANGEROUS: Wipes data from key tables in the production database and reseeds it. | seed_product_types, seed_stores |
check_stock.py | A simple reporting tool to list the stock count for all products. | apps.main.models.Product |
pbp-deploy.sh | Automates the deployment process, including static file collection and database migrations. | manage.py collectstatic, manage.py migrate |
Sources: seed_product_types.py, create_test_user.py, reset_migrations.py, reset_production_db.py, check_stock.py, pbp-deploy.sh
Database Seeding
seed_product_types.py
This script is responsible for populating the ProductType model with an initial set of 12 sport categories. It ensures that the application has a base set of categories to assign to products. The script is idempotent; it checks if a category already exists before creating it, preventing duplicates.
The following diagram illustrates the script's logic for each product type.
Sources: seed_product_types.py:10-20
The script seeds the database with the following product types:
| Product Type Name |
|---|
| Running |
| Cycling |
| Swimming |
| Fitness |
| Team Sports |
| Racket Sports |
| Hiking |
| Camping |
| Water Sports |
| Winter Sports |
| Yoga |
| Boxing |
Sources: seed_product_types.py:11-12, IMPLEMENTATION.md:50-63
Development & Maintenance Utilities
create_test_user.py
A utility for quickly setting up a test administrator account. It creates a superuser with a hardcoded username (testuser), email, and password. This is useful for local development and automated testing environments.
The script performs the following actions:
- Checks if a user with the username
testuseralready exists. - If the user does not exist, it creates a new superuser with the credentials
testuser/testpassword. - If the user already exists, it prints a message and exits.
Sources: create_test_user.py:10-21
reset_migrations.py
This script is a development utility for cleaning up migration files. It recursively searches through the apps directory and deletes any migrations subdirectories it finds. This is typically used during early development to reset the migration history when there are conflicting or broken migrations.
Warning: This is a destructive action and should only be used in a local development environment. It does not affect the database itself, only the migration files on the filesystem.
The process is as follows:
- Iterate through all items in the
appsdirectory. - For each item, check if it's a directory.
- If it is, look for a
migrationssubdirectory within it. - If
migrationsexists, delete the entire directory and its contents.
Sources: reset_migrations.py:9-24
check_stock.py
A simple reporting script that connects to the Django database and prints the name and stock level of every product in the Product table. This can be used for a quick manual audit of inventory levels.
Sources: check_stock.py:10-14
reset_production_db.py
This is a highly destructive script intended to completely reset the production database. It deletes all records from several core application tables and then reseeds the ProductType and Store tables.
EXTREME WARNING: Running this script will result in irreversible data loss in the target database. It should be used with extreme caution and only when a complete data reset is intended.
The script executes the following steps:
- Connects to the database configured via environment variables.
- Deletes all records from the
Product,ProductType,Store,Cart,CartItem,Order, andOrderItemmodels. - Executes
seed_product_types.pyto repopulate the product categories. - Executes
seed_stores.pyto repopulate store locations (Note:seed_stores.pywas not provided in the source files for analysis but is referenced by this script).
The following diagram shows the destructive and reseeding flow of this script.
Sources: reset_production_db.py:12-32
Deployment
pbp-deploy.sh
This shell script automates the deployment process for the PWS (Proyek Perangkat Lunak) environment. It is designed to be run automatically during deployment.
The script performs the following sequence of actions:
- Exits on Error: The
set -ecommand ensures that the script will exit immediately if any command fails. - Create Directories: Creates temporary directories for static and media files (
/tmp/staticfiles,/tmp/media). - Collect Static Files: Runs
python manage.py collectstatic --noinput --clearto gather all static files into theSTATIC_ROOTdirectory. The--clearoption removes existing files before collecting new ones. - Run Migrations: Runs
python manage.py migrate --noinputto apply any pending database migrations.
This flow ensures the application's static assets and database schema are up-to-date upon deployment.
Sources: pbp-deploy.sh