Skip to content

Commit 9c8a2e9

Browse files
committed
comments: add CommentsPart.comments
1 parent ae0e82d commit 9c8a2e9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/docx/comments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33
from __future__ import annotations
44

5+
from typing import TYPE_CHECKING
6+
57
from docx.blkcntnr import BlockItemContainer
68

9+
if TYPE_CHECKING:
10+
from docx.oxml.comments import CT_Comments
11+
from docx.parts.comments import CommentsPart
12+
713

814
class Comments:
915
"""Collection containing the comments added to this document."""
1016

17+
def __init__(self, comments_elm: CT_Comments, comments_part: CommentsPart):
18+
self._comments_elm = comments_elm
19+
self._comments_part = comments_part
20+
1121

1222
class Comment(BlockItemContainer):
1323
"""Proxy for a single comment in the document.

src/docx/parts/comments.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
class CommentsPart(StoryPart):
2020
"""Container part for comments added to the document."""
2121

22+
def __init__(
23+
self, partname: PackURI, content_type: str, element: CT_Comments, package: Package
24+
):
25+
super().__init__(partname, content_type, element, package)
26+
self._comments = element
27+
2228
@property
2329
def comments(self) -> Comments:
2430
"""A |Comments| proxy object for the `w:comments` root element of this part."""
25-
raise NotImplementedError
31+
return Comments(self._comments, self)
2632

2733
@classmethod
2834
def default(cls, package: Package) -> Self:

tests/parts/test_comments.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22

33
from __future__ import annotations
44

5+
from typing import cast
6+
7+
import pytest
8+
9+
from docx.comments import Comments
510
from docx.opc.constants import CONTENT_TYPE as CT
11+
from docx.opc.packuri import PackURI
12+
from docx.oxml.comments import CT_Comments
613
from docx.package import Package
714
from docx.parts.comments import CommentsPart
815

16+
from ..unitutil.cxml import element
17+
from ..unitutil.mock import FixtureRequest, Mock, class_mock, instance_mock
18+
919

1020
class DescribeCommentsPart:
1121
"""Unit test suite for `docx.parts.comments.CommentsPart` objects."""
1222

23+
def it_provides_access_to_its_comments_collection(
24+
self, Comments_: Mock, comments_: Mock, package_: Mock
25+
):
26+
Comments_.return_value = comments_
27+
comments_elm = cast(CT_Comments, element("w:comments"))
28+
comments_part = CommentsPart(
29+
PackURI("/word/comments.xml"), CT.WML_COMMENTS, comments_elm, package_
30+
)
31+
32+
comments = comments_part.comments
33+
34+
Comments_.assert_called_once_with(comments_part.element, comments_part)
35+
assert comments is comments_
36+
1337
def it_constructs_a_default_comments_part_to_help(self):
1438
package = Package()
1539

@@ -23,3 +47,17 @@ def it_constructs_a_default_comments_part_to_help(self):
2347
"{http://schemas.openxmlformats.org/wordprocessingml/2006/main}comments"
2448
)
2549
assert len(comments_part.element) == 0
50+
51+
# -- fixtures --------------------------------------------------------------------------------
52+
53+
@pytest.fixture
54+
def Comments_(self, request: FixtureRequest) -> Mock:
55+
return class_mock(request, "docx.parts.comments.Comments")
56+
57+
@pytest.fixture
58+
def comments_(self, request: FixtureRequest) -> Mock:
59+
return instance_mock(request, Comments)
60+
61+
@pytest.fixture
62+
def package_(self, request: FixtureRequest) -> Mock:
63+
return instance_mock(request, Package)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy