Skip to content

About

No description, website, or topics provided.

Resources

Stars

13 stars

Watchers

7 watching

Forks

Latest commit

 

History

9,045 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Connect

Connect is a Django + PostGIS web application: organizations create paid opportunities, community health workers learn and deliver services through CommCare apps, and Connect tracks their visits, verifies them and records payments. It integrates with CommCare HQ and ConnectID.

Ask DeepWiki

The developer docs site publishes this README together with the app and deployment docs.

Local setup

This assumes you will use the docker compose file in this repo to run services. If that is not the case you may need to edit some settings.

Python environment

Install uv to manage Python versions and dependencies:

curl -LsSf https://astral.sh/uv/install.sh | sh

Create a virtual environment in .venv and install the requirements:

uv sync

Activate it (or prefix commands with uv run):

source .venv/bin/activate

Install the git hooks and run them once:

prek install
prek run -a

Create your env file and edit the settings as needed (or export settings directly):

cp .env_template .env

GeoDjango / PostGIS

Connect uses GeoDjango and PostGIS. To learn more about the required dependencies, see the Django documentation.

For macOS (Intel / Apple Silicon) – using Homebrew:

brew install gdal geos proj

Django may struggle to find the GDAL and GEOS libraries even after they are installed. Get your local library paths:

ls $(brew --prefix gdal)/lib/libgdal.dylib
ls $(brew --prefix geos)/lib/libgeos_c.dylib

Then set them in your .env file:

GDAL_LIBRARY_PATH=...
GEOS_LIBRARY_PATH=...

Install WeasyPrint:

brew install weasyprint

For Ubuntu / Debian Linux:

sudo apt-get install -y binutils libproj-dev gdal-bin

Enabling the PostGIS extension

No migration creates the postgis extension. The inv up setup works because the postgis/postgis image enables it for you when it initialises the database.

If you are using your own PostgreSQL instead, create the extension in the target database before migrating, or the first spatial migration will fail:

psql -d commcare_connect -c "CREATE EXTENSION IF NOT EXISTS postgis;"

Running the app

Start the docker services (PostgreSQL/PostGIS and Redis):

inv up

Install the JS dependencies and build them (add -w to rebuild on changes):

npm ci
inv build-js

Migrate the database and start Django:

./manage.py migrate
./manage.py runserver

Working in git worktrees

To work on several branches at once, give each one its own git worktree. The repository has hooks for worktrunk (wt) in .config/wt.toml.

Set up the main checkout first (above): worktrees copy its .env, and the docker compose services must be running.

Create a worktree

By default, a worktree gets its own empty database and its own Redis database:

wt switch --create my-branch

wt new my-branch does the same; it is a shorter alias defined in .config/wt.toml.

The database has no data in it; create a user with ./manage.py createsuperuser.

To use the main checkout's database and Redis database instead, for example to work with data you already have, pass --share=1:

wt new my-branch --share=1

Setup doesn't run migrate on a shared database. If the branch adds migrations, running them changes the main checkout's schema too, so give that branch its own database instead.

To start Claude Code in the new worktree as soon as it is set up, add --execute claude:

wt switch --create --execute claude my-branch

Remove a worktree

wt remove my-branch

This drops the databases created for the branch. A worktree created with --share=1 has none, so the main checkout's database is left alone.

What setup does

When a worktree is created, scripts/setup-worktree.sh:

  • copies .env from the main checkout, and runs uv sync and npm ci
  • creates an empty PostgreSQL database for the branch, connect_wt_<branch>_<hash>, and runs migrate
  • picks a Redis database (1-14) that no other worktree's .env uses, and points REDIS_URL and CELERY_BROKER_URL at it, so the branch's Celery worker never runs another branch's tasks
  • comments out SECONDARY_DATABASE_URL, which would otherwise point at the main checkout's replica

With --share=1, it skips the database and Redis steps and copies DATABASE_URL, REDIS_URL and CELERY_BROKER_URL from the main checkout's .env instead. Either way, the JS and CSS then build in the background.

Local development

Useful commands

tasks.py defines invoke tasks for common jobs. List them with:

inv -l

To add a Python dependency, use uv add <pkg>; see Dependency management for the details.

Setting up your users

You don't need the CommCare HQ social app to create local users. The "Log in with CommCare HQ" button stays hidden until a commcarehq social app exists (see Setting up auth with CommCare HQ).

To create a superuser, run the command below. --email is not accepted, because the custom User model sets REQUIRED_FIELDS = []; set the address afterwards in the admin if you need one.

./manage.py createsuperuser

To create a normal user account, go to Sign Up and fill out the form. Local settings use ACCOUNT_EMAIL_VERIFICATION = "optional", so you are logged straight in; the confirmation email is still printed to the console if you want to verify the address.

To promote an existing user to superuser:

./manage.py promote_user_to_superuser <email>

For convenience, you can keep your normal user logged in on Chrome and your superuser logged in on Firefox (or similar), so that you can see how the site behaves for both kinds of users.

Sample data

To build local data that hangs together as if the app had been used, run a scenario. It drives the app's real forms, functions and APIs (with CommCare HQ, ConnectID and Twilio stood in for), so validation, visit flags, payment accrual and invoicing all run:

./manage.py run_scenario payments --admin-email you@example.com

--admin-email must belong to an existing local user, who becomes admin of every organization the scenario creates. The command doesn't create one, since that means picking a password; if you have no user yet, create one with createsuperuser (above). Rerunning a scenario first deletes the organizations it built. The command prints the pages worth opening. The scenarios and options are described in commcare_connect/simulation/README.md.

Volume-only data

generate_sample_data writes rows directly with factories, so it does not go through the app's rules and its records don't hang together. Use it only when you need volume, for example to look at report performance (generate_sample_data <num_visits> <org_slug>):

./manage.py generate_sample_data 50 demo-org

The organization is created if the slug does not already exist. Each run first deletes the existing opportunities, visits, payments, programs and attachments belonging to the two organizations it is about to use, including the attachment files under MEDIA_ROOT. Optional flags:

  • --invited_org_slug — reuse a specific organization as the invited org. Without it, a new invited org with a random slug is created on every run, and the previous one is left behind.
  • --pm_opportunities — how many opportunities the invited org delivers under the primary org's programs (default 3)
  • --image_percent — percentage of visits that get a photo (default 0, none)
  • --audio_percent — percentage of visits that get one to three audio recordings, each with a stand-in transcript and translation (default 0, none)
  • --media_dir — a directory to search (recursively) for image / audio files to use as the attachments for generated visits if requested. Otherwise placeholders are used.

Adding yourself to the generated organizations is a separate step, either through /admin/organization/userorganizationmembership/ or the organization's own member admin pages. Note there are two: the primary org from <org_slug> and the invited org, which delivers those opportunities — join only the first and those stay hidden. Worker tabs list only workers whose OpportunityAccess is accepted, which the generated ones are.

Microplanning, without CommCare HQ

To seed an opportunity with work areas, groups, workers and visits for the Progress Map:

./manage.py generate_microplanning_data --fake-hq

It prints the Progress Map URL and creates the superuser mpadmin with password mpadmin. --help lists the other options.

Assigning, unassigning, excluding and editing an assigned work area all update cases on CommCare HQ. --fake-hq sends those calls to a stand-in for HQ on localhost:8001 and gives each worker an HQ user there. Run the stand-in in its own terminal:

./manage.py run_fake_hq

It answers every case update and prints the request with its body, so you can see what Connect sends. Start it with --fail to answer case updates with an error instead. To see the push notification that assignment sends to ConnectID too, start the Celery worker with CONNECTID_URL=http://localhost:8001/connectid.

Setting up auth with CommCare HQ

Expose your local service to the internet

  • Create an account on ngrok and install ngrok
  • Create a custom domain on ngrok using this link
  • Run ngrok http --url=[my-unique-subdomain].ngrok-free.app 8000
  • Set DJANGO_ALLOWED_HOSTS=[my-unique-subdomain].ngrok-free.app in your .env file

Create an OAuth2 application on CommCare HQ

Setting Value
Client Type Confidential
Authorization Grant Type Authorization Code
Redirect URIs https://[my-unique-subdomain].ngrok-free.app/accounts/commcarehq/login/callback/
Pkce required False

Test the OAuth2 flow

  • Confirm COMMCARE_HQ_URL points at https://staging.commcarehq.org. That is the default, so you only need to set it in .env if you have overridden it.
  • Navigate to http://[my-unique-subdomain].ngrok-free.app/accounts/login/
  • Click the "Log in with CommCare HQ" button
  • You should be redirected to CommCare HQ to log in
  • After logging in, you should be redirected back to the app and logged in

Celery

Local settings run Celery tasks eagerly, inside the request (CELERY_TASK_ALWAYS_EAGER defaults to True in config/settings/local.py), so you don't need a worker. That blocks the request and hides progress UIs, though. To run tasks the way production does, set CELERY_TASK_ALWAYS_EAGER=False in .env and start a worker:

celery -A config.celery_app worker -l info

For Celery's import magic to work, run the commands from the folder that holds manage.py.

To run periodic tasks, you'll need to start the celery beat scheduler service. You can start it as a standalone process:

celery -A config.celery_app beat

or embed the beat service inside a worker with the -B option (not recommended for production use):

celery -A config.celery_app worker -B -l info

Tests

Run the test suite:

pytest

To check your test coverage and generate an HTML coverage report:

coverage run -m pytest
coverage html

Then open htmlcov/index.html.

Live reloading and Sass CSS compilation

inv build-js -w

Deployment

The application runs as Docker containers on EC2, deployed with Kamal, and provisioned with Ansible. See deploy/README.md for deploying to staging and production, server access, logs, Django settings and logical replication.

About

No description, website, or topics provided.

Resources

Stars

13 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages