summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2017-05-31 07:52:16 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2017-05-31 07:52:16 (GMT)
commite8435bf3098b29955552f9cd918b22fdb071b1ea (patch)
tree3f94f8794dced959c67e9bec6c00c2a4362600e5
parent8c85cfaacc1f6d814fdf5358bf88f7f0743a4dfd (diff)
downloadpasserelle-imio-extra-fees-e8435bf3098b29955552f9cd918b22fdb071b1ea.zip
passerelle-imio-extra-fees-e8435bf3098b29955552f9cd918b22fdb071b1ea.tar.gz
passerelle-imio-extra-fees-e8435bf3098b29955552f9cd918b22fdb071b1ea.tar.bz2
add code to compute fee based on the number of documents
-rw-r--r--passerelle_imio_extra_fees/models.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/passerelle_imio_extra_fees/models.py b/passerelle_imio_extra_fees/models.py
index 0a38742..99b2e31 100644
--- a/passerelle_imio_extra_fees/models.py
+++ b/passerelle_imio_extra_fees/models.py
@@ -14,9 +14,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import json
+
+from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
+from passerelle.utils.api import endpoint
class ExtraFees(BaseResource):
@@ -28,3 +32,20 @@ class ExtraFees(BaseResource):
@classmethod
def get_connector_slug(cls):
return 'extra-fees'
+
+ @endpoint(methods=['post'])
+ def compute(self, request, **kwargs):
+ data = json.loads(request.body)
+
+ nb_documents = 0
+ for basket_item in data['data']:
+ try:
+ nb_documents += int(basket_item['request_data']['nb_documents'])
+ except KeyError:
+ # basket item not associated with any document, no fee
+ pass
+
+ # compute fee
+ postage_fee = 3 + nb_documents * 1
+
+ return {'data': [{'subject': force_text(_('Postage')), 'amount': str(postage_fee)}]}