Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Add session length field to Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvdm committed Feb 6, 2015
1 parent a8beb59 commit 90d2d08
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def forwards(self, orm):
self.gf('django.db.models.fields.DecimalField')(default='0.0', null=True, max_digits=10, decimal_places=3),
keep_default=False)

# Adding field 'Transaction.session_length'
db.add_column(u'billing_transaction', 'session_length',
self.gf('django.db.models.fields.DecimalField')(default='0.0', null=True, max_digits=10, decimal_places=3),
keep_default=False)


def backwards(self, orm):
# Deleting field 'Transaction.session_unit_credits'
Expand All @@ -26,6 +31,9 @@ def backwards(self, orm):
# Deleting field 'Transaction.session_length_cost'
db.delete_column(u'billing_transaction', 'session_length_cost')

# Deleting field 'Transaction.session_length'
db.delete_column(u'billing_transaction', 'session_length')


models = {
u'auth.group': {
Expand Down Expand Up @@ -134,6 +142,7 @@ def backwards(self, orm):
'session_cost': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '10', 'decimal_places': '3'}),
'session_created': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'session_credits': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '20', 'decimal_places': '6'}),
'session_length': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '10', 'decimal_places': '3'}),
'session_length_cost': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '10', 'decimal_places': '3'}),
'session_length_credits': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '20', 'decimal_places': '6'}),
'session_unit_cost': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'null': 'True', 'max_digits': '10', 'decimal_places': '3'}),
Expand Down
4 changes: 4 additions & 0 deletions go/billing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ class Transaction(models.Model):
help_text=_("The number of credits this transaction adds or "
"subtracts."))

session_length = models.DecimalField(
null=True, max_digits=10, decimal_places=3, default=Decimal('0.0'),
help_text=_("The length of the session (in seconds)"))

status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default=STATUS_PENDING,
help_text=_("The status of this transaction. One of pending, "
Expand Down
12 changes: 10 additions & 2 deletions go/billing/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def mk_transaction(account, tag_pool_name='pool1',
message_direction=MessageCost.DIRECTION_INBOUND,
message_cost=100, storage_cost=50, session_cost=10,
session_unit_cost=10, session_unit_time=20,
session_length_cost=10, markup_percent=10.0,
credit_factor=0.25, credit_amount=28,
session_length_cost=10, session_length=20,
markup_percent=10.0, credit_factor=0.25, credit_amount=28,
provider=None, status=Transaction.STATUS_COMPLETED,
created=None, **kwargs):
transaction = Transaction(
Expand All @@ -94,6 +94,7 @@ def mk_transaction(account, tag_pool_name='pool1',
session_unit_cost, markup_percent),
session_length_credits=get_session_length_credits(
session_length_cost, markup_percent),
session_length=maybe_decimal(session_length),
status=status, **kwargs)

transaction.save()
Expand Down Expand Up @@ -134,6 +135,13 @@ def mk_statement(account,
return statement


def get_session_length_cost(unit_cost, unit_length, length):
return MessageCost.calculate_session_length_cost(
maybe_decimal(unit_cost),
maybe_decimal(unit_length),
maybe_decimal(length))


def get_message_credits(cost, markup):
if cost is None or markup is None:
return None
Expand Down
3 changes: 2 additions & 1 deletion go/billing/tests/test_django_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_to_json(self):
u"markup_percent": 10.0,
u"message_cost": 100.0,
u"storage_cost": 50.0,
u"session_cost": 10.0,
u'session_unit_cost': 10.0,
u'session_unit_time': 20.0,
u'session_length_cost': 10.0,
Expand All @@ -50,7 +51,7 @@ def test_to_json(self):
u"session_credits": float(get_session_credits(10.0, 10.0)),
u"message_direction": u"Inbound",
u"message_id": None,
u"session_cost": 10.0,
u"session_length": 20.0,
u"session_created": None,
u"status": u"Completed",
u"tag_pool_name": u"pool1",
Expand Down
13 changes: 12 additions & 1 deletion go/billing/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from go.billing.tests.helpers import (
start_of_month, end_of_month, this_month, maybe_decimal,
get_billing_account, mk_tagpool, mk_message_cost, mk_transaction,
mk_statement, get_message_credits, get_session_credits,
mk_statement, get_session_length_cost,
get_message_credits, get_session_credits,
get_storage_credits, get_session_unit_credits,
get_session_length_credits, get_line_items)

Expand Down Expand Up @@ -111,6 +112,7 @@ def test_mk_transaction(self):
markup_percent=10.0,
credit_factor=11.0,
credit_amount=28,
session_length=23,
created=date(2015, 3, 23),
status=Transaction.STATUS_COMPLETED)

Expand All @@ -134,6 +136,7 @@ def test_mk_transaction(self):
markup_percent=Decimal('10.0'),
credit_factor=Decimal('11.0'),
credit_amount=28,
session_length=Decimal('23.0'),
created=date(2015, 3, 23),
status=Transaction.STATUS_COMPLETED)

Expand Down Expand Up @@ -189,6 +192,14 @@ def test_mk_statement(self):
cost=Decimal('200.0'),
credits=None)))

def test_get_session_length_cost(self):
self.assertEqual(
get_session_length_cost(1, 2, 3),
MessageCost.calculate_session_length_cost(
Decimal('1.0'),
Decimal('2.0'),
Decimal('3.0')))

def test_get_message_credits(self):
self.assertEqual(
get_message_credits(0.1, 10.0),
Expand Down

0 comments on commit 90d2d08

Please sign in to comment.