A REST API developed using the Django framework as a take home assigment for a job interview back in 2019.
- Times in
testers.csv
file are in UTC timezone. - Testers could report a bug on a device that they no longer own
- If a tester doesn't own a device anymore the bugs related to this device don't count towards experience
- Python 3
- Django
- Django REST framework
- drf-yasg - Yet another Swagger generator
- PostgreSQL
- Docker
- Docker Compose
- Nginx
- Gunicorn
Returns list of testers ordered by experience. It can take two query parameters:
- devices
(array[integer])
- devices for which experience should be calculated, empty means all devices - countries
(array[string]
- countries from which testers should be included, empty means any country
Example query: <ip/domain>/match-testers?devices=3&devices=2&countries=GB&countries=JP
Returns list of all available devices
I've defined 3 simple database models:
- Tester
- Device
- Bug
Many to many relation between Testers and Devices is defined using Django's manytomany field which internally creates a database table. Countries are defined as a simple tuple inside the code.
The easiest way to test and explore the API is to use swagger: <ip/domain>/swagger/
.
Data can be imported using custom django-admin command called populate_db
.
You can run it the same way you would run any other django-admin command.
python manage.py populate_db
Script requires the database tables to be empty to avoid collisions and to avoid populating database twice by mistake. If tables are not empty script throws an error.
App requires PostgreSQL database. Because of that the easiest way to run it locally is to use Docker Compose.
First, open terminal in the TeststerMatch directory.
To start the app in a development mode run this commands:
cp db.env.template db.env
cp dev.env.template dev.env
docker-compose up
After docker containers are up, you need to run these two commands in another terminal to setup the database.
docker-compose exec web python manage.py migrate
docker-compose exec web python manage.py populate_db
API url: http://127.0.0.1:8000/swagger/
After starting Docker Compose and running migration command, you can run tests inside Docker using command bellow.
docker-compose exec web python manage.py test
I think it's a good idea to show something more than just a working Django development server. I've created a second docker-compose file which is much closer to an actually deployed app.
It uses Gunicorn to serve the application and Nginx as web server.
To start app in a production mode run this commands:
cp db.env.template db.env
cp prod.env.template prod.env
docker-compose -f docker-compose-prod.yml up
Next two steps are pretty much the same as for the development mode.
docker-compose -f docker-compose-prod.yml exec web python manage.py migrate
docker-compose -f docker-compose-prod.yml exec web python manage.py populate_db
Production mode requires one additional step which setups static files
docker-compose -f docker-compose-prod.yml exec web python manage.py collectstatic
You will need to enter yes
to confirm this command.
API url: http://127.0.0.1:1337/swagger/