From 0da962e0590780f698690a0fe8d6ea6e5534b3cc Mon Sep 17 00:00:00 2001 From: Anatoliy Udarczev Date: Tue, 15 Oct 2024 15:26:56 +0300 Subject: [PATCH] Add custom namespaces definitions --- tests/test_util.py | 15 +++++++++++++++ wsgidav/util.py | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 5f1c6c4..3f1d27c 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -7,10 +7,13 @@ import logging.handlers import sys import unittest +from lxml import etree from io import StringIO +from wsgidav import xml_tools from wsgidav.util import ( BASE_LOGGER_NAME, + add_property_response, check_tags, checked_etag, deep_update, @@ -25,6 +28,7 @@ pop_path, removeprefix, shift_path, + to_str, update_headers_in_place, ) @@ -180,6 +184,17 @@ def testBasics(self): assert get_dict_value(d, "c", as_dict=True) is False assert get_dict_value(d, "x", as_dict=True) == {} self.assertRaises(KeyError, get_dict_value, d, "x", as_dict=False) + + multistatus_el = xml_tools.make_multistatus_el() + add_property_response(multistatus_el, "", [ + ('{custom}name', etree.Element("{custom}name", nsmap={"C": "custom"})) + ]) + assert to_str(xml_tools.xml_to_bytes(multistatus_el, pretty=False)) == ( + '\n' + '' + 'HTTP/1.1 200 OK' + '' + ) class LoggerTest(unittest.TestCase): diff --git a/wsgidav/util.py b/wsgidav/util.py index 4a0e208..2355811 100644 --- a/wsgidav/util.py +++ b/wsgidav/util.py @@ -1306,7 +1306,7 @@ def add_property_response(multistatus_elem, href, prop_list): """ # Split prop_list by status code and build a unique list of namespaces nsCount = 1 - nsDict = {} + nsSet = set() nsMap = {} propDict = {} @@ -1320,10 +1320,11 @@ def add_property_response(multistatus_elem, href, prop_list): # Collect namespaces, so we can declare them in the for # compacter output ns, _ = split_namespace(name) - if ns != "DAV:" and ns not in nsDict and ns != "": - nsDict[ns] = True - nsMap[f"NS{nsCount}"] = ns - nsCount += 1 + if ns != "DAV:" and ns not in nsSet and ns != "": + if not is_etree_element(value) or ns not in value.nsmap.values(): + nsMap[f"NS{nsCount}"] = ns + nsCount += 1 + nsSet.add(ns) propDict.setdefault(status, []).append((name, value))