saleor

Saleor is a fast-growing open source e-commerce platform based on Python and Django.

 

It has the following characteristics.

1.GraphQL API:The front and back-end separation based on GraphQL implementation is among the most cutting-edge technologies.
2.Dashboard:Administrators have full control over users, processes and products.
3.Orders:An integrated system for orders, shipments and refunds.
4.Shopping Cart:Advanced payment and tax options, support for discounts and promotions
5.Payments:Flexible API architecture allows integration with any payment method.
6.Geo-Adaptive:Automatic support for multi-country checkout experience.
7.Support for Cloud Deployment:Support for Docker deployment.
8.Support Google Analytics:Integrated with Google Analytics, it is easy to analyze traffic going and staying.

Saleor github

1.Deployment Guide

Saleor supports a variety of ways to run, you can use the manual installation and run the way, you can also use Docker to run, the following is to introduce the platform-wide common and the most simple Docker deployment scheme.

Docker is very easy to deploy Saleor, you just need to clone the repository and build the image and then run the service.

# Clone Repository
git clone https://github.com/mirumee/saleor-platform.git --recursive --jobs 3
cd saleor-platform
# Building Docker images
docker-compose build

Executing database migrations and packaging front-end resources.

docker-compose run --rm api python3 manage.py migrate
docker-compose run --rm api python3 manage.py collectstatic --noinput

 

(Optional) Populate the database with sample data.

docker-compose run --rm api python3 manage.py populatedb

 

Finally, create an administrator account for yourself at

docker-compose run --rm api python3 manage.py createsuperuser

Running services:

Use the following command to run Saleor:

docker-compose up

 

 

2.Introduction to the architecture

If you are going to develop based on Saleor, then you must understand its architecture.

saleor ecommerce

Saleor consists of three important components.

1.Saleor Core, It is the backend server for GraphQL API. Developed based on Django, the database uses PostgreSQL and stores some cached information in Redis.

2.Saleor Dashboard, This is a dashboard that can be used to run a store. It is a static website and therefore does not have any back-end code of its own, it is a React program that talks to the Saleor Core core server.

3.Saleor Storefront, This is a sample store based on React implementation, you can customize this part of the code to meet your own needs or you can build a custom storefront using Saleor SDK.

All three components use GraphQL to communicate over HTTPS.

 

3.Extended Development

Although you can develop directly based on Saleor source code, it is officially recommended not to do so because once your code conflicts with the official Saleor source code, it will be difficult for you to keep up with the official updates, which will eventually lead to an embarrassing situation where no one will maintain the code.

Saleor therefore offers two ways to add functionality.

1.Plug-in function:The plug-in provides an ability to run additional code on Saleor Core and has the ability to access the database.

2.APPS:App development based on GraphQL API and Saleor Core, you can also use WebHooks to subscribe to events.

Below we describe how to develop extensions based on plugins.

saleor extended

As shown above, Saleor Core provides a callback notification event to the plugin, based on which the plugin performs relevant operations and interacts with the database.

To develop a plugin, you must inherit the BasePlugin base class and then override some of the methods. For example, the following example overrides the postprocess_order_creation method to add some operations when an order is created.

# custom/plugin.py

from django.conf import settings
from urllib.parse import urljoin

from ..base_plugin import BasePlugin
from .tasks import api_post_request_task


class CustomPlugin(BasePlugin):
    def postprocess_order_creation(self, order: "Order", previous_value: Any):
        # Operations at order creation
        data = ...

        transaction_url = urljoin(settings.CUSTOM_API_URL, "transactions/createoradjust")
        api_post_request_task.delay(transaction_url, data)

 

To load a plugin, you need to configure setup.py to automatically discover the installed plugins. To make plugins discoverable, you need to set the saleor_plugins field of entry_points, and define plugins using this syntax: package_name = package_name.path.to:PluginClass .

Examples are as follows.

# setup.py
from setuptools import setup

setup(
    ...,
    entry_points={
        "saleor.plugins": [
            "my_plugin = my_plugin.plugin:MyPlugin"
        ]
    }
)

 

If your plugin is a Django application, the package name (the part before the equal sign) will be added to Django's INSTALLED_APPS so that you can take advantage of Django features such as ORM integration and database migration.

Notice that our previous order creation operation uses the .delay syntax, which is Celery's asynchronous task. Since some plugin operations should be done asynchronously, Saleor uses Celery and will find all the asynchronous tasks declared by tasks.py in the plugin directory.

# custom_plugin/tasks.py

import json
from celery import shared_task
from typing import Any, Dict

import requests
from requests.auth import HTTPBasicAuth
from django.conf import settings


@shared_task
def api_post_request(
    url: str,
    data: Dict[str, Any],
):
    try:
        username = "username"
        password = "password"
        auth = HTTPBasicAuth(username, password)
        requests.post(url, auth=auth, data=json.dumps(data), timeout=settings.TIMEOUT)
    except requests.exceptions.RequestException:
        return

 

The api_post_request function above is the asynchronous task used by the previous plugin. After the plugin calls the delay method, this task will be stuffed into the queue and executed asynchronously.

keywords: pythonsaleordjango