Purpose — to introduce and familiarise you to the basic tenets of a highly scalable, web-based application (from the POV of UrbanPiper :))
So, what are we building?
• A simple transactional system
• Comprises of:
- Merchants (name)
- Items (name, price, description)
- Stores (name, address, lat, lng)
• Orders/ Transactions:
- Made up of items
- Each order is associated w/ a single store and merchant
- Orders can be placed only when the store, items and order are all associated with the same merchant
Steps:
- Clone the project and change directory into the cloned folder
- Install pyenv link .This will seperate your system's python installation, from what's required by the project. This project required python version
3.6.5
.- Install python version 3.6.5 in pyenv and set it as the local python env
- Install mysqlv5.7 This project uses mysql
5.7
, follow the steps below after installating mysql,mysql_config
must be installed, if this is not installed the next step will failsudo apt-get install libmysqlclient-dev
link to stackoverflow- Create a database called updjango_db in mysql using the mqsql admin
- In mysql-admin, create a new user and grant all the permissions to this db and configure the username and password in
settings.py
- Create a virtual env and Install the dependencies in the requirements.txt file
- Make migrations using
./manage.py makemigrations
, this will generate the migrations - Migrate using
./manage.py migrate
, this will apply the migrations to the database
- Make migrations using
- Installing rabbitmq Install the latest version of rabbitmq
- Rabbitmq depends on erlang and might throw an error during installations because of an incompatable versions. This stackoverflow link to install the correct version of erlang helps link
- Start the rabbitmq server in the background
- Run Migrations Make migrations and migrate the project, create a super user to use the django admin portal
- Start Server Run the server, the server will be running at http://localhost:8000
This project uses JWT for authentication, all requests other than login and request token must contain
The header Authorization
:Bearer TOKEN_FROM_REFRESH_TOKEN_RESPONSE
URL - /api/token/
request body
{
"username":"YOUR_USERNAME",
"password":"YOUR_PASSWORD"
}
A succesful response, contains an Access token that is valid for 5 minutes and a refresh token which can be used to get a token with a validitiy of 24 hours
response body
{
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU4NDkxNzk2OCwianRpIjoiNjNmYmE1ZjBhODcxNDQ3YmIyMjExY2ZmNzkwZTcwN2EiLCJ1c2VyX2lkIjoxfQ.oZ6R8lAUN_ubtGFEPqnCxyNhwUFnoe8vJOitWN-1VpU",
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg0ODMxODY4LCJqdGkiOiJhMzA4ZTk0YzA0MDg0NTA5OGMwNzNjODdmMmEzY2VhOSIsInVzZXJfaWQiOjF9.kETgnqchKS3kS1YQ7hcsrBwb8BT04WEGX8zFaqlYljg"
}
URL - /api/token/refresh/
request body
{
"refresh":"REFRESH_TOKEN_FROM_LOGIN_RESPOSE"
}
A succesful response, contains an Access token that is valid for 24 hours
response body
{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg0ODE3ODkzLCJqdGkiOiI3YmQxYmEwNzMzYWQ0MTBhODkwYmE3YTcwMDE5OGNlMiIsInVzZXJfaWQiOjF9.TBHZWhs8JlggC-97maurqb8Nz-vfwsV7zHmUqmLdUCQ"
}
URL - /api/merchants/
- Returns a list of all merchants
response body
[
{
"id": 1,
"name": "Chai Point",
"owner": 1
}
]
URL - /api/merchants/<int:pk>/
- Returns the merchant detail for a specific merchant
response body
{
"id": 1,
"name": "Chai Point",
"owner": 1
}
URL - /api/merchants/
- Add a new merchant
request body
{
"name": "Kanti sweets",
"owner": 1
}
response body
{
"message": "merchant saved"
}
URL - /api/merchants/<int:pk>/
- Edit a merchant
request body
{
"name": "Kanti sweets",
"owner": 1
}
response body
{
"message": "merchant saved"
}
URL - /api/merchants/<int:pk>/
- delete a merchant
response status - 204 no content
URL - /api/stores/
- Returns a list of all stores
response body
[
{
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
]
URL - /api/store/<int:pk>/
- Returns the store detail for a specific merchant
response body
{
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
URL - /api/stores/
- Add a new store
request body
{
"name": "Cunningham Road",
"address": "Super, cunning",
"lat": 45.9699,
"lng": 24.7319,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
response body
{
"message": "Store saved"
}
URL - /api/merchants/<int:pk>/
- Edit a Store
request body
{
"name": "Cunningham Road",
"address": "Super, cunning",
"lat": 45.9699,
"lng": 24.7319,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
response body
{
"message": "Store edited successfully"
}
URL - /api/stores/<int:pk>/
- delete a store
response status - 204 no content
URL - /api/items/
- Returns a list of all items
Query Params
merchant_id
- Returns a list of all the items for a particular merchant
q
- Search items by name, returns a list
response body
[
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
]
URL - /api/items/<int:pk>/
- Returns the item detail for a specific merchant
response body
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
URL - /api/items/
- Add a new item
request body
{
"name": "Egg Puff",
"price": "25",
"description": "An assortment of vegetables wrapped in a flaky pastry, baked to perfection",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
response body
{
"message": "merchant saved"
}
URL - /api/items/<int:pk>/
- Edit an item
request body
{
"name": "Egg Puff",
"price": "25",
"description": "An assortment of vegetables wrapped in a flaky pastry, baked to perfection",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
response body
{
"message": "merchant saved"
}
URL - /api/items/<int:pk>/
- delete an item
response status - 204 no content
URL - /api/orders/
- Returns a list of all order
response body
[
{
"id": 1,
"address": "Celery shared task 1",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
},
"store": {
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
},
"items": [
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
],
"order_subtotal": 234.0,
"taxes": 123.0,
"order_total": 123.0,
"created_time": "2020-03-21T21:03:22.428609Z",
"delivery_time": "2020-03-21T21:43:16.446742Z"
},
{
"id": 2,
"address": "Celery shared task 1",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
},
"store": {
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
},
"items": [
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
],
"order_subtotal": 234.0,
"taxes": 123.0,
"order_total": 123.0,
"created_time": "2020-03-21T22:08:21.396133Z",
"delivery_time": "2020-03-21T22:52:01.217416Z"
}
]
URL - /api/orders/<int:pk>
- Returns the details for a single order
response body
{
"id": 1,
"address": "Celery shared task 1",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
},
"store": {
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
},
"items": [
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
],
"order_subtotal": 234.0,
"taxes": 123.0,
"order_total": 123.0,
"created_time": "2020-03-21T21:03:22.428609Z",
"delivery_time": "2020-03-21T21:43:16.446742Z"
}
URL - /api/orders/
- Create a new order
request body
{
"address": "Nallurhalli",
"merchant":{
"id": 1,
"name": "Chai Point",
"owner": 1
},
"store": {
"id": 1,
"name": "Nallurhalli",
"address": "Brigade irv center",
"lat": 12.12345,
"lng": 12.21356,
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
},
"items": [
{
"id": 1,
"name": "Vada Pav",
"price": "25",
"description": "Mumbai style",
"merchant": {
"id": 1,
"name": "Chai Point",
"owner": 1
}
}
],
"order_subtotal": 234.0,
"taxes": 123.0,
"order_total": 123.0
}
response body
"message": "Order Queued"