django-ledgersv0.1.0
Django app which implements ledgers to store cumulative data.
Description
A Ledger is an abstraction over Django ORM which consists of 2 tables:
- movements — stores periodic data about ledger's state change.
- totals — stores pre-calculated cumulative result for some period.
Ledger's state is changed via Documents — an "atomic" abstraction over set of simultaneous changes.
Examples
Warehouse remains
Items:
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=200, unique=True)
can be stored at Warehouse:
class Warehouse(models.Model):
name = models.CharField()
via Warehouse Balance Ledger:
from ledgers.enum import CumulativeLedgerRecordActionEnum
from ledgers.ledger import CumulativeLedger
from ledgers.ledger.fields import dimensions, resources
class BalanceAtWarehousesLedger(CumulativeLedger):
warehouse = dimensions.ForeignKey(
to=Warehouse,
null=False,
)
item = dimensions.ForeignKey(
to=Item,
null=False,
)
amount = resources.Integer()
comment = models.CharField(max_length=200, null=False, default='')
@classmethod
def movements_for_itemmovement(cls, doc):
# noinspection PyCallingNonCallable
return (
cls.mvm_model(
period=doc.execution_date,
registrator=doc,
line_index=0,
action=CumulativeLedgerRecordActionEnum.OUTCOME,
warehouse_id=doc.from_warehouse_id,
item_id=doc.item_id,
amount=doc.amount,
),
cls.mvm_model(
period=doc.execution_date,
registrator=doc,
line_index=1,
action=CumulativeLedgerRecordActionEnum.INCOME,
warehouse_id=doc.to_warehouse_id,
item_id=doc.item_id,
amount=doc.amount,
),
)
and changed via Item Movement:
class ItemMovement(Document):
class DocumentMeta(Document.DocumentMeta):
related_ledgers = [BalanceAtWarehousesLedger]
default_prefix = 'MVM'
from_warehouse = models.ForeignKey(
Warehouse,
on_delete=models.CASCADE,
null=False,
blank=False,
related_name='+',
)
to_warehouse = models.ForeignKey(
Warehouse,
on_delete=models.CASCADE,
null=False,
blank=False,
related_name='+',
)
item = models.ForeignKey(
Item,
on_delete=models.CASCADE,
null=False,
blank=False,
)
amount = models.IntegerField(
null=False,
blank=False,
)
Then, on creating an ItemMovement:
from ledgers.enum import DocumentStateEnum
# prepare instances
t_shirt = Item.objects.get_or_create(name='T-shirt')
london_warehouse = Warehouse.objects.get_or_create(name='London')
tokyo_warehouse = Warehouse.objects.get_or_create(name='Tokyo')
# prepare and save document
doc = ItemMovement(
state=DocumentStateEnum.ACTIVE,
item=t_shirt,
from_warehouse=london_warehouse,
to_warehouse=tokyo_warehouse,
amount=100,
)
doc.save()
Movements and totals tables will be updated, and actual remains for any period can be fetched:
from example.exampleapp.models import Item, Warehouse, BalanceAtWarehousesLedger
from datetime import timedelta
# prepare instances
t_shirt = Item.objects.get_or_create(name='T-shirt')
london_warehouse = Warehouse.objects.get_or_create(name='London')
tokyo_warehouse = Warehouse.objects.get_or_create(name='Tokyo')
remains_before_document_date = BalanceAtWarehousesLedger.remains_qs(
period=doc.execution_date - timedelta(days=1),
)
remains_after_document_date = BalanceAtWarehousesLedger.remains_qs()
print(list(remains_before_document_date))
# [
# {
# 'warehouse_id': 1,
# 'item_id': 1,
# 'amount_remains': 0
# },
# {
# 'warehouse_id': 2,
# 'item_id': 1,
# 'amount_remains': 0
# }
# ]
print(list(remains_after_document_date))
# [
# {
# 'warehouse_id': 1,
# 'item_id': 1,
# 'amount_remains': -10
# },
# {
# 'warehouse_id': 2,
# 'item_id': 1,
# 'amount_remains': 10
# }
# ]
Features
django-ledgers @ v0.1.0 can:
- store push tokens from FCM or HMS
- link push tokens with their users and applications
- store push notifications
- store push notifications extra kwargs (e.g. deeplinks)
- compose recipients via UI based on user-specified conditions
- watch notifications schedule on calendar
- send scheduled push notifications
- store applications as swappable model
Usage example
TODO
exampleproj
Example project is a showcase django project.
You can reference to it for usage cases, examples, testing.
You must never deploy exampleproj in production due to exposed SECRET_KEY.
Getting Started
Dependencies
Python packages
djangodjango-cte
Installing
Using Python Package Index
-
make sure to use latest
pip:python3 -m pip install --upgrade pip
-
install
django-ledgers:python3 -m pip install django-ledgers
OR download package from releases
-
download release asset (
.tar.gzor.whl) -
make sure to use latest
pip:python3 -m pip install --upgrade pip
-
install
djangoFCMfrom file:python3 -m pip install /path/to/downloaded/asset.tar.gz # or .whl
OR clone from repository
-
clone project:
git clone \ --depth=1 \ --branch=master \ git@github.com:omelched/django-ledgers.git \ </path/to/downloads>
-
move
/django-ledgers/ledgerssolely to folder containing django appsmv </path/to/downloads>/django-ledgers/ledgers \ </path/to/django/project/apps>
-
remove leftovers
rm -rf </path/to/downloads>/django-ledgers
Configuring
Installing application
Add django-ledgers to INSTALLED_APPS in your Django project settings.py.
If you installed package the third way, </path/to/django/project/apps>
must be added to PYTHONPATH. If you not sure add code below in your Django project manage.py before calling main():
sys.path.append('</path/to/django/project/apps>')
Subclass ledgers.ledger.cumulative.CumulativeLedger and ledgers.models.documents.document.Document.
Migrations
Execute database migrations:
python example/manage.py migrate
Collect static:
python example/manage.py collectstatic
Authors
@omelched (Denis Omelchenko)
Contributors
Changelist
django-ledgers version history and changelist available at releases page.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This project is inspired by 1C ORM. More on topic:
Metadata
Release files for django-ledgers 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-ledgers-0.1.0.tar.gz | 18.0 kB | Without provenance | Details for django-ledgers-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_ledgers-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for django_ledgers-0.1.0-py3-none-any.whl |
Total release size: 41.7 kB
Release files / django-ledgers-0.1.0.tar.gz
| Download URL | django-ledgers-0.1.0.tar.gz |
|---|---|
| Size | 18.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07796b6b45c7fde6ed5ab414128b28c557c1013d3d68c8d12ff27d99018de20a
|
|
BLAKE2b-256 checksum How to use checksums |
b852973b73a79761d6754f0ee4662417ef4f125832a58245c64269f8155a3b7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / django_ledgers-0.1.0-py3-none-any.whl
| Download URL | django_ledgers-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5522274e97c2b6082c8866ef959e92ba9be6015469a0c44e653775fff571758c
|
|
BLAKE2b-256 checksum How to use checksums |
b0ef093d45d56690151f86b2893008e1f55f260e1c45ffa27ce7d3894813622f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for django-ledgers 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.