Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Open source backend for, frontend located at webkom/lego-webapp

MIT last commit contributors Build Status codecov

LEGO Er Ganske Oppdelt

Getting started

LEGO requires python3.9, python3.9-venv, docker and docker-compose. Services like Postgres, Redis, Thumbor and Minio run inside docker.

# Initial setup (only need to once)
$ git clone && cd lego/
$ python3 -m venv venv
$ echo "from .development import *" > lego/settings/
$ source venv/bin/activate
$ pip install -r requirements/dev.txt
$ docker-compose up -d
$ python initialize_development

# Activate and run (do every time)
$ docker-compose up -d
$ source venv/bin/activate
$ python runserver

# Note 1: Whenever you switch branches you might need to make minor changes
$ pip install -r requirements/dev.txt # If the branch has changes in the dependencies
$ python 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
$ python makemigrations # Creates one or more new files that must be commited
# Remember to format generated migrations! (using f.ex: make fixme)

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 isort, black & flake8. In addition to the standards outlined in PEP 8, we have a few guidelines (see setup.cfg for more info):

Format the code with black & isort

$ make fixme

To check if it is formatted properly, run:

$ tox -e isort -e flake8 -e black

To check if code is typed properly, run:

$ tox -e mypy


If you want to run a specific test class you can run

$ ./ test lego.apps.[APP]

You can add flags to speed up the tests

By adding the --keepdb the next time it will go a lot faster to run the tests multiple times.

By adding the --parallel will 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
$ tox -e tests --
# or run without tox
$ coverage run --source=lego ./ test

# If you now have multiple coverage files or a .coverage.* file, you'll have to combine it in order to output report
$ coverage combine

# Then you can output the full coverage report
$ coverage report
# or a small one that only contains the things you are interested in
$ coverage report | grep [some string]


Lego runs in Docker Swarm and deploys are managed by Drone and Ansible.

How to deploy:

  1. Make sure the changes are pushed to master and the test passes.
  2. Have you added some new settings in settings/? If so, make sure the Ansible variables reflects these changes.
  3. We run migrations automatically, make sure they work!
  4. Push to the build branch. From master: git push origin master:build
  5. Wait for the build build to complete. The last step will be docker build
  6. Go to 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.

Testing with elasticsearch

Testing with elasticsearch

By default, development and production uses postgres for search. We can still enable elasticsearch backend in prod, so you can test things locally with elasticsearch. In order to do so, you need to run elasticsearch from docker-compose.extra.yml by running docker-compose -f docker-compose.extra.yml up -d. Then you need to run lego with the env variable SEARCH_BACKEND=elasticsearch. You might need to run the migrate_search and rebuild_index commands to get elasticsearch up to date.



If you get an error while installing requirements, you might be missing some dependencies on your system.

$ apt-get install libpq-dev python3-dev

For MACOS you need to brew install postgresql

If you get an error while running initialize_development mentioning elasticsearch, you probably need to run the following code, and then start over from docker-compose up -d. Read why and how to make it permanent on Elasticsearch docs.

$ sysctl -w vm.max_map_count=262144

If you get ld: library not found for -lssl

export LDFLAGS="-L/usr/local/opt/openssl/lib"

When changing the user fixtures one must load new fixtures

./ load_fixtures --generate

An overview of the available users for development can be found in this PR