Skip to content

Commit

Permalink
Force package creation when post info is retrived from correios
Browse files Browse the repository at this point in the history
  • Loading branch information
Corcioli committed Jul 11, 2018
1 parent 416182c commit abdaf6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 11 additions & 7 deletions correios/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ def build_zip_address(self, zip_address_data):
)
return zip_address

def build_post_info(self, data, user: User) -> PostInfo:
def build_post_info(self, data, user: User, validate_package: bool = True) -> PostInfo:
post_info = PostInfo(
postal_unit=self.build_postal_unit(data.plp),
posting_list=self._load_posting_list(
data=data,
user=user
user=user,
validate_package=validate_package
),
value=data.plp.valor_global
)
Expand All @@ -157,7 +158,7 @@ def build_postal_unit(self, data) -> PostalUnit:
)
return postal_unit

def _load_posting_list(self, data, user: User) -> PostingList:
def _load_posting_list(self, data, user: User, validate_package: bool = True) -> PostingList:
contract_number = to_integer(data.remetente.numero_contrato)

contract = next(
Expand All @@ -179,6 +180,7 @@ def _load_posting_list(self, data, user: User) -> PostingList:
data=postal_object,
posting_card=posting_card,
sender_address=self._load_sender_address(data.remetente),
validate_package=validate_package
))

posting_list.close_with_id(data.plp.id_plp)
Expand All @@ -190,6 +192,7 @@ def _load_shipping_label(
data,
posting_card: PostingCard,
sender_address: SenderAddress,
validate_package: bool = True
) -> ShippingLabel:

declared_value = getattr(
Expand Down Expand Up @@ -217,7 +220,7 @@ def _load_shipping_label(
posting_card=posting_card,
sender=sender_address,
receiver=self._load_receiver_address(data),
package=self._load_package(data),
package=self._load_package(data, validate_package=validate_package),
service=Service.get(data.codigo_servico_postagem.text),
tracking_code=data.numero_etiqueta.text,
receipt=self.build_receipt(data)
Expand Down Expand Up @@ -266,7 +269,7 @@ def _load_receiver_address(self, data) -> ReceiverAddress:

return receiver_address

def _load_package(self, data) -> Package:
def _load_package(self, data, validate_package) -> Package:
dimensions = data.dimensao_objeto

type_ = dimensions.tipo_objeto.text.strip()
Expand All @@ -288,7 +291,8 @@ def _load_package(self, data) -> Package:
weight=float(data.peso.text.replace(',', '.')),
width=float(dimensions.dimensao_largura.text.replace(',', '.')),
package_type=dimensions.tipo_objeto,
service=data.codigo_servico_postagem.text
service=data.codigo_servico_postagem.text,
validate_package=validate_package
)

return package
Expand Down Expand Up @@ -609,7 +613,7 @@ def get_post_info(self, number: int) -> PostInfo:
posting_card_number=posting_card_number
)

return self.model_builder.build_post_info(data=data, user=user)
return self.model_builder.build_post_info(data=data, user=user, validate_package=False)

def _generate_xml_string(self, posting_list: PostingList) -> str:
posting_list_serializer = PostingListSerializer()
Expand Down
6 changes: 4 additions & 2 deletions correios/models/posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,14 @@ def __init__(self,
diameter: Union[float, int] = 0, # cm
weight: Union[float, int] = 0, # g
sequence=(1, 1),
service: Optional[Union[Service, str, int]] = None) -> None:
service: Optional[Union[Service, str, int]] = None,
validate_package: bool = True) -> None:

if service:
service = Service.get(service)

Package.validate(package_type, width, height, length, diameter, service, weight)
if validate_package:
Package.validate(package_type, width, height, length, diameter, service, weight)

if len(sequence) != 2 or sequence[0] > sequence[1]:
raise exceptions.InvalidPackageSequenceError("Package must be a tuple with 2 elements: (number, total)")
Expand Down

0 comments on commit abdaf6d

Please sign in to comment.