Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
suyog ojha committed Jun 2, 2022
0 parents commit cb53565
Show file tree
Hide file tree
Showing 90 changed files with 750 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2020-2022 Suyog Ojha

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
code: S4nUhruExwA
5 changes: 5 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest_plugins = [
"ecommerce.tests.inventory_fixtures",
"ecommerce.tests.api_client",
"ecommerce.tests.c_client",
]
Binary file added db.sqlite3
Binary file not shown.
Empty file added ecommerce/__init__.py
Empty file.
Binary file added ecommerce/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/settings.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/__pycache__/wsgi.cpython-39.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions ecommerce/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for ecommerce project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings')

application = get_asgi_application()
Empty file added ecommerce/demo/__init__.py
Empty file.
Binary file added ecommerce/demo/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/admin.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/apps.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/demo/__pycache__/models.cpython-39.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions ecommerce/demo/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions ecommerce/demo/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class DemoConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "ecommerce.demo"
13 changes: 13 additions & 0 deletions ecommerce/demo/fixtures/db_admin_fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{ "model": "auth.user",
"pk": 1,
"fields":{
"username": "admin",
"password": "pbkdf2_sha256$260000$KfZ2hspAeA8KhAAJ8mKpjw$60ajS7y4Fw72yg+DzZQADfUsUs4l3MJuhABupfjQ3z0=",
"email": "[email protected]",
"is_superuser": true,
"is_staff": true,
"is_active": true
}
}
]
Binary file not shown.
9 changes: 9 additions & 0 deletions ecommerce/demo/management/commands/demo-fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand


class Command(BaseCommand):
def handle(self, *args, **kwargs):
call_command("makemigrations")
call_command("migrate")
call_command("loaddata", "db_admin_fixture.json")
Empty file.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions ecommerce/demo/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions ecommerce/demo/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions ecommerce/demo/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Binary file added ecommerce/dninja/__pycache__/api.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/dninja/__pycache__/api.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/dninja/__pycache__/schema.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/dninja/__pycache__/schema.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/dninja/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/dninja/__pycache__/urls.cpython-39.pyc
Binary file not shown.
69 changes: 69 additions & 0 deletions ecommerce/dninja/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from django.shortcuts import get_object_or_404
from ninja import NinjaAPI, Form, File
from ninja.files import UploadedFile
from .schema import CategorySchema, ProductSchema
from ecommerce.inventory.models import Category, Product, Media
from typing import List
from django.shortcuts import get_object_or_404

api = NinjaAPI()

@api.post("/inventory/category")
def post_category(request, data: CategorySchema):
qs = Category.objects.create(**data.dict())
return {"name":qs.name}

@api.post("/inventory/product")
def post_product(request, data: ProductSchema):
qs = Product.objects.create(**data.dict())
return {"name": qs.name}

@api.get("inventory/category/all/", response=List[CategorySchema])
def get_category_list(request):
qs = Category.objects.all()
return qs

@api.get("/inventory/products/category/{category_slug}", response=List[ProductSchema])
def get_product_by_category(request, category_slug: str):
qs = Product.objects.filter(category__slug=category_slug)
return qs

@api.put("/inventory/category/{category_id}")
def update_category(request, category_id: int, payload: CategorySchema):
category = get_object_or_404(Category, id=category_id)
for attr, value in payload.dict().items():
if value:
setattr(category, attr, value)
category.save()
return {"Success": True}

@api.delete("/category/{cat_id}")
def delete_cat(request, cat_id: int):
category = get_object_or_404(Category, id=cat_id)
category.delete()
return {"Success": True}

@api.post("inventory/category/form/")
def post_category_form(request, form: CategorySchema = Form(...)):
category = Category.objects.create(**form.dict())
return {"name": category.name}

@api.post("inventory/category/form/params/")
def post_category_form_params(request, name: str = Form(...), slug: str = Form(...)):
category = Category.objects.create(name=name, slug=slug)
return {"name": category.name}

@api.post("/upload/media")
def upload_media(request, prod_id: int = Form(...), file: UploadedFile = File(...)):
product = get_object_or_404(Product, id=prod_id)
media = Media.objects.create(product_inventory=product, img_url=file)
return {"id": media.id}

@api.post("/upload/media/multiple")
def upload_media_multiple(request, prod_id: int = Form(...), files: List[UploadedFile] = File(...)):
product = get_object_or_404(Product, id=prod_id)

for img in files:
Media.objects.create(product_inventory=product, img_url=img)

return {"success": True}
12 changes: 12 additions & 0 deletions ecommerce/dninja/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ninja import Schema, ModelSchema

from ecommerce.inventory.models import Product

class CategorySchema(Schema):
name: str
slug: str

class ProductSchema(ModelSchema):
class Config:
model = Product
model_fields = ["name", "web_id", "category"]
6 changes: 6 additions & 0 deletions ecommerce/dninja/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from ecommerce.dninja.api import api

urlpatterns = [
path("", api.urls)
]
Empty file added ecommerce/inventory/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ecommerce/inventory/__pycache__/admin.cpython-39.pyc
Binary file not shown.
Binary file added ecommerce/inventory/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added ecommerce/inventory/__pycache__/apps.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions ecommerce/inventory/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from . import models


admin.site.register(models.Category)
admin.site.register(models.Product)
admin.site.register(models.Media)
6 changes: 6 additions & 0 deletions ecommerce/inventory/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class InventoryConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "ecommerce.inventory"
60 changes: 60 additions & 0 deletions ecommerce/inventory/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 4.0.3 on 2022-03-15 14:29

from django.db import migrations, models
import django.db.models.deletion
import mptt.fields


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('slug', models.SlugField(max_length=150, unique=True)),
('is_active', models.BooleanField(default=False)),
('lft', models.PositiveIntegerField(editable=False)),
('rght', models.PositiveIntegerField(editable=False)),
('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
('level', models.PositiveIntegerField(editable=False)),
('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='children', to='inventory.category')),
],
options={
'verbose_name_plural': 'categories',
'ordering': ['name'],
},
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('web_id', models.CharField(max_length=50, unique=True)),
('slug', models.SlugField(max_length=255)),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True)),
('is_active', models.BooleanField(default=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('category', models.ForeignKey(blank=True, default=1, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='product', to='inventory.category')),
],
),
migrations.CreateModel(
name='Media',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('img_url', models.ImageField(upload_to='images/')),
('alt_text', models.CharField(max_length=255)),
('is_feature', models.BooleanField(default=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('product_inventory', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='media', to='inventory.product')),
],
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
88 changes: 88 additions & 0 deletions ecommerce/inventory/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey


class Category(MPTTModel):
name = models.CharField(
max_length=100,
)
slug = models.SlugField(max_length=150, unique=True)
is_active = models.BooleanField(
default=False,
)
parent = TreeForeignKey(
"self",
on_delete=models.PROTECT,
related_name="children",
null=True,
blank=True,
)

class MPTTMeta:
order_insertion_by = ["name"]

class Meta:
ordering = ["name"]
verbose_name_plural = _("categories")

def __str__(self):
return self.name


class Product(models.Model):
web_id = models.CharField(
max_length=50,
unique=True,
)
slug = models.SlugField(
max_length=255,
)
name = models.CharField(
max_length=255,
)
description = models.TextField(blank=True)
category = models.ForeignKey(
Category,
related_name="product",
default=1,
on_delete=models.SET_DEFAULT,
null=True,
blank=True,
)
is_active = models.BooleanField(
default=False,
)
created_at = models.DateTimeField(
auto_now_add=True,
editable=False,
)
updated_at = models.DateTimeField(
auto_now=True,
)

def __str__(self):
return self.name


class Media(models.Model):
product_inventory = models.ForeignKey(
Product,
on_delete=models.PROTECT,
related_name="media",
)
img_url = models.ImageField(upload_to='images/')
alt_text = models.CharField(
max_length=255,
)
is_feature = models.BooleanField(
default=False,
)
created_at = models.DateTimeField(
auto_now_add=True,
editable=False,
)
updated_at = models.DateTimeField(
auto_now=True,
)

13 changes: 13 additions & 0 deletions ecommerce/inventory/tests/test_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ecommerce.inventory.models import Category


def test_create_category(single_category):
new_category = single_category
get_category = Category.objects.all().first()
assert new_category.id == get_category.id


def test_create_category_with_child(category_with_child):
new_sub_category = category_with_child
get_category = Category.objects.all().first()
assert get_category.children.first().id == new_sub_category.id
7 changes: 7 additions & 0 deletions ecommerce/inventory/tests/test_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ecommerce.inventory.models import Product


def test_insert_single_product_with_sub_category(single_product):
new_product = single_product
get_product = Product.objects.all().first()
assert new_product.id == get_product.id
Empty file added ecommerce/search/__init__.py
Empty file.
Binary file added ecommerce/search/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions ecommerce/search/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions ecommerce/search/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SearchConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "ecommerce.search"
Loading

0 comments on commit cb53565

Please sign in to comment.