diff --git a/passerelle_imio_extra_fees/models.py b/passerelle_imio_extra_fees/models.py index 668df2f..434d8a4 100644 --- a/passerelle_imio_extra_fees/models.py +++ b/passerelle_imio_extra_fees/models.py @@ -17,7 +17,6 @@ # along with this program. If not, see . - from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.utils.encoding import force_text @@ -28,20 +27,25 @@ from passerelle.compat import json_loads from passerelle.utils.api import endpoint from decimal import Decimal + class ExtraFees(BaseResource): category = _('Misc') PROFILE_CHOICES = ( - ('DEFAULT','Default'), - ('NAMUR','Namur'), + ('DEFAULT', 'Default'), + ('NAMUR', 'Namur'), ) profile = models.CharField(max_length=30, choices=PROFILE_CHOICES, default='Default') - max_doc_in_letter = models.PositiveSmallIntegerField(verbose_name=_('Maximum documents in one letter'), - default=5, - validators=[MaxValueValidator(100), MinValueValidator(1)]) - belgium_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for belgium'), default=0.00) - europe_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for europe'), default=0.00) - world_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for rest of the world'), default=0.00) + max_doc_in_letter = models.PositiveSmallIntegerField(verbose_name=_('Maximum documents in one letter'), + default=5, + validators=[MaxValueValidator(100), MinValueValidator(1)]) + belgium_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, + verbose_name=_('Postage fees for belgium'), default=0.00) + europe_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for europe'), + default=0.00) + world_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, + verbose_name=_('Postage fees for rest of the world'), default=0.00) + # free_postage_exeptions_list_doc ? class Meta: @@ -51,7 +55,6 @@ class ExtraFees(BaseResource): def get_connector_slug(cls): return 'extra-fees' - # Minimum requirement in request to compute basket with postage fee # nb_documents : Number of desired documents for one citizen posted form. # postage_fee : THanks to citizen choice (Be/Eu/Wrld), Postage Fee value for this form. @@ -63,16 +66,17 @@ class ExtraFees(BaseResource): for basket_item in data['data']: try: nb_documents += int(basket_item['request_data']['nb_documents']) - destination = basket_item['request_data']['destination'] # Passed in the workflow webservice + destination = basket_item['request_data']['destination'] # Passed in the workflow webservice # No postage fees if send by mail - if destination and 'mail' in destination: + if destination and 'mail' in destination: postage_fee = Decimal(0) if Decimal(basket_item['request_data']['postage_fee']) > postage_fee: postage_fee = Decimal(basket_item['request_data']['postage_fee']) except KeyError: # basket item not associated with any document, no fee pass - nb_letter = int(nb_documents / int(self.max_doc_in_letter)) + (((nb_documents % int(self.max_doc_in_letter)) > 0) and 1 or 0) + nb_letter = int(nb_documents / int(self.max_doc_in_letter)) + ( + ((nb_documents % int(self.max_doc_in_letter)) > 0) and 1 or 0) postage_fee = nb_letter * postage_fee return {'data': [{'subject': force_text(_('Postage')), 'amount': str(postage_fee)}]} @@ -100,7 +104,9 @@ class ExtraFees(BaseResource): for basket_item in data['data']: try: nb_documents += int(basket_item['request_data']['nb_documents']) - if Decimal(basket_item['request_data']['country_price']) > postage_fee and 'duplicata-de-livret-de-mariage' not in basket_item['request_data']['form_slug']: + if Decimal(basket_item['request_data'][ + 'country_price']) > postage_fee and 'duplicata-de-livret-de-mariage' not in \ + basket_item['request_data']['form_slug']: postage_fee = Decimal(basket_item['request_data']['country_price']) except KeyError: # basket item not associated with any document, no fee @@ -110,7 +116,7 @@ class ExtraFees(BaseResource): postage_fee = Decimal('0.00') else: if changement_adresse_exception == True and nb_documents > 1: - nb_documents = nb_documents -1 + nb_documents = nb_documents - 1 nb_letter = int(nb_documents / max_doc_in_letter) + (((nb_documents % max_doc_in_letter) > 0) and 1 or 0) postage_fee = nb_letter * postage_fee if duplicata_exception == True: @@ -129,21 +135,22 @@ class ExtraFees(BaseResource): # So, we need to keep 0.74 and 1.13 like id and use fee key to get real fee value. @endpoint() def namur_fees(self, request): - return {'data':[{'id':'0.74','fee':self.belgium_postage_fee,'text':'En Belgique'}, - {'id':'1.13','fee':self.europe_postage_fee,'text':'À l\'étranger'}]} + return {'data': [{'id': '0.74', 'fee': self.belgium_postage_fee, 'text': 'En Belgique'}, + {'id': '1.13', 'fee': self.europe_postage_fee, 'text': 'À l\'étranger'}]} # wcs : new webservice call : https://[COMMUNE]-passerelle.guichet-citoyen.be/extra-fees/[CONNECTOR-SLUG]/fees # datasources : webservice.fees.get('belgium') @endpoint() def fees(self, request): - return {'belgium':self.belgium_postage_fee, - 'europe':self.europe_postage_fee, - 'world' :self.world_postage_fee} + return {'belgium': self.belgium_postage_fee, + 'europe': self.europe_postage_fee, + 'world': self.world_postage_fee} @endpoint() def destination_choices(self, request, q=None, **kwargs): - destination_choices = {'data':[{'id':'belgium', 'text':'En belgique', 'fee':self.belgium_postage_fee}, - {'id':'europe', 'text':'En europe', 'fee':self.europe_postage_fee}, - {'id':'world', 'text':'Dans le reste du monde', 'value':self.world_postage_fee} - ]} + destination_choices = {'data': [{'id': 'belgium', 'text': 'En belgique', 'fee': self.belgium_postage_fee}, + {'id': 'europe', 'text': 'En europe', 'fee': self.europe_postage_fee}, + {'id': 'world', 'text': 'Dans le reste du monde', + 'value': self.world_postage_fee} + ]} return destination_choices