django-business-logic demo

by dgk

GitHub Readme.md

django-business-logic

travis-ci codeclimate codecov coverage MIT license python versions django versions pypi version pypi version status

FRONTEND DEVELOPER WANTED!!!

If you are familiar with react/mobx and want to help us build awesome django-business-logic library, contact us at dgk@dgk.su.

Documentation

Work on documentation in the process, see draft here.

Introduction

The main goal of this project is to provide django site users (such as administrators or ordinal users) with ability to visually edit business logic layer. This library implements execution engine and code editor based on awesome blockly library. See screenshots.

Warning

Please note that this software is still Alpha/Beta quality and is not recommended for production use. The library is currently in active development and any API may be changed. Migration history for models is not supported now. Some internal objects are world writable!

Requirements

This library requires the following:

  • Python (2.7, 3.4, 3.5, 3.6)
  • Django (1.11, 2.0, 2.1) (for Django 1.8, 1.9, 1.10 use 0.4.13 version)
  • django-rest-framework 3.8+

Architecture

Internally program code is stored as special django models such as NumberConstant, IfStatement, Assignment and so forth. Structure of syntax tree is held by class Node derived from treebeard.NS_Node. Operators and operands are linked with Node objects via django contenttypes system. Other details are briefly described below in sections Administrative setup and Execution

Docker demo

You can run demo app locally using docker:

docker build . -t django-business-logic-demo
docker run --rm -it -p 8000:8000 django-business-logic-demo

or using docker-compose:

docker-compose up

Also you can use prebuilt image:

docker run --rm -it -p 8000:8000 dgksu/django-business-logic:demo

Now you can login into django admin interface http://localhost:8000/admin/ with username test and password test.

Heroku demo

You can deploy demo app directly to Heroku to see the app live. Just click the button below. This will walk you through getting this app up and running on Heroku in minutes.

Deploy

Install

There are a few different ways you can install django-business-logic:

  • Use pip: pip install -U django-business-logic
  • Download the zipfile from the releases page and install it.
  • Checkout the source: git clone git://github.com/dgk/django-business-logic.git and install it yourself.

Server setup

  1. Edit settings.py and add business_logic to your INSTALLED_APPS
# settings.py
INSTALLED_APPS = (
    # ...
    'django.contrib.contenttypes',
    
    'ace_overlay', # optional, for comfortable python functions editing
    'adminsortable2',
    'nested_admin',
    'polymorphic',
    'rest_framework', # optional, provided browsable API for this library handy development
    'django_filters', # ^^ same
    
    'business_logic',
    # ...
)
  1. Edit urls.py and include business_logic.urls
# urls.py
urlpatterns = (
  # ...
  url('^nested_admin/', include('nested_admin.urls')),
  url('^business-logic/', include('business_logic.urls')),
  # ...
)
  1. Make migrations
python manage.py migrate
  1. Collect static files
python manage.py collectstatic

Administrative setup

First you should define one or more ProgramInterface objects via django admin interface (http://localhost:8000/admin/business_logic/programinterface/).

Each ProgramInterface must contain one or more ProgramArgument objects. The ProgramArgument object represents one instance of django.db.Model derived class specified as django.contrib.contentypes.ContentType instance (e.g. for your custom Order model) and his keyword argumet name (e.g. order).

Each ProgramArgument object must contain one or more ProgramArgumentField which represents one field of django model (e.g. sum for Order object or delivery_address.city for city field nested into Order DeliveryAddress model).

If you want to use system-wide references (e.g. your custom City or ProductCategory django model) and you define represented ProgramArgumentField you should register referenced model via django admin interface(http://localhost:8000/admin/business_logic/referencedescriptor/).

Next create one or more Program objects which must implements described ProgramInterface(e.g. named "On Order create" with "on_order_create" code field for programmatic access)

Visual Programming

After setup you can go to web editor interface (http://localhost:8000/static/business_logic/index.html), choose program interface, program, create and start editing your first program version.

Execution

You may inject execution engine call at arbitrary place of your code, such as custom form.save, model.post_save methods, any django.dispatch.Signal handler or so on. Just instantiate appropriate ProgramVersion object and pass kwargs described in the ProgramInterface admin page to its execute() method . e.g.

from django.views.generic.edit import CreateView

from business_logic.models import Program

class OrderCreate(CreateView):
    def form_valid(self, form):
       order = form.save()
       program = Program.objects.get(code="on_order_create")
       program_version = program.versions.order_by("id").last()
       program_version.execute(order=order)

ProgramVersion.execute() method can accept instance of business_logic.Context object. If this parameter omitted execute() method creates new instance of Context with default parameters.

It can be initialized by the following parameters:

  • log(boolean) - create execution log (default - False)
  • debug(boolean) - create special Execution object contained all variables passed to execute() method, ProgramVersion object, start and end execution times, and root of log objects if its created (default - False)

ProgramVersion.execute() returns the Context instance.

Credits

Many thanks to:

TODO

  • References support
  • Execution log viewer
  • Imported and editable via admin interface python functions
  • Lists and Loops
  • Implicit strong static typing
  • Visually editable functions and its libraries
  • Two-dimensional constant tables (matrices)
  • Code sharing
  • Pure python code generation for execution speedup

Library Development

Backend development environment

virtualenv

Virtualenv is probably what you want to use during development. Once you have virtualenv installed, just fire up a shell and create your own environment.

virtualenv venv
source venv/bin/activate
pip install -r requirements.dev.txt

Installing test data

python manage.py migrate
python manage.py loaddata sites/dev/fixtures/data.json

Running backend dev server

python manage.py runserver

An instance of django dev server will be listening on http://localhost:8000/ . Now you can login into django admin interface http://localhost:8000/admin/ with username test and password test.

Frontend development environment

Fronted source files located under frontend folder.

cd frontend

Installing dependencies

npm install

Running webpack dev server

npm run server:dev:hmr

Now webpack dev server will be listening on http://localhost:3000/ .

Building frontend files

npm run build:prod

Running tests

Running backend tests

python manage.py test

Test it all

You need to know at least one command; the one that runs all the tests:

tox