def _get_alias_values(self, type, alias_name=None): if not alias_name: alias_name = self.name if self.company_id != self.env.ref('base.main_company'): alias_name += '-' + str(self.company_id.name) try: remove_accents(alias_name).encode('ascii') except UnicodeEncodeError: try: remove_accents(self.code).encode('ascii') safe_alias_name = self.code except UnicodeEncodeError: safe_alias_name = self.type _logger.warning("Cannot use '%s' as email alias, fallback to '%s'", alias_name, safe_alias_name) alias_name = safe_alias_name return { 'alias_defaults': { 'move_type': type == 'purchase' and 'in_invoice' or 'out_invoice', 'company_id': self.company_id.id, 'journal_id': self.id }, 'alias_parent_thread_id': self.id, 'alias_name': alias_name, }
def set_shipper(self, company_partner, warehouse_partner): Contact = self.factory.Contact() Contact.PersonName = remove_accents( company_partner.name) if not company_partner.is_company else '' Contact.CompanyName = remove_accents( company_partner.commercial_company_name) or '' Contact.PhoneNumber = warehouse_partner.phone or '' # TODO fedex documentation asks for TIN number, but it seems to work without Address = self.factory.Address() Address.StreetLines = [ remove_accents(warehouse_partner.street) or '', remove_accents(warehouse_partner.street2) or '' ] Address.City = remove_accents(warehouse_partner.city) or '' if warehouse_partner.country_id.code in STATECODE_REQUIRED_COUNTRIES: Address.StateOrProvinceCode = warehouse_partner.state_id.code or '' else: Address.StateOrProvinceCode = '' Address.PostalCode = warehouse_partner.zip or '' Address.CountryCode = warehouse_partner.country_id.code or '' self.RequestedShipment.Shipper = self.factory.Party() self.RequestedShipment.Shipper.Contact = Contact self.RequestedShipment.Shipper.Address = Address
def shipping_charges_payment_ship_collect( self, picking, shipping_charges_payment_account): self.RequestedShipment.ShippingChargesPayment = self.factory.Payment() self.RequestedShipment.ShippingChargesPayment.PaymentType = 'THIRD_PARTY' Payor = self.factory.Payor() Payor.ResponsibleParty = self.factory.Party() Payor.ResponsibleParty.Contact = self.factory.Contact() Payor.ResponsibleParty.Contact.CompanyName = remove_accents( picking.sale_id.partner_id.name) or '' Payor.ResponsibleParty.Contact.PhoneNumber = picking.sale_id.partner_id.phone or '' Payor.ResponsibleParty.Address = self.factory.Address() Payor.ResponsibleParty.Address.StreetLines = [ remove_accents(picking.sale_id.partner_id.street) or '', remove_accents(picking.sale_id.partner_id.street2) or '' ] Payor.ResponsibleParty.Address.City = remove_accents( picking.sale_id.partner_id.city) or '' if picking.sale_id.partner_id.country_id.code in STATECODE_REQUIRED_COUNTRIES: Payor.ResponsibleParty.Address.StateOrProvinceCode = picking.sale_id.partner_id.state_id.code or '' else: Payor.ResponsibleParty.Address.StateOrProvinceCode = '' Payor.ResponsibleParty.Address.PostalCode = picking.sale_id.partner_id.zip or '' Payor.ResponsibleParty.Address.CountryCode = picking.sale_id.partner_id.country_id.code or '' Payor.ResponsibleParty.AccountNumber = shipping_charges_payment_account self.RequestedShipment.ShippingChargesPayment.Payor = Payor
def get_company_suffix(): if self.company_id != self.env.ref('base.main_company'): try: remove_accents(self.company_id.name).encode('ascii') return '-' + str(self.company_id.name) except UnicodeEncodeError: return '-' + str(self.company_id.id) return ''
def _sanitize_alias_name(name): """ Cleans and sanitizes the alias name """ sanitized_name = remove_accents(name).lower().split('@')[0] sanitized_name = re.sub(r'[^\w+.]+', '-', sanitized_name) sanitized_name = re.sub(r'^\.+|\.+$|\.+(?=\.)', '', sanitized_name) sanitized_name = sanitized_name.encode('ascii', errors='replace').decode() return sanitized_name
def _jitsi_sanitize_name(self, name): sanitized = re.sub(r'[^\w+.]+', '-', remove_accents(name).lower()) counter, sanitized_suffixed = 1, sanitized existing = self.env['chat.room'].search([('name', '=like', '%s%%' % sanitized)]).mapped('name') while sanitized_suffixed in existing: sanitized_suffixed = '%s-%d' % (sanitized, counter) counter += 1 return sanitized_suffixed
def _compute_caps(self): for rec in self: alpha_count = sum(1 for c in rec.name if c.upper() in ascii_uppercase) rec.caps_count = sum(1 for c in rec.name if c.isupper()) rec.caps_ratio = rec.caps_count / alpha_count # Create a normalized name to easilly find duplicates s_name = remove_accents(rec.name.lower()) rec.normalized_name = ''.join(c for c in s_name if c in ascii_lowercase)
def set_recipient(self, recipient_partner): Contact = self.factory.Contact() if recipient_partner.is_company: Contact.PersonName = '' Contact.CompanyName = remove_accents(recipient_partner.name) else: Contact.PersonName = remove_accents(recipient_partner.name) Contact.CompanyName = remove_accents( recipient_partner.commercial_company_name) or '' Contact.PhoneNumber = recipient_partner.phone or '' Address = self.factory.Address() Address.StreetLines = [ remove_accents(recipient_partner.street) or '', remove_accents(recipient_partner.street2) or '' ] Address.City = remove_accents(recipient_partner.city) or '' if recipient_partner.country_id.code in STATECODE_REQUIRED_COUNTRIES: Address.StateOrProvinceCode = recipient_partner.state_id.code or '' else: Address.StateOrProvinceCode = '' Address.PostalCode = recipient_partner.zip or '' Address.CountryCode = recipient_partner.country_id.code or '' self.RequestedShipment.Recipient = self.factory.Party() self.RequestedShipment.Recipient.Contact = Contact self.RequestedShipment.Recipient.Address = Address
def _get_alias_values(self, type, alias_name=None): """ This function verifies that the user-given mail alias (or its fallback) doesn't contain non-ascii chars. The fallbacks are the journal's name, code, or type - these are suffixed with the company's name or id (in case the company's name is not ascii either). """ def get_company_suffix(): if self.company_id != self.env.ref('base.main_company'): try: remove_accents(self.company_id.name).encode('ascii') return '-' + str(self.company_id.name) except UnicodeEncodeError: return '-' + str(self.company_id.id) return '' if not alias_name: alias_name = self.name alias_name += get_company_suffix() try: remove_accents(alias_name).encode('ascii') except UnicodeEncodeError: try: remove_accents(self.code).encode('ascii') safe_alias_name = self.code except UnicodeEncodeError: safe_alias_name = self.type safe_alias_name += get_company_suffix() _logger.warning("Cannot use '%s' as email alias, fallback to '%s'", alias_name, safe_alias_name) alias_name = safe_alias_name return { 'alias_defaults': { 'move_type': type == 'purchase' and 'in_invoice' or 'out_invoice', 'company_id': self.company_id.id, 'journal_id': self.id }, 'alias_parent_thread_id': self.id, 'alias_name': alias_name, }
def _clean_and_check_unique(self, names): """When an alias name appears to already be an email, we keep the local part only. A sanitizing / cleaning is also performed on the name. If name already exists an UserError is raised. """ sanitized_names = [] for name in names: sanitized_name = remove_accents(name).lower().split('@')[0] sanitized_name = re.sub(r'[^\w+.]+', '-', sanitized_name) sanitized_name = sanitized_name.encode('ascii', errors='replace').decode() sanitized_names.append(sanitized_name) catchall_alias = self.env['ir.config_parameter'].sudo().get_param('mail.catchall.alias') bounce_alias = self.env['ir.config_parameter'].sudo().get_param('mail.bounce.alias') domain = [('alias_name', 'in', sanitized_names)] if self: domain += [('id', 'not in', self.ids)] if any(sanitized_name in [catchall_alias, bounce_alias] for sanitized_name in sanitized_names) or self.search_count(domain): raise UserError(_('The e-mail alias is already used. Please enter another one.')) return sanitized_names
def set_recipient(self, recipient_partner, attn=None, residential=False): """ Adds ATTN: and sanitizes against known 'illegal' common characters in names. :param recipient_partner: default :param attn: NEW add to contact name as an ' ATTN: $attn' :param residential: NEW allow ground home delivery :return: """ Contact = self.factory.Contact() if recipient_partner.is_company: Contact.PersonName = '' Contact.CompanyName = sanitize_name( remove_accents(recipient_partner.name)) else: Contact.PersonName = sanitize_name( remove_accents(recipient_partner.name)) Contact.CompanyName = sanitize_name( remove_accents(recipient_partner.parent_id.name)) or '' if attn: Contact.PersonName = Contact.PersonName + ' ATTN: ' + str(attn) Contact.PhoneNumber = recipient_partner.phone or '' Address = self.factory.Address() Address.StreetLines = [ remove_accents(recipient_partner.street) or '', remove_accents(recipient_partner.street2) or '' ] Address.City = remove_accents(recipient_partner.city) or '' if recipient_partner.country_id.code in STATECODE_REQUIRED_COUNTRIES: Address.StateOrProvinceCode = recipient_partner.state_id.code or '' else: Address.StateOrProvinceCode = '' Address.PostalCode = recipient_partner.zip or '' Address.CountryCode = recipient_partner.country_id.code or '' if residential: Address.Residential = True self.RequestedShipment.Recipient = self.factory.Party() self.RequestedShipment.Recipient.Contact = Contact self.RequestedShipment.Recipient.Address = Address
def test_non_latin(self): self.assertEqual(remove_accents('العربية'), 'العربية') self.assertEqual(remove_accents('русский алфавит'), 'русскии алфавит')
def test_latin(self): self.assertEqual(remove_accents('Niño Hernández'), 'Nino Hernandez') self.assertEqual(remove_accents('Anaïs Clémence'), 'Anais Clemence')
def test_empty_string(self): self.assertEqual(remove_accents(False), False) self.assertEqual(remove_accents(''), '') self.assertEqual(remove_accents(None), None)
def _clean_and_make_unique(self, name, alias_ids=False): # when an alias name appears to already be an email, we keep the local part only name = remove_accents(name).lower().split('@')[0] name = re.sub(r'[^\w+.]+', '-', name) return self._find_unique(name, alias_ids=alias_ids)
def _jitsi_sanitize_name(self, name): return re.sub(r'[^\w+.]+', '-', remove_accents(name).lower())