Coder Social home page Coder Social logo

django-calculation's Introduction

django-calculation

Release PyPI version PyPI - Django Version PyPI - Python Version

Make simple calculations in your django forms using django-calculation. This app provide a Django Widget that derives its value from a expression defined in the widget instance.

The field is updated when any of the source fields change.

calculation

Installation

pip install django-calculation

Add calculation to your INSTALLED_APPS

INSTALLED_APPS = [
    ...
    'calculation',
]

Usage

Import calculation and complete the definition.

Example

Using FormulaInput widget

from django import forms

import calculation


class TestForm(forms.Form):
    quantity = forms.DecimalField()
    price = forms.DecimalField()
    amount = forms.DecimalField(
        widget=calculation.FormulaInput('quantity*price') # <- using single math expression
    )
    apply_taxes = forms.BooleanField(initial=True)
    tax = forms.DecimalField(
        # using math expression and javascript functions.
        widget=calculation.FormulaInput('apply_taxes ? parseFloat(amount/11).toFixed(2) : 0.0') 
    )

django-calculation works with static files and therefore it is necessary to include the media of the form in the template file.

<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

{{ form.media }}

Modes

Currently the app support two modes of calculation FORMULA and SUMMARY.

FORMULA

The field value derive from a formula expression. In the expression you can refer to the form field using its name.

amount = forms.DecimalField(
    widget=calculation.FormulaInput('quantity*price')
)

SUMMARY

The field value derive from a summary definition, it is useful when you need to get the sum of a django formset field.

total = forms.DecimalField(
    widget=calculation.SummaryInput(
            function=calculation.SUM,
            field='amount' 
    )

Summary example

Summary definition in OrderForm

class OrderForm(forms.ModelForm):
    total = forms.DecimalField(
        # using SumInput a SummaryInput abstraction
        widget=calculation.SumInput('subtotal')
    )
    class Meta:
        model = Order
        fields = ['date', 'customer']

OrderDetForm also contain a calculated field subtotal.

class OrderDetForm(forms.ModelForm):
    subtotal = forms.DecimalField(
        widget=calculation.FormulaInput('quantity*price')
    )
    class Meta:
        model = OrderDet
        fields = ['product', 'price', 'quantity', 'subtotal']

# formset definition
OrderDetFormSet = forms.inlineformset_factory(Order, OrderDet, OrderDetForm)

chrome-capture

Roadmap

  • Create demo project.
  • Create documentation.
  • Add changelog.

django-calculation's People

Contributors

blasferna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-calculation's Issues

Uncaught TypeError: value.element is null

The error appears when using a formulated text field belonging to a formset that references an external form field.

Uncaught TypeError: value.element is null
    runFormula http://127.0.0.1:8000/static/calculation/js/calculation.js:180
    executeAll http://127.0.0.1:8000/static/calculation/js/calculation.js:173
    handleBlurCb http://127.0.0.1:8000/static/calculation/js/calculation.js:308
3 calculation.js:180:32
    runFormula http://127.0.0.1:8000/static/calculation/js/calculation.js:180
    executeAll http://127.0.0.1:8000/static/calculation/js/calculation.js:173
    handleBlurCb http://127.0.0.1:8000/static/calculation/js/calculation.js:308

The definition of forms

class ExpenseForm(forms.ModelForm):
    class Meta:
        model = Expense
        fields = ['doc_type', 'number', 'account', 'currency', 'currency_rate']

class ExpenseDetailForm(forms.ModelForm):
    class Meta:
        model = ExpenseDetail
        fields = ['item', 'amount', 'amount_local']
        # The currency_rate field belong to ExpenseForm
        widgets = {
            'amount_local': calculation.FormulaInput('amount*currency_rate')
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.