19.0 vanilla

This commit is contained in:
Ernad Husremovic 2026-03-09 09:30:07 +01:00
parent ba20ce7443
commit 768b70e05e
2357 changed files with 1057103 additions and 712486 deletions

View file

@ -1,28 +1,8 @@
from itertools import zip_longest
import requests
from urllib3.util.ssl_ import create_urllib3_context
def calc_check_digits(number: str) -> str:
"""Calculate the extra digits that should be appended to the number to make it a valid number.
Source: python-stdnum iso7064.mod_97_10.calc_check_digits
"""
number_base10 = ''.join(str(int(x, 36)) for x in number)
checksum = int(number_base10) % 97
return '%02d' % ((98 - 100 * checksum) % 97)
def format_rf_reference(number: str) -> str:
"""Format a string into a Structured Creditor Reference.
The Creditor Reference is an international standard (ISO 11649).
Example: `123456789` -> `RF18 1234 5678 9`
"""
check_digits = calc_check_digits('{}RF'.format(number))
return 'RF{} {}'.format(
check_digits,
" ".join("".join(x) for x in zip_longest(*[iter(str(number))]*4, fillvalue=""))
)
from .structured_reference import *
from .dict_to_xml import dict_to_xml
class LegacyHTTPAdapter(requests.adapters.HTTPAdapter):

View file

@ -1,73 +0,0 @@
from types import SimpleNamespace
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import pkcs12
from OpenSSL import crypto
def load_key_and_certificates(content, password):
private_key, certificate, _dummy = pkcs12.load_key_and_certificates(content, password, backend=default_backend())
def public_key():
public_key = certificate.public_key()
def public_numbers():
public_numbers = public_key.public_numbers()
return SimpleNamespace(
n=public_numbers.n,
e=public_numbers.e,
)
return SimpleNamespace(
public_numbers=public_numbers,
public_bytes=public_key.public_bytes,
)
simple_private_key = SimpleNamespace(
sign=private_key.sign,
private_bytes=private_key.private_bytes,
)
simple_certificate = SimpleNamespace(
fingerprint=certificate.fingerprint,
issuer=SimpleNamespace(
rfc4514_string=certificate.issuer.rfc4514_string,
rdns=[
SimpleNamespace(rfc4514_string=item.rfc4514_string)
for item in certificate.issuer.rdns
],
get_attributes_for_oid=lambda oid: [
SimpleNamespace(value=item.value)
for item in certificate.issuer.get_attributes_for_oid(oid)
]
),
subject=SimpleNamespace(
rfc4514_string=certificate.subject.rfc4514_string,
rdns=[
SimpleNamespace(rfc4514_string=item.rfc4514_string)
for item in certificate.subject.rdns
],
get_attributes_for_oid=lambda oid: [
SimpleNamespace(value=item.value)
for item in certificate.subject.get_attributes_for_oid(oid)
]
),
not_valid_after=certificate.not_valid_after,
not_valid_before=certificate.not_valid_before,
public_key=public_key,
public_bytes=certificate.public_bytes,
serial_number=certificate.serial_number,
)
return simple_private_key, simple_certificate
def crypto_load_certificate(cer_pem):
certificate = crypto.load_certificate(crypto.FILETYPE_PEM, cer_pem)
simple_certificate = SimpleNamespace(
get_notAfter=certificate.get_notAfter,
get_notBefore=certificate.get_notBefore,
get_serial_number=certificate.get_serial_number,
get_subject=lambda: SimpleNamespace(
CN=certificate.get_subject().CN,
serialNumber=certificate.get_subject().serialNumber,
),
)
return simple_certificate

View file

@ -0,0 +1,102 @@
from lxml import etree
from odoo.tools.xml_utils import remove_control_characters
def dict_to_xml(node, *, nsmap={}, template=None, render_empty_nodes=False, tag=None, path=None):
""" Helper to render a Python dict as an XML node.
The dict is expected to be of the form:
{
# Special keys:
'_tag': 'tag_name', # '_tag' is rendered as the node's tag
'_text': 'content', # '_text' is rendered as the node's text content
'_dummy': 'dummy_value', # Keys starting with '_' are not rendered
# Simple values are rendered as attributes
'attribute_name': 'attribute_value',
# Dicts are rendered as child nodes
'child_tag': {
'_text': 'content',
'attribute_name': 'attribute_value',
},
# Lists of dicts are also rendered as child nodes
'child_tag': [
{
'_text': 'content',
'attribute_name': 'attribute_value',
},
],
}
:param node: The Python dict to render.
:param nsmap: (optional) A dict of namespaces to be used for rendering the node.
:param template: (optional) A Python dict providing default values and an order of keys for rendering the node.
:param render_empty_nodes: (optional) If True, empty nodes will be rendered in the XML tree.
:param tag: (optional) The tag of the node to render (needed only for recursive calls).
:param path: (optional) The path of the currently rendered node in the XML tree (needed only for recursive calls).
:return: The rendered XML node as an lxml.Element.
"""
def convert_tag_to_lxml_convention(tag):
if ':' in tag:
namespace, local_name = tag.split(':')
if namespace in nsmap:
return etree.QName(nsmap[namespace], local_name).text
return tag
if template is not None:
# Ensure order of keys
node = dict.fromkeys(template) | node
tag = node.get('_tag') or (template or {}).get('_tag', tag)
if tag is None:
raise ValueError(f"No tag was specified for node: {str(node)[:20]}")
if path is None:
path = tag
element = etree.Element(convert_tag_to_lxml_convention(tag), nsmap=nsmap)
# Add attributes
for attr_name, attr_value in node.items():
if not attr_name.startswith('_') and not isinstance(attr_value, (dict, list)) and attr_value is not None and attr_value is not False:
element.set(convert_tag_to_lxml_convention(attr_name), str(attr_value))
# Add text content if present
text = node.get('_text')
if text is not None and text is not False:
element.text = remove_control_characters(str(text).encode()).decode()
# Add child nodes
for child_tag, child in node.items():
if not child_tag.startswith('_') and isinstance(child, (dict, list)):
child_template = (template or {}).get(child_tag)
child_is_empty = True
if isinstance(child, dict):
child = [child]
# child is a list (of dicts)
for sub_child in child:
if sub_child is not None:
child_element = dict_to_xml(
sub_child,
nsmap=nsmap,
template=child_template,
render_empty_nodes=render_empty_nodes,
tag=child_tag,
path=f'{path}/{child_tag}',
)
if child_element is not None:
element.append(child_element)
child_is_empty = False
# Check that all non-empty child nodes are defined in the template
if template is not None and child_tag not in template and not child_is_empty:
raise ValueError(f"The following child node is not defined in the template: {path}/{child_tag}")
if not render_empty_nodes and not element.attrib and not element.text and len(element) == 0:
return None
return element

View file

@ -0,0 +1,192 @@
import re
from itertools import zip_longest
from stdnum import iso11649, luhn
from stdnum.iso7064 import mod_97_10
def sanitize_structured_reference(reference):
"""Removes whitespace and specific characters from Belgian structured references:
Example: ` RF18 1234 5678 9 ` -> `RF18123456789`
`+++020/3430/57642+++` -> `020343057642`
`***020/3430/57642***` -> `020343057642`
"""
ref = re.sub(r'\s', '', reference)
if re.fullmatch(r'(\+{3}|\*{3}|)\d{3}/\d{4}/\d{5}\1', ref):
return re.sub(r'[+*/]', '', ref)
return ref
def format_structured_reference_iso(number):
"""Format a string into a Structured Creditor Reference.
The Creditor Reference is an international standard (ISO 11649).
Example: `123456789` -> `RF18 1234 5678 9`
"""
check_digits = mod_97_10.calc_check_digits(f"{number}RF")
return 'RF{} {}'.format(
check_digits,
' '.join(''.join(x) for x in zip_longest(*[iter(str(number))]*4, fillvalue=''))
)
def is_valid_structured_reference_iso(reference):
"""Check whether the provided reference is a valid Structured Creditor Reference (ISO).
:param reference: the reference to check
"""
ref = sanitize_structured_reference(reference)
return iso11649.is_valid(ref)
def is_valid_structured_reference_be(reference):
"""Check whether the provided reference is a valid structured reference for Belgium.
:param reference: the reference to check
"""
ref = sanitize_structured_reference(reference)
be_ref = re.fullmatch(r'(\d{10})(\d{2})', ref)
return be_ref and int(be_ref.group(1)) % 97 == int(be_ref.group(2)) % 97
def is_valid_structured_reference_fi(reference):
"""Check whether the provided reference is a valid structured reference for Finland.
:param reference: the reference to check
"""
ref = sanitize_structured_reference(reference)
fi_ref = re.fullmatch(r'(\d{1,19})(\d)', ref)
if not fi_ref:
return False
total = sum((7, 3, 1)[idx % 3] * int(val) for idx, val in enumerate(fi_ref.group(1)[::-1]))
check_digit = (10 - (total % 10)) % 10
return check_digit == int(fi_ref.group(2))
def is_valid_structured_reference_no_se(reference):
"""Check whether the provided reference is a valid structured reference for Norway or Sweden.
:param reference: the reference to check
"""
ref = sanitize_structured_reference(reference)
no_se_ref = re.fullmatch(r'\d+', ref)
return no_se_ref and luhn.is_valid(ref)
def is_valid_structured_reference_nl(reference):
""" Generates a valid Dutch structured payment reference (betalingskenmerk)
by ensuring it follows the correct format.
Valid reference lengths:
- 7 digits: Simple reference with no check digit.
- 9-14 digits: Includes a check digit and a length code.
- 16 digits: Contains only a check digit, commonly used for wire transfers.
:param reference: the reference to check
:return: True if reference is a structured reference, False otherwise
"""
sanitized_reference = sanitize_structured_reference(reference)
if re.fullmatch(r'\d{7}', sanitized_reference):
return True
if not re.fullmatch(r'\d{9,16}', sanitized_reference):
return False
if len(sanitized_reference) == 15:
return False
check, reference_to_check = sanitized_reference[0], sanitized_reference[1:]
weigths = [2, 4, 8, 5, 10, 9, 7, 3, 6, 1]
reference_to_check = reference_to_check.zfill(16)[::-1]
total = sum(
int(digit) * weigths[index % len(weigths)]
for index, digit in enumerate(reference_to_check)
)
computed_check = 11 - (total % 11)
if computed_check == 11:
computed_check = 0
elif computed_check == 10:
computed_check = 1
return computed_check == int(check)
def is_valid_structured_reference_si(reference):
""" Validates a Slovenian structured reference using Model 01 (SI01).
Format: SI01 (P1-P2-P3)K
- Starts with 'SI01'
- P1, P2, P3 are numeric segments (max 20 digits total, up to 2 hyphens)
- K is a check digit calculated using MOD 11
:param reference: the reference to check
:return: True if reference is a structured reference, False otherwise
"""
sanitized_reference = sanitize_structured_reference(reference)
if sanitized_reference.startswith('SI01'):
sanitized_reference = sanitized_reference[4:] # Remove SI01
else:
return False
# Contains maximum of two hyphens
if sanitized_reference.count('-') > 2:
return False
# Validate hyphenated parts using regex: 3 numeric parts (last ends with check digit)
match = re.match(r'^(\d+)-(\d+)-(\d+)$', sanitized_reference)
if not match:
return False
# Split into main digits and check digit
core = sanitized_reference.replace('-', '')
if not core.isdigit() or len(core) < 2:
return False
digits, given_check_digit = core[:-1], core[-1]
weights = list(range(2, 14))
weights = weights[0:len(digits)]
weighted_sum = sum(int(d) * w for d, w in zip(reversed(digits), weights))
expected_check_digit = 11 - (weighted_sum % 11)
if expected_check_digit in (10, 11):
expected_check_digit = 0
return given_check_digit == str(expected_check_digit)
def is_valid_structured_reference(reference):
"""Check whether the provided reference is a valid structured reference.
This is currently supporting SEPA enabled countries. More specifically countries covered by functions in this file.
:param reference: the reference to check
"""
reference = sanitize_structured_reference(reference or '')
return (
is_valid_structured_reference_be(reference) or
is_valid_structured_reference_fi(reference) or
is_valid_structured_reference_no_se(reference) or
is_valid_structured_reference_si(reference) or
is_valid_structured_reference_nl(reference) or
is_valid_structured_reference_iso(reference)
) if reference else False
def is_valid_structured_reference_for_country(reference, country_code=''):
"""Check the validity of the reference's structure for a specific country or ISO 11649 as a fallback.
:param reference: the reference to check
:param country_code: the country code to check against
:return: True if reference is a structured reference for the given country or ISO 11649, False otherwise
"""
check_per_country = {
'BE': is_valid_structured_reference_be,
'FI': is_valid_structured_reference_fi,
'NO': is_valid_structured_reference_no_se,
'SE': is_valid_structured_reference_no_se,
'NL': is_valid_structured_reference_nl,
'SI': is_valid_structured_reference_si,
}
reference = sanitize_structured_reference(reference or '')
if check := check_per_country.get(country_code.upper()):
return check(reference)
return is_valid_structured_reference_iso(reference)