Teach you to deploy the open source Python super beautiful electronic mall — Saleor

Saleor is a fast-growing open source e-commerce platform, based on Python and Django development, and is constantly being updated, so there is no need to worry about the issue of too old version.

Its characteristics are as follows:

1. GraphQL API : The front-end and back-end separation based on GraphQL is the most cutting-edge technology.
2. Dashboard : Administrators can fully control users, processes and products.
3. Order : a comprehensive system of order, delivery and refund.
4. Shopping cart : advanced payment and tax options, support discounts and promotions
5. Payment : flexible API architecture allows integration of any payment method.
6. Geographic adaptation : Automatically support multi-country checkout experience.
7. Support cloud deployment : support Docker deployment.
8. Support Google analysis : integrated Google analysis, you can easily analyze the flow to stay.

Saleor warehouse address:

1. Deployment Guide

Saleor supports a variety of operating methods. You can install and run manually, or you can use Docker to run. The following describes the most common and simplest Docker deployment solution for all platforms.

Before following the instructions below, you need to install Docker Desktop and Docker Compose. If you haven’t installed them, you can take a look at this tutorial:

Docker deployment of Saleor is very convenient, you only need to clone the repository and build the image and then run the service:

# Python 实用宝典# 克隆存储库git clone https://github.com/mirumee/saleor-platform.git --recursive --jobs 3cd saleor-platform# 构建Docker镜像docker-compose build

If you cannot successfully clone the Salor source code repository, please reply to the official account of the Python Practical Collection: Saleor download all the source code.

Saleor uses shared folders to enable real-time code reloading . If you are using Windows or MacOS , you need:

1. Place the cloned sales-platform directory into Docker's shared directory configuration ( Settings  ->  Shared Drives  or  Preferences  ->  Resources  ->  File sharing ).

2. Make sure you have at least 5 GB of dedicated memory in the Docker preferences ( Settings-  >  Advanced  or  Preferences- > Resources-  >  Advanced )

Perform database migrations and package front-end resources:

docker-compose run --rm api python3 manage.py migratedocker-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:

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

Run the service:

Run Saleor with the following command:

docker-compose up

2. Architecture Introduction

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

Saleor consists of three important components:

1. Saleor Core,  it is the back-end server of GraphQL API. Based on Django development, the database uses PostgreSQL and some cache information is stored in Redis.

2. Saleor Dashboard , this is a dashboard that can be used to run a store. It is a static website, so it 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. You can customize this part of the code to meet your own needs, or you can use the Saleor SDK to build a custom storefront.

All three components communicate via HTTPS using GraphQL.

3. Extended development

Although you can develop directly based on the Saleor source code, the official recommendation is not to do so. The reason is that 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 unmaintained code. Embarrassing situation.

Therefore, Saleor provides two ways to add features:

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

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

Below we introduce how to develop extensions based on plug-ins.

As shown in the above figure, Saleor Core provides a callback notification event to the plug-in. The plug-in performs related operations based on this event and interacts with the database.

To develop a plug-in, you must inherit the BasePlugin base class, and then rewrite some of the methods. For example, the following example rewrites the method and adds some operations during order creation: postprocess_order_creation

# Python实用宝典# custom/plugin.py from django.conf import settingsfrom urllib.parse import urljoin from ..base_plugin import BasePluginfrom .tasks import api_post_request_task  class CustomPlugin(BasePlugin):    def postprocess_order_creation(self, order: "Order", previous_value: Any):        # 订单创建时的操作        data = ...         transaction_url = urljoin(settings.CUSTOM_API_URL, "transactions/createoradjust")        api_post_request_task.delay(transaction_url, data)

To load the plug-in, you need to configure it in setup.py to automatically discover the installed plug-in. To make the plug-in can be found, you need to set the field, and uses this syntax definitions plugin: . entry_points saleor_plugins package_name = package_name.path.to:PluginClass

Examples are as follows:

# setup.pyfrom 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 INSTALLED_APPS so that you can take advantage of Django's features, such as ORM integration and database migration.

Note that our previous order creation operation used the .delay syntax, which is Celery's asynchronous task. Because some plug-in operations should be completed asynchronously, Saleor uses Celery and will find all asynchronous tasks declared in tasks.py in the plug-in directory:

# custom_plugin/tasks.py import jsonfrom celery import shared_taskfrom typing import Any, Dict import requestsfrom requests.auth import HTTPBasicAuthfrom django.conf import settings  @shared_taskdef 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 above api_post_request function is the asynchronous task used by the previous plug-in. After the plug-in calls the delay method, this task will be queued for asynchronous execution.

Well, the above is a simple plug-in development example. I personally think that Saleor's development model is still very good. If you need it, you can use this project to build your own mall.

This is the end of our article. If you like today's Python practical tutorial, please continue to pay attention to the Python practical guide.

If you have any questions, you can reply in the background of the official account: add a group , answer the corresponding red letter verification information , and enter the mutual aid group to ask.

Originality is not easy, I hope you can click like below and support me to continue to create, thank you!

Click below to read the original text for a better reading experience

Python Practical Collection (pythondict.com) is
not just a collection,
please pay attention to the official account : Python Practical Collection