- export CURRENT_UID=$(id -u):$(id -g)
- export PROJECT_DIR='path/to/project'
- export CODE_FOLDER=categories_api
- Create
docker/docker-compose.yml
based ondocker-compose.yml.sample
. - Create
docker/dockerfiles/uwsgi.Dockerfile
based ondocker/dockerfiles/uwsgi.Dockerfile.sample
. - Create
docker/docker_data/nginx/nginx.conf
based ondocker/docker_data/nginx/nginx.conf.sample
. - Create
docker/docker_data/uwsgi/uwsgi.ini
based ondocker/docker_data/uwsgi/uwsgi.ini.sample
. - Create
categories_api/categories_api/local_settings.py
based oncategories_api/categories_api/local_settings.py.sample
. - docker-compose -f docker/docker-compose.yml build
- docker-compose -f docker/docker-compose.yml up -d
docker-compose -f docker/docker-compose.yml down
cd ./categories_api
mkvirtualenv categories_api -p /usr/bin/python3
workon categories_api
pip install -r requirements.txt
- Create
categories_api/categories_api/local_settings.py
based oncategories_api/categories_api/local_settings.py.sample
and use sqlite3. ./manage.py runserver
py.test -s
Introduction
Create a simple Categories API that stores category tree to database and returns category parents, children and siblings by category id.
Requirements
Use Python 3 and Django Rest Framework. Use of any other third-party libraries is prohibited.
Categories Endpoint
Create POST /categories/ API endpoint. Endpoint should accept json body (see example Request), validate input data (see Request) and save categories to database (category name should be unique).
Example.
Request:
{
"name": "Category 1",
"children": [
{
"name": "Category 1.1",
"children": [
{
"name": "Category 1.1.1",
"children": [
{
"name": "Category 1.1.1.1"
},
{
"name": "Category 1.1.1.2"
},
{
"name": "Category 1.1.1.3"
}
]
},
{
"name": "Category 1.1.2",
"children": [
{
"name": "Category 1.1.2.1"
},
{
"name": "Category 1.1.2.2"
},
{
"name": "Category 1.1.2.3"
}
]
}
]
},
{
"name": "Category 1.2",
"children": [
{
"name": "Category 1.2.1"
},
{
"name": "Category 1.2.2",
"children": [
{
"name": "Category 1.2.2.1"
},
{
"name": "Category 1.2.2.2"
}
]
}
]
}
]
}
Category Endpoint
Create GET /categories// API endpoint. Endpoint should retrieve category name, parents, children and siblings (see examples) by primary key () in json format.
Example 1.
GET /categories/2/
Response:
{
"id": 2,
"name": "Category 1.1",
"parents": [
{
"id": 1,
"name": "Category 1"
}
],
"children": [
{
"id": 3,
"name": "Category 1.1.1"
},
{
"id": 7,
"name": "Category 1.1.2"
}
],
"siblings": [
{
"id": 11,
"name": "Category 1.2"
}
]
}
Example 2.
GET /categories/8/
Response:
{
"id": 8,
"name": "Category 1.1.2.1",
"parents": [
{
"id": 7,
"name": "Category 1.1.2"
},
{
"id": 2,
"name": "Category 1.1"
},
{
"id": 1,
"name": "Category 1"
}
],
"children": [],
"siblings": [
{
"id": 9,
"name": "Category 1.1.2.2"
},
{
"id": 10,
"name": "Category 1.1.2.3"
}
]
}