acs / podirect

A sample purcharse order management system
Apache License 2.0
0 stars 0 forks source link

POD Backend with Django #2

Open acs opened 5 years ago

acs commented 5 years ago

The Django backend will use the ORM first to model the data based on SKUs. A PO is a list of SKUs. The key functionality is implementing ACID over SKUs/lists of SKUs (lines sheet). A viewer/editor for the SKUs will be used by the sellers to define their offerings. And the sellers will build the POs based on those SKUs. This interaction with POD will be done using a web browser app implemented using React and Reduct #3

A REST API will be also offered using Django REST API.

acs commented 5 years ago

As always, let's follow the steps from the Django tutorial to create the app skeleton: https://docs.djangoproject.com/es/2.2/intro/tutorial01/

(venv) acastillo@acastillo:~/devel/podirect$ pip install django
...
Successfully installed django-2.2.3 pytz-2019.1 sqlparse-0.3.0
(venv) acastillo@acastillo:~/devel/podirect$ python -m django --version
2.2.3
(venv) acastillo@acastillo:~/devel/podirect$ django-admin startproject django_pod
(venv) acastillo@acastillo:~/devel/podirect$ python django_pod/manage.py runserver

Ok, the basic app is working. The next step is to define the data model. And the create a REST API to access the business layer (in the first step it is just a data layer with the minimal logic to manage the data, ACID operations).

Let's create the app pod which will be deployed inside the project django_pod.

(venv) acastillo@acastillo:~/devel/podirect/django_pod$ django-admin startapp pod

Once the initial view is created, to start working in the data model, the initial step is:

(venv) acastillo@acastillo:~/devel/podirect$ python django_pod/manage.py migrate

and the sqlite datbase is created.

Now let's create a basic model for POD:

from django.db import models

class Brand(models.Model):
    name = models.CharField(max_length=200)
    contact = models.CharField(max_length=200)

class SKU(models.Model):
    name = models.CharField(max_length=200)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)

class Retailer(models.Model):
    name = models.CharField(max_length=200)
    contact = models.CharField(max_length=200)

class PurchaseOrder(models.Model):
    buyer = models.ForeignKey(Retailer, on_delete=models.CASCADE)
    order = models.ManyToManyField(SKU)

and now we need to create the support in the database for it with, we add the new app pod to the project config:

podirect/django_pod/django_pod/settings.py
INSTALLED_APPS = [
....
    'pod',
]

and create the data schema for the new application:

(venv) acastillo@acastillo:~/devel/podirect$ python django_pod/manage.py makemigrations pod
Migrations for 'pod':
  django_pod/pod/migrations/0001_initial.py
    - Create model Brand
    - Create model Retailer
    - Create model SKU
    - Create model PurchaseOrder

Now the can use the admin interface to manage the database until we have a better way with our viewers/editors and/or REST API (or graphql).

To activate the admin interface for the pod application:

podirect/django_pod/pod/admin.py

from django.contrib import admin
from .models import SKU, Brand, Retailer, PurchaseOrder
admin.site.register(SKU)
admin.site.register(Brand)
admin.site.register(Retailer)
admin.site.register(PurchaseOrder)

To use the admin interface remember to create the admin user:

(venv) acastillo@acastillo:~/devel/podirect$ python django_pod/manage.py createsuperuser

acs commented 5 years ago

As the idea is to modify the data from the front implemented in react+redux the next step is to offer a REST API so the front can reach the backed. For that we use the Django REST Framework.

acs commented 5 years ago

In order to create the API REST in a quick way, let's follow the Django REST Framework tutorial.

pip install djangorestframework

https://www.django-rest-framework.org/#example

This is a PoC so let's read and write to anon users to avoid investing time in auth issues.

All working now with the commit: https://github.com/acs/podirect/commit/ee7e7ec2fc0b3b799ff1e203ec5be9cbec187caa

To add a new SKU, first we need its brand added:

curl -H "Content-Type: application/json" -XPOST localhost:8000/pod/api/brand/ -d '{"name":"b1", "contact":"me"}'

and then:

curl -H "Content-Type: application/json" -XPOST localhost:8000/pod/api/sku/ -d '{"name":"sku2", "brand":"http://localhost:8000/pod/api/brand/1/"}'

Also, you can quickly access the data using the web browser API REST browser:

http://localhost:8000/pod/api/

Ok, all is ready to start interacting with the API REST from the front!