LEGO
Open source backend for abakus.no, frontend located at webkom/lego-webapp
LEGO Er Ganske Oppdelt
Getting started
LEGO requires python3.11, docker and uv. Services like Postgres, Redis, Thumbor and Minio run inside docker.
Initial setup (only needed once)
$ git clone [email protected]:webkom/lego.git && cd lego/
$ echo "from .development import *" > lego/settings/local.py
$ docker compose up -d
$ uv run python manage.py initialize_development
uv run creates .venv on first use and re-syncs it against uv.lock, so there is nothing to install up front and nothing to activate.
Run (every time)
$ docker compose up -d
$ uv run python manage.py runserver
Notes
# Note 1: Whenever you switch branches you might need to make minor changes
$ uv run python manage.py migrate # If the branch has a database in another state than yours
# Note 2: When you make changes to models, or constants used by models, you need to create new migrations
$ uv run python manage.py makemigrations # Creates one or more new files that must be commited
# Remember to format generated migrations! (using e.g. `make fixme`)
uv.lock conflicts
If you have updated dependencies it's likely you might get conflicts in the uv lock file. This solution should resolve most conflicts quite well:
$ git rebase origin/master
# If conflicts
$ git checkout --theirs uv.lock
$ uv lock
# The conflicts should be resolved
If you get problems it can be a solution to delete the
.venv, and do a fresh setup
Code Style
This codebase uses the PEP 8 code style. We enforce this with ruff. In addition to the standards outlined in PEP 8, we have a few guidelines (see pyproject.toml for more info):
Format the code with ruff
$ make fixme
To check if it is formatted properly, run:
$ uv run --only-group lint ruff check lego
$ uv run --only-group lint ruff format --check lego
To check if it is typed properly, run:
$ uv run --group mypy --group prod mypy .
Tests
If you want to run a specific test class you can run
$ ./manage.py test lego.apps.[APP]
You can add flags to speed up the tests
By adding the
--keepdbthe next time it will go a lot faster to run the tests multiple times.By adding the
--parallelwill run multiple tests in parallel using multiple cores.
If you want to check your test coverage, you can do the following
# Run all tests in LEGO. Remember to add the recommended flags mentioned above
$ uv run --group coverage coverage run --source=lego ./manage.py test
# If you now have multiple coverage files or a .coverage.* file, you'll have to combine it in order to output report
$ uv run --group coverage coverage combine
# Then you can output the full coverage report
$ uv run --group coverage coverage report
# or a small one that only contains the things you are interested in
$ uv run --group coverage coverage report | grep [some string]
Deployment
LEGO runs in Docker Swarm and deploys are managed by Drone and Ansible.
How to deploy:
- Make sure the changes are pushed to master and the test passes.
- Have you added some new settings in
settings/? If so, make sure theAnsible variablesreflects these changes. - We run migrations automatically, make sure they work!
- Push to the
buildbranch. From master:git push origin master:build - Wait for the
buildbuild to complete. The last step will bedocker build - Go to ci.webkom.dev and use the promote feature to deploy the staging/production build.
Ansible will automatically run the playbook for deploying the new build to staging or production based on the target selected in step 6.
Debugging
### Debugging
If you get an error while installing project dependencies, you might be missing some on your system.
$ apt-get install libpq-dev python3-dev
$ rm -rf .venv && uv sync --python "$(brew --prefix)/bin/python3.11"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
./manage.py load_fixtures --generate