Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings: get and set on trackRevisions #1270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/docx/oxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
register_element_cls("w:r", CT_R)
register_element_cls("w:t", CT_Text)

# ---------------------------------------------------------------------------
# track revisions configurations
register_element_cls("w:trackRevisions", CT_OnOff)

# ---------------------------------------------------------------------------
# header/footer-related mappings

Expand Down
16 changes: 16 additions & 0 deletions src/docx/oxml/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class CT_Settings(BaseOxmlElement):
"w:listSeparator",
)
evenAndOddHeaders = ZeroOrOne("w:evenAndOddHeaders", successors=_tag_seq[48:])
trackRevisions = ZeroOrOne("w:trackRevisions", successors=_tag_seq[32:])
del _tag_seq

@property
Expand All @@ -117,9 +118,24 @@ def evenAndOddHeaders_val(self):
return False
return evenAndOddHeaders.val

@property
def trackRevisions_val(self):
"""Value of `w:trackRevisions/@w:val` or |None| if not present."""
trackRevisions = self.trackRevisions
if trackRevisions is None:
return False
return trackRevisions.val

@evenAndOddHeaders_val.setter
def evenAndOddHeaders_val(self, value):
if value in [None, False]:
self._remove_evenAndOddHeaders()
else:
self.get_or_add_evenAndOddHeaders().val = value

@trackRevisions_val.setter
def trackRevisions_val(self, value):
if value in [None, False]:
self._remove_trackRevisions()
else:
self.get_or_add_trackRevisions().val = value
13 changes: 13 additions & 0 deletions src/docx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def odd_and_even_pages_header_footer(self):
"""
return self._element.evenAndOddHeaders_val

@property
def track_revisions(self):
"""
True if this document has track revisions feature enabled.

Read/write.
"""
return self._element.trackRevisions_val

@odd_and_even_pages_header_footer.setter
def odd_and_even_pages_header_footer(self, value):
self._element.evenAndOddHeaders_val = value

@track_revisions.setter
def track_revisions(self, value):
self._element.trackRevisions_val = value
52 changes: 52 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ def it_can_change_whether_the_document_has_distinct_odd_and_even_headers(

assert settings_elm.xml == expected_xml

def it_knows_when_the_document_has_track_revisions(
self, track_revisions_get_fixture
):
settings_elm, expected_value = track_revisions_get_fixture
settings = Settings(settings_elm)

track_revisions = settings.track_revisions

assert track_revisions is expected_value

def it_can_change_whether_the_document_has_track_revisions(
self, track_revisions_set_fixture
):
settings_elm, value, expected_xml = track_revisions_set_fixture
settings = Settings(settings_elm)

settings.track_revisions = value

assert settings_elm.xml == expected_xml

# fixtures -------------------------------------------------------

@pytest.fixture(
Expand Down Expand Up @@ -61,3 +81,35 @@ def odd_and_even_set_fixture(self, request):
settings_elm = element(settings_cxml)
expected_xml = xml(expected_cxml)
return settings_elm, value, expected_xml

@pytest.fixture(
params=[
("w:settings", False),
("w:settings/w:trackRevisions", True),
("w:settings/w:trackRevisions{w:val=0}", False),
("w:settings/w:trackRevisions{w:val=1}", True),
("w:settings/w:trackRevisions{w:val=true}", True),
]
)
def track_revisions_get_fixture(self, request):
settings_cxml, expected_value = request.param
settings_elm = element(settings_cxml)
return settings_elm, expected_value

@pytest.fixture(
params=[
("w:settings", True, "w:settings/w:trackRevisions"),
("w:settings/w:trackRevisions", False, "w:settings"),
(
"w:settings/w:trackRevisions{w:val=1}",
True,
"w:settings/w:trackRevisions",
),
("w:settings/w:trackRevisions{w:val=off}", False, "w:settings"),
]
)
def track_revisions_set_fixture(self, request):
settings_cxml, value, expected_cxml = request.param
settings_elm = element(settings_cxml)
expected_xml = xml(expected_cxml)
return settings_elm, value, expected_xml