Skip to content

Latest commit

 

History

History
104 lines (73 loc) · 5.85 KB

README.md

File metadata and controls

104 lines (73 loc) · 5.85 KB

DriveBC Backend

This page details the steps to set up a local, non-containerized dev environment for our backend Django server.

Using a local python venv is recommended for easier and faster access to server reloads and test modules, as well as direct access to Django management commands through terminal. Verify all changes on the docker container before pushing though.

Alternatively, our Docker images innately supports hot-reloading on code changes. Refer to the IDE - Pycharm section for more info.


Prerequisites

Installing required packages

MacOS:

  1. Homebrew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Git brew install git
  3. Python 3.11 and openssl brew install [email protected] openssl

Linux:

  1. Git sudo apt install git
  2. Python 3.11 and development packages sudo apt-get install python3.11 python3.11-dev libssl-dev libffi-dev

Windows:

  1. Git
  2. Python 3.11

Database and caching

Install PostGIS and Redis in your local environment and edit .env with the right hosts and ports.

If Docker containers are preferred instead, comment out the 'django' service in docker-compose.yml, run docker-compose up in the root folder, and connect to containers mapped to the host machine via localhost:5432/localhost:6379.

IDE - PyCharm

This section details setup for PyCharm run configurations on local Python virtual environments.

For containerized environments, see documentation on setting up debugging with VS code with python containers.

Installation

Install PyCharm Community Edition for your OS.

Git checkout

Checkout the repo using git command line or PyCharm VCS options. Open the project with Pycharm at the root folder DriveBC.ca.

Project structure

Set sources root: in the project panel, right click Drivebc.ca/src/backend -> Mark Directory as -> Sources root. This allows PyCharm to resolve imports in the project. Alternatively, go to Pycharm -> Settings -> Project: DrivebC.ca -> Project Structure: pycharm_setup_1

Virtual environment

Set up a virtual environment. Specifying the 'Dependencies' file should install all required packages in the newly created venv. Otherwise, once the venv is created, run pip install -r ./src/backend/requirements/development.txt in the Terminal tab on the bottom-left. pycharm_setup_2

Run configurations and management commands

Set up run configurations for various management commands. Use /.../DriveBC.ca/src/backend/manage.py for Script path, and the command to be ran in Parameters i.e. runserver: pycharm_setup_3

We can now run/debug the server with breakpoints using the toolbar on the top-right: pycharm_setup_4

Useful management commands to add as run configs. Note that these commands can also be ran manually in console: python manage.py runserver

runserver - Starts server at localhost:8000

sandbox - Runs custom management command under shared/management/commands/sandbox.py. Any other custom management commands can be added and ran the same way.

test apps --keepdb - Runs all test modules. Use apps.app_name or a full module path to run tests specific to an app or module.

The --keepdb option uses an existing test database if it already exists and saves time, but we will need to remove this option when there is a new migration to be applied.

run_huey - starts huey worker instance and run all registered tasks on start.

makemigrations - creates db migrations, see Django documentation

migrate - executes db migrations, see Django documentation

Test coverage and report

In the terminal tab on the bottom left:

  1. Switch to the backend folder cd src/backend/
  2. coverage run --source='.' manage.py test apps --keepdb && coverage report