Skip to main content

Managed accounts for django-oscar

Project description

Managed accounts for Django

A 'managed account' is an allocation of money that can be debited and credited. This
package provides managed account functionality for use with the e-commerce framework
[Oscar]( It can also be used
standalone without Oscar.

Accounts can be used to implement a variety of interesting components, including:

* Giftcards
* Web accounts
* Loyalty schemes

Basically anything that involves tracking the movement of funds within a closed

This package uses [double-entry bookkeeping](
where every transaction is recorded twice (once for the source and once for the
destination). This ensures the books always balance and there is full audit
trail of all transactional activity.

If your project manages money, you should be using a library like this. Your
finance people will thank you.

[![Build Status](](
[![Coverage Status](](


* An account has a credit limit which defaults to zero. Accounts can be set up
with no credit limit so that they are a 'source' of money within the system.
At least one account must be set up without a credit limit in order for money
to move around the system.

* Accounts can have:
- No users assigned
- A single "primary" user - this is the most common case
- A set of users assigned

* A user can have multiple accounts

* An account can have a start and end date to allow its usage in a limited time

* An account can be restricted so that it can only be used to pay for a range of

* Accounts can be categorised


[![Dashboard account list](](
[![Create new account ](](
[![Dashboard account list](](
[![Dashboard account list](](


Install using pip:

pip install django-oscar-accounts

and add `accounts` to `INSTALLED_APPS`. Then, add the following

* `ACCOUNTS_SOURCE_NAME` - The name of the 'source' account which is used to
transfer funds to other accounts (it has no credit limit).
* `ACCOUNTS_REDEMPTIONS_NAME` - The name of the 'sales' account which is the
recipient of any funds used to pay for orders
* `ACCOUNTS_LAPSED_NAME` - The name of the 'expired' account which is the
recipient of any funds left if accounts that expire. A cronjob is used to
close expired accounts.

Running ` syncdb` will create the appropriate tables and initialise accounts based
on the above 3 settings.

If running with Oscar, add an additional path to your `TEMPLATE_DIRS`:
``` python


This allows the templates to be customised by overriding blocks instead of
replacing the entire template.

You should also set-up a cronjob that calls:

./ close_expired_accounts

to close any expired accounts and transfer their funds to the 'expired'


Create account instances using the manager:

``` python

from decimal import Decimal
import datetime

from django.contrib.auth.models import User

from accounts import models

anonymous_account = models.Account.objects.create()

barry = User.objects.get(username="barry")
user_account = models.Account.objects.create(primary_user=barry)

no_credit_limit_account = models.Account.objects.create(credit_limit=None)
credit_limit_account = models.Account.objects.create(credit_limit=Decimal('1000.00'))

today =
next_week = today + datetime.timedelta(days=7)
date_limited_account = models.Account.objects.create(
start_date=today, end_date=next_week)

Transfer funds using the facade:

``` python

from accounts import facade

staff_member = User.objects.get(username="staff")
trans = facade.transfer(source=no_credit_limit_account,

Reverse transfers:

``` python
facade.reverse(trans, user=staff_member,
description="Just an example")

If the proposed transfer is invalid, an exception will be raised. All
exceptions are subclasses of `accounts.exceptions.AccountException`. Your
client code should look for exceptions of this type and handle them

Client code should only use the `accounts.models.Budget` class and the
two functions from `accounts.facade` - nothing else should be required.

Error handling

Note that the transfer operation is wrapped in its own database transaction to
ensure that only complete transfers are written out. When using Django's
transaction middleware, you need to be careful. If you have an unhandled
exception, then account transfers will still be committed even though nothing
else will be. To handle this, you need to make sure that, if an exception
occurs during your post-payment code, then you roll-back any transfers.

Here's a toy example:

``` python
from accounts import facade

def submit(self, order_total):
# Take payment first
transfer = facade.transfer(self.get_user_account(),
# Create order models
except Exception, e:
# Something went wrong placing the order. Roll-back the previous
# transfer

In this situation, you'll end up with two transfers being created but no order.
While this isn't ideal, it's the best way of handling exceptions that occur
during order placement.


* `ACCOUNTS_SOURCE_NAME` The name of the 'source' account
* `ACCOUNTS_SALES_NAME` The name of the 'sales' account
* `ACCOUNTS_EXPIRED_NAME` The name of the 'expired' account
* `ACCOUNTS_MIN_INITIAL_VALUE` The minimum value that can be used to create an
account (or for a top-up)
* `ACCOUNTS_MAX_INITIAL_VALUE` The maximum value that can be transferred to an


Fork repo, set-up virtualenv and run:

make install

Run tests with:


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-oscar-accounts, version 0.2
Filename, size File type Python version Upload date Hashes
Filename, size django-oscar-accounts-0.2.tar.gz (30.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page