Skip to content

Commit da9a468

Browse files
author
Steve Canny
committed
opc: add CorePropertiesPart.default()
1 parent 113c999 commit da9a468

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

docx/opc/parts/coreprops.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from datetime import datetime
12+
13+
from ..constants import CONTENT_TYPE as CT
1114
from ..coreprops import CoreProperties
15+
from ...oxml.parts.coreprops import CT_CoreProperties
16+
from ..packuri import PackURI
1217
from ..part import XmlPart
1318

1419

@@ -23,7 +28,13 @@ def default(cls, package):
2328
Return a new |CorePropertiesPart| object initialized with default
2429
values for its base properties.
2530
"""
26-
raise NotImplementedError
31+
core_properties_part = cls._new(package)
32+
core_properties = core_properties_part.core_properties
33+
core_properties.title = 'Word Document'
34+
core_properties.last_modified_by = 'python-docx'
35+
core_properties.revision = 1
36+
core_properties.modified = datetime.utcnow()
37+
return core_properties_part
2738

2839
@property
2940
def core_properties(self):
@@ -32,3 +43,12 @@ def core_properties(self):
3243
properties contained in this core properties part.
3344
"""
3445
return CoreProperties(self.element)
46+
47+
@classmethod
48+
def _new(cls, package):
49+
partname = PackURI('/docProps/core.xml')
50+
content_type = CT.OPC_CORE_PROPERTIES
51+
coreProperties = CT_CoreProperties.new()
52+
return CorePropertiesPart(
53+
partname, content_type, coreProperties, package
54+
)

docx/oxml/parts/coreprops.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
from datetime import datetime, timedelta
1414

15-
from ..ns import qn
15+
from .. import parse_xml
16+
from ..ns import nsdecls, qn
1617
from ..xmlchemy import BaseOxmlElement, ZeroOrOne
1718

1819

@@ -40,6 +41,19 @@ class CT_CoreProperties(BaseOxmlElement):
4041
title = ZeroOrOne('dc:title', successors=())
4142
version = ZeroOrOne('cp:version', successors=())
4243

44+
_coreProperties_tmpl = (
45+
'<cp:coreProperties %s/>\n' % nsdecls('cp', 'dc', 'dcterms')
46+
)
47+
48+
@classmethod
49+
def new(cls):
50+
"""
51+
Return a new ``<cp:coreProperties>`` element
52+
"""
53+
xml = cls._coreProperties_tmpl
54+
coreProperties = parse_xml(xml)
55+
return coreProperties
56+
4357
@property
4458
def author_text(self):
4559
"""

features/doc-coreprops.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Feature: Read and write core document properties
1616
Then the core property values match the new values
1717

1818

19-
@wip
2019
Scenario: a default core properties part is added if doc doesn't have one
2120
Given a document having no core properties part
2221
When I access the core properties object

tests/opc/parts/test_coreprops.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from datetime import datetime, timedelta
12+
1113
import pytest
1214

1315
from docx.opc.coreprops import CoreProperties
@@ -25,6 +27,17 @@ def it_provides_access_to_its_core_props_object(self, coreprops_fixture):
2527
CoreProperties_.assert_called_once_with(core_properties_part.element)
2628
assert isinstance(core_properties, CoreProperties)
2729

30+
def it_can_create_a_default_core_properties_part(self):
31+
core_properties_part = CorePropertiesPart.default(None)
32+
assert isinstance(core_properties_part, CorePropertiesPart)
33+
core_properties = core_properties_part.core_properties
34+
assert core_properties.title == 'Word Document'
35+
assert core_properties.last_modified_by == 'python-docx'
36+
assert core_properties.revision == 1
37+
delta = datetime.utcnow() - core_properties.modified
38+
max_expected_delta = timedelta(seconds=2)
39+
assert delta < max_expected_delta
40+
2841
# fixtures ---------------------------------------------
2942

3043
@pytest.fixture

0 commit comments

Comments
 (0)