Moment For Technology

Sadmin: Build a private Django public library for code reuse

Posted on Aug. 7, 2023, 8:51 p.m. by Paula Eglinton
Category: reading Tag: django devops

We use Django to develop a number of internal management systems, such as Probius, Kerrigan, Proxy, etc. These systems look the same, but the actual implementation of the function is very different. Why do these different systems look the same? Not only do they use the same front-end template, but we've bundled together a basic Django application that includes many of the basic functions that most systems use, such as front-end pages and common plugins, back-end user and permission management, and so on. Any Django project that integrates with this application will quickly have these features, not only saving a lot of development time for basic features, but also making subsequent updates very user-friendly

Design ideas

Because of various reasons, we have no way to put all of the functions are concentrated in a system, there will always be the existence of multiple systems, and for each system has some basic functions to achieve, such as user management, rights management, logging, etc., in the beginning I will copy has been developed to complete the project code to use for the new project, the basis of This is the most simple and convenient, but if there are bug fixes or function updates, I have to modify the code of multiple projects at the same time, which is not only tedious but also error-prone. In order to optimize the experience, I decided to separate all the basic functions from the project and package them into a public application for project reference, which we called sadmin

Setuptools is used to complete the packaging. After the packaging is completed, it is uploaded to the private warehouse. PIP can be installed directly in the application server, which is easy and convenient

The main function

Sadmin provides the following functions:

1. Integrated adminLt-based front-end templates and common front-end components, such as select2, Datatables, etc

2. The Title, Title and theme of the website can be configured in the background

3. Multiple authentication modes, such as OpenId and LDAP

4. Configure menus in the background and display different menus according to different permissions

5. Manage users, groups, and departments, and log in and out users

6. Automatically record operation logs

7. Encapsulate the increase, deletion, change and check of the database

Djangos Admin system does this very well. However, the Admin system is not only ugly, but also difficult to implement for many complicated requirements. Therefore, we directly abandoned the Admin system. I refer to Django ViewSet ListCreateView and RetrieveUpdateDestroyView encapsulates the two method to deal with

For example, add, delete, change, and check the Product table. The back end only needs a few lines of code

class ProductList(ListCreateView):
    model = Product
    template = 'workflow/product.index.html'
    permission = {'get': 'loginuser', 'post': 'workflow.product_change'}

class ProductDetail(RetrieveUpdateDestroyView):
    model = Product
    permission = {'get': 'workflow.product_select', 'put': 'workflow.product_change',
                  'delete': 'workflow.product_delete'}
Copy the code

A few lines of code allow the back end to handle adding, deleting, changing, and checking the database, while returning json data in a fixed format, while there is not much specification for the front end page, so you can design it as you like

In addition to the above main functions, but also integrated some such as the whole site watermarking, notification sending, data verification and other small functions

Use effect

With Sadmin, we no longer need to consider these basic functions in the development process, and focus more on the business itself, which has significantly improved efficiency. Nearly 10 internal projects have used Sadmin and run stably

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.