Environment Configuration
Relevant source files
The following files were used as context for generating this wiki page:The Becathlon project employs a flexible configuration system that adapts to different environments, primarily distinguishing between local development and production deployment. The configuration is managed through environment variables, with sensible defaults provided for a seamless local development experience. The central file for this system is becathlon/settings.py.
This setup allows developers to run the application locally using SQLite and debug mode, while the production environment on PWS (Pusat-sumber-belajar-dan-ilmu-pengetahuan) uses a more robust PostgreSQL database and production-ready security settings. The deployment process is automated via the pbp-deploy.sh script.
Core Configuration
Core application settings are dynamically configured based on environment variables. If these variables are not set, the application falls back to default values suitable for local development.
Sources: becathlon/settings.py:23-32
Key Environment Variables
The following table outlines the main environment variables that control the application's behavior.
| Variable | Purpose | Default / Fallback |
|---|---|---|
SECRET_KEY | Django's secret key for cryptographic signing. | A hardcoded insecure key. |
DEBUG | Toggles Django's debug mode. | 'True' |
ALLOWED_HOSTS | A list of allowed host/domain names. | 'localhost,127.0.0.1,testserver' |
DB_NAME | The name of the PostgreSQL database. | None (triggers SQLite fallback) |
DB_USER | The username for the PostgreSQL database. | None |
DB_PASSWORD | The password for the PostgreSQL database. | None |
DB_HOST | The host for the PostgreSQL database. | None |
DB_PORT | The port for the PostgreSQL database. | '5432' |
SCHEMA | The PostgreSQL schema name. | None |
STATIC_URL | URL to use when referring to static files. | '/static/' |
STATIC_ROOT | The absolute path to the directory where collectstatic will collect static files. | BASE_DIR / 'staticfiles' |
MEDIA_URL | URL that handles media served from MEDIA_ROOT. | '/media/' |
MEDIA_ROOT | Absolute filesystem path to the directory that will hold user-uploaded files. | BASE_DIR / 'media' |
Sources: becathlon/settings.py:26-193
Database Configuration
The application features a dynamic database configuration that switches between PostgreSQL for production and SQLite for local development. This logic is determined by the presence of DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST environment variables.
The following diagram illustrates the database selection logic.
This diagram shows how the application decides which database backend to use based on environment variables.
Sources: becathlon/settings.py:101-133
PostgreSQL Configuration (Production)
When PostgreSQL environment variables are detected, the system configures a connection with the specified credentials. It can also set a specific PostgreSQL schema if the SCHEMA environment variable is provided.
# becathlon/settings.py:109-126
if DB_NAME and DB_USER and DB_PASSWORD and DB_HOST:
# Use PostgreSQL with university-provided credentials
db_config = {
'ENGINE': 'django.db.backends.postgresql',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': DB_PORT or '5432', # Default PostgreSQL port
'ATOMIC_REQUESTS': False, # Allow non-atomic operations for complex migrations
'CONN_MAX_AGE': 0, # Close connections immediately to avoid transaction issues
}
# Add schema search path if specified
if DB_SCHEMA:
db_config['OPTIONS'] = {
'options': f'-c search_path={DB_SCHEMA},public'
}
Sources: becathlon/settings.py:109-126
SQLite Configuration (Development)
If the PostgreSQL environment variables are not set, the application defaults to using a simple db.sqlite3 file in the project's base directory, which is ideal for local development.
# becathlon/settings.py:130-133
# Fallback to SQLite for local development
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Sources: becathlon/settings.py:130-133
Static & Media Files
The project uses WhiteNoise to serve static files efficiently in a production environment. The configuration switches between whitenoise.storage.CompressedStaticFilesStorage and Django's default StaticFilesStorage based on whether whitenoise is installed.
Static File Storage Logic
This diagram shows the conditional configuration of the static files storage backend.
Sources: becathlon/settings.py:194-214
Key Settings
STATIC_URL: The URL prefix for static files, defaulting to/static/.STATIC_ROOT: The directory wheremanage.py collectstaticgathers all static files for deployment. It is set toBASE_DIR / 'staticfiles'.STATICFILES_DIRS: An additional list of directories to find static files. It is configured to include theBASE_DIR / "static"directory if it exists.MEDIA_URL/MEDIA_ROOT: Configured for handling user-uploaded files, stored in themediadirectory.
The pbp-deploy.sh script executes python manage.py collectstatic --noinput --clear to ensure all static assets from different apps are collected into STATIC_ROOT before deployment.
Sources: becathlon/settings.py:188-222, pbp-deploy.sh:12-13
Application Structure (INSTALLED_APPS)
The project is organized into several Django apps, each responsible for a specific feature module.
| App Name | Configuration Class | Purpose |
|---|---|---|
apps.main | MainConfig | Homepage, navigation, and base templates. |
apps.authentication | AuthenticationConfig | User login, registration, and logout. |
apps.catalog | CatalogConfig | Product listings, categories, and details. |
apps.cart | CartConfig | Shopping cart management. |
apps.profiles | ProfilesConfig | User profiles and preferences. |
apps.stores | StoresConfig | Store locator functionality. |
apps.recommendation | RecommendationConfig | Product recommendation system. |
apps.order | OrderConfig | Order history and management. |
This modular structure is defined in INSTALLED_APPS and documented in the README.md.
Sources: becathlon/settings.py:65-76, README.md:92-108
Security
Security settings are hardened for production by checking if DEBUG is False.
Production Security Settings
When DEBUG is False, the following settings are enabled:
SESSION_COOKIE_SECURE = True: Ensures session cookies are only sent over HTTPS.CSRF_COOKIE_SECURE = True: Ensures CSRF cookies are only sent over HTTPS.SECURE_BROWSER_XSS_FILTER = True: Enables the browser's XSS filter.SECURE_CONTENT_TYPE_NOSNIFF = True: Prevents the browser from MIME-sniffing the content type.X_FRAME_OPTIONS = 'DENY': Prevents the site from being rendered in an<iframe>.
Additionally, CSRF_TRUSTED_ORIGINS is configured to allow POST requests from the PWS deployment domains.
# becathlon/settings.py:35-39
CSRF_TRUSTED_ORIGINS = [
'https://muhammad-vegard-becathlon.pbp.cs.ui.ac.id',
'https://pbp.cs.ui.ac.id',
'https://muhammad.vegard.pbp.cs.ui.ac.id',
]
Sources: becathlon/settings.py:35-51
Deployment Process
The deployment to the PWS environment is handled by the pbp-deploy.sh script. This script automates the necessary setup steps for a production environment.
Deployment Flow
The following diagram illustrates the steps executed by the deployment script.
This sequence shows the automated steps for preparing the application for production.
Sources: pbp-deploy.sh
This configuration ensures that the Becathlon application is both easy to develop locally and robust when deployed to a production environment, with clear separation of concerns and automated setup processes.