Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic qrcode generation via frontend #1143

Merged
merged 20 commits into from
Jan 30, 2024
Merged

Conversation

spwoodcock
Copy link
Member

@spwoodcock spwoodcock commented Jan 28, 2024

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • πŸ“¦ Chore (Release)
  • ⏩ Revert

Related Issue

Fixes #736
Partially fixes #966
Partially fixes #1077

Describe this PR

Current Setup

Lack of dynamic fields in QR code:

  • We generate QR codes in bulk when a project is created, then save to the DB.
  • The issue is that the QR code wants to contain dynamic fields, such as the user who is completing the task.

Other:

  • Unencrypted ODK credentials.
  • Inefficient calling of tasks-list every time the task boundary is clicked.

New Setup

Dynamic QR codes:

  • I moved the QR code generation to the frontend.
  • The backend generates the ODK Central appuser token, and stores it encrypted in the database.
  • This token is returned to the frontend from the tasks-list endpoint.
  • The ODK Collect JSON is generated via the frontend, base64 encoded, then embedded in a QR code.
    • The JSON also includes field metadata_username where the OSM username is included.
    • I also misuse the metadata_email field to include the task id from FMTM, to hopefully be passed through to submissions.

Other:

  • I added default ODK credential fields (encrypted) to the organisations table (which we can use later to solve Map ODK Central Credentials with organization nameΒ #966).
  • I updated the project ODK credentials to be encrypted by default too.
  • QR code generation on the frontend is very performant.
  • Updated odk password to SecretStr type, so defaults to obfuscated ****. To extract the actual value, use .get_secret_value()
  • Updated fmtm-splitter to the first stable major release: v1.0.0.

Screenshots

image

Important Considerations

  • Relies on a new release from osm-fieldwork
  • This is a big change, and hence has some migrations for the database.
  • However, to preserve QR codes from existing test data, and to encrypt the ODK credentials prior to usage, I added a script that can do this.
  • It must be ran prior to applying the migrations.
    • The script is under migrations/qrcode_to_odktoken.py.
  • To use:
  1. Pull the latest code or checkout to feat/frontend-qr-codes
  2. Enter the database container:
docker compose exec fmtm-db bash

# Apply required migrations
psql -U fmtm
ALTER TABLE IF EXISTS public.tasks
    ADD COLUMN IF NOT EXISTS odk_token VARCHAR;
exit
  1. Install extra dependencies in the api container:
docker compose exec -u root api bash
apt update && apt install libzbar0 --no-install-recommends
exit
docker compose exec api bash
pip install pillow pyzbar cryptography
# Generate a encryption key (save this for later)
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
exit
  1. Add the variable ENCRYPTION_KEY=xxx to your .env file, using the value generated above.
  2. Restart the api container with your .env set: docker compose up -d api
  3. Run the script:
docker cp migrations/qrcode_to_odktoken.py fmtm-api-1:/opt/app/
docker compose exec api bash
cd app
python qrcode_to_odktoken.py --apply

Checklist before requesting a review

[optional] What gif best describes this PR or how it makes you feel?

@spwoodcock spwoodcock self-assigned this Jan 28, 2024
@github-actions github-actions bot added frontend Related to frontend code backend Related to backend code devops Related to deployment or configuration migration Contains a DB migration labels Jan 28, 2024
@spwoodcock spwoodcock marked this pull request as ready for review January 30, 2024 13:39
@spwoodcock spwoodcock merged commit 814db0d into development Jan 30, 2024
6 checks passed
@spwoodcock spwoodcock deleted the feat/frontend-qr-codes branch January 30, 2024 14:24
@nrjadkry
Copy link
Member

nrjadkry commented Feb 1, 2024

I am getting such error when I scan the QR Code from odk collect.
QR Code does not contain valid settings

@spwoodcock , I think we needed to compress the json using zlib before encoding the result into base64.
I guess, this is missing in the frontend ??

@spwoodcock
Copy link
Member Author

Thanks @nrjadkry #1159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code devops Related to deployment or configuration frontend Related to frontend code migration Contains a DB migration
Projects
None yet
3 participants