<commit_before># -*- encoding: utf-8 -*-
"""
Customizes party address to have address in correct format for Endicia API .
"""
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

import string

from endicia import FromAddress, ToAddress
from trytond.pool import PoolMeta

__all__ = ['Address']
__metaclass__ = PoolMeta


class Address:
    '''
    Address
    '''
    __name__ = "party.address"

    def address_to_endicia_from_address(self):
        '''
        Converts party address to Endicia From Address.

        :param return: Returns instance of FromAddress
        '''
        phone = self.party.phone
        if phone:
            # Remove the special characters in the phone if any
            phone = "".join([char for char in phone if char in string.digits])
        return FromAddress(
            FromName=self.name or self.party.name,
            # FromCompany = user_rec.company.name or None,
            ReturnAddress1=self.street,
            ReturnAddress2=self.streetbis,
            ReturnAddress3=None,
            ReturnAddress4=None,
            FromCity=self.city,
            FromState=self.subdivision and self.subdivision.code[3:],
            FromPostalCode=self.zip[:5],
            FromPhone=phone and phone[-10:],
            FromEMail=self.party.email,
        )

    def address_to_endicia_to_address(self):
        '''
        Converts party address to Endicia To Address.

        :param return: Returns instance of ToAddress
        '''
        phone = self.party.phone
        if phone:
            # Remove the special characters in the phone if any
            phone = "".join([char for char in phone if char in string.digits])
            if self.country and self.country.code != 'US':
                phone = phone[-30:]
            else:
                phone = phone[-10:]
        return ToAddress(
            ToName=self.name or self.party.name,
            ToCompany=self.name or self.party.name,
            ToAddress1=self.street,
            ToAddress2=self.streetbis,
            ToAddress3=None,
            ToAddress4=None,
            ToCity=self.city,
            ToState=self.subdivision and self.subdivision.code[3:],
            ToPostalCode=self.zip[:5],
            ToCountry=self.country and self.country.name,
            ToCountryCode=self.country and self.country.code,
            ToPhone=phone,<commit_msg>Fixed 'Russian Federation' issue with Endicia<commit_after># -*- encoding: utf-8 -*-
"""
Customizes party address to have address in correct format for Endicia API .
"""
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

import string

from endicia import FromAddress, ToAddress
from trytond.pool import PoolMeta

__all__ = ['Address']
__metaclass__ = PoolMeta


class Address:
    '''
    Address
    '''
    __name__ = "party.address"

    def address_to_endicia_from_address(self):
        '''
        Converts party address to Endicia From Address.

        :param return: Returns instance of FromAddress
        '''
        phone = self.party.phone
        if phone:
            # Remove the special characters in the phone if any
            phone = "".join([char for char in phone if char in string.digits])
        return FromAddress(
            FromName=self.name or self.party.name,
            # FromCompany = user_rec.company.name or None,
            ReturnAddress1=self.street,
            ReturnAddress2=self.streetbis,
            ReturnAddress3=None,
            ReturnAddress4=None,
            FromCity=self.city,
            FromState=self.subdivision and self.subdivision.code[3:],
            FromPostalCode=self.zip[:5],
            FromPhone=phone and phone[-10:],
            FromEMail=self.party.email,
        )

    def address_to_endicia_to_address(self):
        '''
        Converts party address to Endicia To Address.

        :param return: Returns instance of ToAddress
        '''
        phone = self.party.phone
        if phone:
            # Remove the special characters in the phone if any
            phone = "".join([char for char in phone if char in string.digits])
            if self.country and self.country.code != 'US':
                phone = phone[-30:]
            else:
                phone = phone[-10:]
        to_country = self.country and self.country.name
        if self.country and self.country.code == 'RU':
            # Ugly hack to fix `Russian Federation` issue
            # Endicia don't have any country called `Russian Federation`
            # they call it `Russia`
            to_country = 'Russia'
        return ToAddress(
            ToName=self.name or self.party.name,
            ToCompany=self.name or self.party.name,
            ToAddress1=self.street,
            ToAddress2=self.streetbis,
            ToAddress3=None,
            ToAddress4=None,
            ToCity=self.city,
            ToState=self.subdivision and self.subdivision.code[3:],
            ToPostalCode=self.zip[:5],
            ToCountry=to_country,
            ToCountryCode=self.country and self.country.code,
            ToPhone=phone,
{'fraction_non_alphanumeric': 0.0853008377760853, 'fraction_numerical': 0.0072353389185072356, 'mean_word_length': 2.0381723539618277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7495677', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1383, 'n_words': 414}