Programming Chunks

Explore Python And Data Science with me!!

Home ยป Designing An Automated Ecommerce Inventory System- Part1

Designing An Automated Ecommerce Inventory System- Part1

Have you ever wondered how to design an inventory system for ecommerce products. If not let’s understand more of how can we do that. So, here is the flow.

There would be an inventory dashboard where we could see the registered products and the stocks of the same, once the product are purchased, it stock count decreases, and when the count goes to an alarming rate we would send an email to the concern person to add new product.

In this part of this tutorial, we will understand more on setting up the Django project to show the registered product on the inventory dashboard, in the coming part we will handle the notification system when at the alarming rate.

To begin here are few things you should be already done,

  • Set up a typical Django project
  • Add a product folder to it
  • Tweak the setting file to include the product module to the project
  • Have graphql endpoints ready to add the products in the products module
  • Create a template and static module where all the static files like css , js , html files

So, the idea here is to when we add a product to the database, it should appear on the UI as soon as it added. What could be a better choice to run a cron that would pick up the records from database and generate a result in every 1 minute.

We will use apscheduler to set up this cron. To do so, the first thing that we want to do is to create a tasks folder with sample_tasks.py and views.py file in it.

The first file would have our results from database that we want to send.

from django.db.models import Manager
from products.models import Category, Products


class ProductsManager(Manager):
    def get_queryset(self):
        return Products.objects.raw('SELECT * FROM public.products_products')

So, here I am getting my products model and using Manager object provided my Django to write raw postgres queries to fetch data from database.

The views file looks like this.

from django.http import HttpResponse
from django.shortcuts import render
from django.core import serializers
from tasks.sample_tasks import ProductsManager
from django.views.decorators.csrf import csrf_exempt


class ProductsCron():


    def __init__(self,list_of_products):

        self.list_of_products = list_of_products



    def runCron(self,type):
            self.list_of_products = serializers.serialize(
                'json', ProductsManager().get_queryset())
            print("Cron Is Running At Every 1 minute")
            return HttpResponse(self.list_of_products, content_type="application/json")



    def run_task_cron_def(self, request):
        self.list_of_products = serializers.serialize(
            'json', ProductsManager().get_queryset())
        return HttpResponse(self.list_of_products, content_type="application/json")
    

    def returnHomePPage(self, request):
        return render(request, "home.html", {'products': self.list_of_products})

Here I am creating a ProductsCron class and have all the cron related functions this. Basically, we are using the ProductsManager class from the previous file to return http response when class methods are called.

And the class methods, are being called on two main place for this to be functional, one is the product module config file i.e. apps.py and other in the endpoint URL that UI will use to get the products in urls.py file of the product module

class ProductsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'products'

    def ready(self):
        from tasks.views import ProductsCron
        scheduler = BackgroundScheduler()
        scheduler.add_job(lambda: csrf_exempt(
            ProductsCron( None).runCron(0)), 'interval', minutes=0.2)
        scheduler.add_job(lambda: csrf_exempt(
            ProductsCron(None).run_task_cron_def), 'interval', minutes=0.2)
        print("triggering")
        scheduler.start()
products = ProductsCron(None)
urlpatterns = [
    path('admin/', admin.site.urls),
    path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
    path("products/", include("products.urls")),
    path("tasks/", csrf_exempt(products.runCron), name="runCron"),
    path("", products.returnHomePPage, name="returnHomePPage"),
]

Mark we are calling the same runCron function both in the endpoint and the while we start the app that internally gets us the result.

In part2, I will cover the email notification part.

pallavy.com

3 thoughts on “Designing An Automated Ecommerce Inventory System- Part1

  1. Howdy! I just would like to give you a big thumbs up for your
    great information you have got right here on this post.
    I’ll be coming back to your web site for more soon.
    casino en ligne
    Amazing! Its truly amazing paragraph, I have got much clear idea
    on the topic of from this piece of writing.

    casino en ligne
    Hey there! I know this is kinda off topic but I was wondering which blog platform are you using for this website?
    I’m getting fed up of WordPress because I’ve had issues
    with hackers and I’m looking at options for another platform.

    I would be great if you could point me in the direction of a
    good platform.
    meilleur casino en ligne
    I’m more than happy to find this web site. I wanted to thank you for ones
    time due to this fantastic read!! I definitely enjoyed
    every bit of it and I have you book-marked to see new things in your website.

    casino en ligne
    We are a group of volunteers and starting a new scheme in our community.
    Your web site offered us with valuable info to work on. You’ve done a formidable job and our entire
    community will be thankful to you.
    casino en ligne
    Hi there, i read your blog from time to time and i own a
    similar one and i was just wondering if you get a lot
    of spam comments? If so how do you prevent it, any plugin or anything you can advise?

    I get so much lately it’s driving me mad so any help is very much appreciated.

    casino en ligne France
    This website was… how do you say it? Relevant!! Finally I’ve found something which helped me.
    Cheers!
    meilleur casino en ligne
    Unquestionably imagine that which you stated. Your favorite reason seemed to be at the web the easiest thing
    to bear in mind of. I say to you, I definitely get irked at the
    same time as other people think about issues that they plainly don’t understand about.

    You controlled to hit the nail upon the highest and also defined
    out the whole thing with no need side effect , folks can take a signal.
    Will probably be again to get more. Thanks
    casino en ligne francais
    Attractive section of content. I just stumbled upon your
    weblog and in accession capital to assert that I acquire in fact enjoyed account your blog posts.
    Anyway I will be subscribing to your feeds and even I achievement you access consistently quickly.

    meilleur casino en ligne
    Hello, yes this piece of writing is in fact nice and I have
    learned lot of things from it about blogging. thanks.

    casino en ligne

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top