Skip to content
This repository was archived by the owner on Jan 24, 2018. It is now read-only.

Commit 41717e5

Browse files
author
Ted Salmon
committed
[IMP] sale_clouder: Finish module
* Fix docstrings * Implement unimplemented methods * Add tests for metrics * Add tests for contracts
1 parent c3760f7 commit 41717e5

19 files changed

+159
-19
lines changed

clouder_metric/__init__.py

100755100644
File mode changed.

clouder_metric/__manifest__.py

100755100644
File mode changed.

clouder_metric/models/__init__.py

100755100644
File mode changed.

clouder_metric/models/clouder_metric_interface.py

100755100644
+3-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ class ClouderMetricInterface(models.Model):
5252
interval_type = fields.Selection(
5353
related='cron_id.interval_type',
5454
)
55-
query_code = fields.Text(
56-
# default=lambda s: s._default_query_code()
57-
)
55+
query_code = fields.Text()
56+
5857

5958
@property
6059
@api.multi
@@ -67,5 +66,5 @@ def metric_id(self):
6766
@api.multi
6867
def name_get(self):
6968
return [
70-
(r.id, '%s - %s' % (r.type_id.name, r.metric_id)) for r in self
69+
(r.id, '%s - %s' % (r.type_id.name, r.metric_ref)) for r in self
7170
]

clouder_metric/models/clouder_metric_type.py

100755100644
File mode changed.

clouder_metric/models/clouder_metric_value.py

100755100644
File mode changed.

clouder_metric/tests/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from . import test_clouder_metric_interface
6+
from . import test_clouder_metric_type

clouder_metric/tests/common.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from odoo.tests.common import TransactionCase
6+
7+
8+
class TestCommon(TransactionCase):
9+
10+
def setUp(self):
11+
super(self, TestCommon).__init__()
12+
self.metric_type = self.env['clouder.metric.type'].create({
13+
'name': 'Test Metric',
14+
'code': 'TEST',
15+
'metric_model': 'base',
16+
'uom_id': self.env.ref('product.uom_categ_wtime'),
17+
})
18+
self.metric_interface = self.env['clouder.metric.interface'].create({
19+
'type_id': self.metric_type.id,
20+
'metric_ref': 7,
21+
'source_id': None # @TODO
22+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from .common import TestCommon
6+
7+
8+
class TestClouderMetricInterface(TestCommon):
9+
10+
def test_metric_id(self):
11+
""" It should test to see that at least one metric_id is returned """
12+
self.assertTrue(len(self.metric_interface.metric_id) == 1)
13+
14+
def test_name_get(self):
15+
""" It should return the right name """
16+
exp = [
17+
(self.metric_interface.id, 'Test Metric - 7')
18+
]
19+
self.assertEqual(exp, self.metric_interface.name_get())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from .common import TestCommon
6+
7+
8+
class TestClouderMetricType(TestCommon):
9+
10+
def test_get_metric_models(self):
11+
""" It should have the correct metric model types """
12+
exp = [
13+
('clouder.base', 'Base'),
14+
('clouder.service', 'Service'),
15+
]
16+
self.assertEquals(exp, self.metric_type._get_metric_models())
17+

sale_clouder/__init__.py

100755100644
File mode changed.

sale_clouder/__openerp__.py sale_clouder/__mainifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Sale - Clouder",
66
"summary": "Provides the ability to sell Clouder instances.",
7-
"version": "9.0.1.0.0",
7+
"version": "10.0.1.0.0",
88
"category": "Clouder",
99
"website": "https://github.com/clouder-community/clouder",
1010
"author": "LasLabs",

sale_clouder/models/__init__.py

100755100644
File mode changed.

sale_clouder/models/clouder_contract.py

100755100644
+45-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright 2016 LasLabs Inc.
2+
# Copyright 2016-2017 LasLabs Inc.
33
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
44

55
import logging
@@ -23,6 +23,11 @@ class ClouderContract(models.Model):
2323
require=True,
2424
ondelete='restrict',
2525
)
26+
contract_line_ids = fields.One2many(
27+
string='Contract Lines',
28+
comodel_name='clouder.contract.line',
29+
inverse_name='contract_line_id'
30+
)
2631

2732
_sql_constraints = [
2833
('ref_contract_id_unique', 'UNIQUE(ref_contract_id)',
@@ -64,11 +69,11 @@ def get_invoice_line_quantity(self, account, account_line, invoice):
6469
)
6570
6671
Args:
67-
account: (account.analytic.account) Contract that recurring
72+
account (account.analytic.account): Contract that recurring
6873
invoice line belongs to. This is called
69-
account_line: (account.analytic.invoice.line) Recurring invoice
74+
account_line (account.analytic.invoice.line): Recurring invoice
7075
line being referenced.
71-
invoice: (account.invoice) Invoice that is being created.
76+
invoice (account.invoice): Invoice that is being created.
7277
Returns:
7378
(int) Quantity to use on invoice line in the UOM defined on the
7479
``contract_line``.
@@ -77,7 +82,6 @@ def get_invoice_line_quantity(self, account, account_line, invoice):
7782
invoice_policy = account_line.product_id.invoice_policy
7883
invoice_policy_map = self.invoice_policy_map
7984
try:
80-
invoice
8185
method = invoice_policy_map[invoice_policy]
8286
except KeyError:
8387
_logger.info(_(
@@ -94,7 +98,7 @@ def _create_default_vals(self, account):
9498
""" It returns default values to create and link new ClouderContracts.
9599
96100
Args:
97-
account: (account.analytic.account) Account that ClouderContract
101+
account (account.analytic.account): Account that ClouderContract
98102
will reference.
99103
Returns:
100104
(dict) Values fed to ``create`` in ``_get_contract_by_account``.
@@ -121,25 +125,52 @@ def _get_contract_by_account(self, account, create=False):
121125

122126
@api.multi
123127
def _get_quantity_flat(self, account_line, invoice):
124-
""" It returns the base quantity with no calculations """
128+
""" It returns the base quantity with no calculations
129+
Args:
130+
account_line (account.analytic.invoice.line): Recurring invoice
131+
line being referenced.
132+
invoice (account.invoice): Invoice that is being created.
133+
Returns:
134+
(float) Quantity with no calculations performed
135+
"""
125136
return account_line.quantity
126137

127138
@api.multi
128139
def _get_quantity_threshold(self, account_line, invoice):
129140
""" It functions like flat rate for the most part
130141
131142
Args:
132-
account_line: (account.analytic.invoice.line) Recurring invoice
143+
account_line (account.analytic.invoice.line): Recurring invoice
133144
line being referenced.
134-
invoice: (account.invoice) Invoice that is being created.
145+
invoice (account.invoice): Invoice that is being created.
135146
Returns:
136-
(int) Quantity to use on invoice line in the UOM defined on the
147+
(float) Quantity to use on invoice line in the UOM defined on the
137148
``contract_line``.
138149
"""
139-
raise NotImplementedError
140-
self.get_quantity_flat(account_line, invoice)
150+
return account_line.quantity
141151

142152
@api.multi
143153
def _get_quantity_usage(self, account_line, invoice):
144-
""" It provides a quantity based on unbilled and used metrics """
145-
raise NotImplementedError
154+
""" It provides a quantity based on unbilled and used metrics
155+
Args:
156+
account_line (account.analytic.invoice.line): Recurring invoice
157+
line being referenced.
158+
invoice (account.invoice): Invoice that is being created.
159+
Returns:
160+
(float) Quantity to use on invoice line in the UOM defined on the
161+
``contract_line``.
162+
"""
163+
usage = 0
164+
vals = account_line.metric_interface_id.metric_value_ids
165+
inv_date = invoice.date_invoice
166+
inv_delta = self.get_relative_delta(
167+
self.recurring_rule_type, self.recurring_interval
168+
)
169+
start_date = inv_date - inv_delta
170+
# Filter out metrics that fall within the billing period
171+
usage_values = vals.filtered(
172+
lambda x: x.date_start >= start_date and x.date_end <= inv_date
173+
)
174+
for val in usage_values:
175+
usage += val.value
176+
return usage

sale_clouder/models/clouder_contract_line.py

100755100644
+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class ClouderContractLine(models.Model):
1111
_name = 'clouder.contract.line'
1212
_inherits = {'account.analytic.invoice.line': 'contract_line_id'}
1313

14+
contact_id = fields.Many2one(
15+
string='Contract',
16+
comodel_name='clouder.contract',
17+
index=True,
18+
required=True,
19+
ondelete='restrict',
20+
)
1421
contract_line_id = fields.Many2one(
1522
string='Recurring Line',
1623
comodel_name='account.analytic.invoice.line',

sale_clouder/models/product_template.py

100755100644
File mode changed.

sale_clouder/tests/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from . import test_clouder_contract
6+
from . import test_product_template
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from odoo.tests.common import TransactionCase
6+
7+
8+
class TestClouderContract(TransactionCase):
9+
10+
def setUp(self):
11+
super(self, TestClouderContract).__init__()
12+
self.contract = self.env['clouder.contract']
13+
14+
def test(self):
15+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from odoo.tests.common import TransactionCase
6+
7+
8+
class TestProductTemplate(TransactionCase):
9+
10+
def test_invoice_policy(self):
11+
""" It should ensure the right options exist for invoice policy """
12+
policy = self.env['product.template']._fields['invoice_policy']
13+
exp = ('usage', 'threshold')
14+
res = []
15+
for item in policy.selection:
16+
res.append(item[0])
17+
for e in exp:
18+
self.assertTrue(e in res)

0 commit comments

Comments
 (0)