Skip to content

Product Information Management with Django Ninja

License

Notifications You must be signed in to change notification settings

OmarThinks/PIM-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PIM-API:

Minimal Product Information Management with Django-Ninja

Initializiation:

Just to make sure that you environment is set up as mine.
Run these scripts.

virtualenv env --python=python3.7.9
source env/Scripts/activate
pip install -r requirements.txt

Testing:

Run these scripts.

python models_test.py
python pydantic_test.py

There is also a postman collection to test API endpoints.

models.py:

This file contains all the SQLAlchemy models.

pydatic_models.py:

This file contains all the pydantic classes, for validation and sanitization of user inputs.

Used tech stack:

SQLAchemy:

This is the ORM

unittest:

This enables me to run unittesting.

pydantic:

for validation and sanitization of user inputs

dajngo ninja:

To make django look more like FastAPI

Running the API:

./manage.py runserver

http://127.0.0.1:8000/api/

Documentation:

http://127.0.0.1:8000/api/docs

Endpoints:

Make sure that everything is working correctly:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/
2 GET http://127.0.0.1:8000/api/ping

Database endpoints:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/drop_create
2 GET http://127.0.0.1:8000/api/populate

Documentation endpoints:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/docs

Product endpoints:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/products
2 GET http://127.0.0.1:8000/api/products/{id}
3 POST http://127.0.0.1:8000/api/products
4 DELETE http://127.0.0.1:8000/api/products/{id}
5 PUT http://127.0.0.1:8000/api/products/{id}

Category endpoints:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/categories
2 GET http://127.0.0.1:8000/api/categories/{id}
3 POST http://127.0.0.1:8000/api/categories
4 DELETE http://127.0.0.1:8000/api/categories/{id}
5 PUT http://127.0.0.1:8000/api/categories/{id}

ProductCategory endpoints:

Number Method Endpoint
1 GET http://127.0.0.1:8000/api/product/categories
2 GET http://127.0.0.1:8000/api/product/categories/{id}
3 POST http://127.0.0.1:8000/api/product/categories
4 DELETE http://127.0.0.1:8000/api/product/categories/{id}

CRUD Rules:

1) Product:

Name Type Unique Nullable Min Max PrimaryKey ForeignKey
id Integer True False 1 True
name String False False min len = 3 max len = 300 False
price Float False False 0.1 1000000 False
quantity float False False .001 100000000 False
code Integer True True 3 100000000000000000000000000 False
  1. Each product has a unique id
  2. Each product has a name, price and quantity, they are not unique, and they can not be equal to null
  3. name is string
    • Example: "Cheese"
  4. price is float
    • Example: $0.5
  5. quantity is float
    • Example: 0.5 kg
  6. each product has a code, it is integer
  7. code can be equal to null
    • Example: new unregistered product
  8. code must be unique, unless if it was null
    • There can be lots of products with code equal to null
  9. When deleting a Product
    1. catogories will not be affected
    2. all ProductCategory related to this product will be deleted
      • Do not confuse with Category
      • Categories will not be affected

2) Category:

Name Type Unique Nullable Min Max PrimaryKey ForeignKey
id Integer True False 1 True
name String True False min len = 3 max len = 300 False
parent_id Integer False True 1 False Category.id
  1. Each category has a unique id
  2. Each category has a name, parent_id
  3. name is unique, and can not be equal to null
  4. parent_id can be equal to null
  5. each category can only have one parent
  6. each category can unlimited amount of children
  7. When deleting a Category
    1. If it has children, all them will have a null parent
    2. Products will not be affected at all
    3. ProductCategory related to this Category will be deleted
      • Do not confuse Product and ProductCategory
      • Product will not be affected be the deletion

2) ProductCategory:

Name Type Unique Nullable Min Max PrimaryKey ForeignKey
id Integer True False 1 True
category_id Integer False False 1 False Category.id
product_id Integer False False 1 False Product.id
  1. Each ProductCategory has a unique id
  2. Each ProductCategory has a product_id, category_id, they can not be equal to null
  3. the pair of (product_id, category_id) is unique
  4. a ProductCategory will be deleted Automatically in any of those cases
    1. Associated Product is deleted
    2. Associated Category is deleted
  5. When deleting a ProductCategory
    1. Associated Product will not be affected
    2. Associated Category will not be affected

Errors:

Errors are pydantic type of errors, because I use Django ninja

Example:

{
    "detail": [
        {
            "loc": [
                "body",
                "product",
                "name"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "product",
                "price"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "product",
                "quantity"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}

What is a good API:

  • A good a API authenticated and validated that no one can manipulate it and get information he is not supposed to have access to.
  • Secure
  • Tested

About

Product Information Management with Django Ninja

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages