The structure of the whole Django project is built for you. Run the following commands in order to have your local environment up and running.
$ mkvirtualenv -p $(which python3) ecommerce
$ pip install -r requirements.txt
You can now run the development server and point the browser to the correct URL:
$ make runserver
The main goal of this project is to practice all the concepts related with APIs and Django's Class Based Views.
We'll make use of the Ecommerce project that we've been working on, and implement all the endpoints related with the Product
model.
You'll be in charged of implementing an API using Django's Class Based Views (CBV).
For this task, views must be implemented inside api/views.py
and urls inside api/urls.py
. We already provide a function called serialize_product_as_json
, located in api/serializers.py
, that serializes a Product model into a JSON document. You should use this function to format most of the API responses.
A couple of Product objects are already loaded in your database, so you can test the endpoints directly from the browser when you point to the proper URL.
The following endpoints must be supported:
GET /api/products/
Example response:
[
{
"id": 1,
"name": "Nike Vapor",
"sku": "44444444",
"category": 4,
"description": "",
"price": "129.99",
"created": "2018-12-22T17:01:08.683537Z",
"featured": true
},
{
"id": 2,
"name": "Nike Cap",
"sku": "33333333",
"category": 2,
"description": "",
"price": "27.99",
"created": "2018-12-22T17:01:08.684956Z",
"featured": true
}
]
GET /api/products/<product_id>
Example response:
{
"id": 1,
"name": "Nike Vapor",
"sku": "44444444",
"category": 4,
"description": "",
"price": "129.99",
"created": "2018-12-22T17:01:08.683537Z",
"featured": true
}
POST /api/products/
Example payload
{
"name": "New product",
"category": 2,
"sku": "11111111",
"description": "New product description",
"price": 39.99
}
PUT /api/products/<product_id>
Example payload
{
"name": "Updated name",
"category": 1,
"sku": "11111111",
"description": "Updated description",
"price": 39.99
}
PATCH /api/products/<product_id>
Example payload
{
"name": "Updated name"
}
DELETE /api/products/<product_id>
In order to check if your implementation is correct, a couple of tests that should pass are implemented inside api/tests.py
. You can run them this way:
$ make test