Getting Started with Development

This guide will help you set up your development environment and get started with contributing to the NSGG Backend project.

Prerequisites

Before you begin, ensure you have the following installed:

  • Python 3.12 or higher
  • PostgreSQL 15 or higher
  • Redis 7 or higher
  • Docker and Docker Compose (optional, but recommended)
  • Git

Setting Up the Development Environment

1. Clone the Repository

git clone https://github.com/yourusername/nsgg-backend.git
cd nsgg-backend

2. Create a Virtual Environment

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

3. Install Dependencies

pip install -r requirements/development.txt

4. Set Up Environment Variables

Copy the example environment file and update it with your settings:

cp .env.example .env

Required environment variables:

DEBUG=True
SECRET_KEY=your-secret-key
DATABASE_URL=postgres://user:password@localhost:5432/nsgg
REDIS_URL=redis://localhost:6379/0
STRIPE_PUBLIC_KEY=your-stripe-public-key
STRIPE_SECRET_KEY=your-stripe-secret-key

5. Initialize the Database

python manage.py migrate
python manage.py createsuperuser

6. Run Pre-commit Hooks

Install and run pre-commit hooks to ensure code quality:

pre-commit install
pre-commit run --all-files

Development with Docker

1. Build and Start Services

docker-compose up -d

2. Run Migrations

docker-compose exec web python manage.py migrate

3. Create Superuser

docker-compose exec web python manage.py createsuperuser

Running Tests

Running All Tests

pytest

Running Specific Tests

pytest path/to/test_file.py
pytest path/to/test_file.py::TestClass::test_function

Test Coverage

pytest --cov=.

Code Quality Tools

Running Linters

# Run flake8
flake8

# Run isort
isort .

# Run black
black .

# Run mypy
mypy .

API Documentation

The API documentation is available at:

  • Swagger UI: http://localhost:8000/api/docs/
  • ReDoc: http://localhost:8000/api/redoc/

Development Workflow

  1. Create a new branch for your feature or bugfix:

    git checkout -b feature/your-feature-name
    

  2. Make your changes and commit them:

    git add .
    git commit -m "feat: add your feature description"
    

  3. Push your changes:

    git push origin feature/your-feature-name
    

  4. Create a pull request on GitHub

Common Development Tasks

Creating a New App

python manage.py startapp new_app_name

Making Model Changes

python manage.py makemigrations
python manage.py migrate

Creating a Superuser

python manage.py createsuperuser

Generating API Schema

python manage.py spectacular --file schema.yml

Troubleshooting

Database Issues

Reset your database:

python manage.py reset_db
python manage.py migrate

Cache Issues

Clear Redis cache:

redis-cli flushall

Docker Issues

Reset Docker environment:

docker-compose down -v
docker-compose up -d

Additional Resources