-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmetadata.py
383 lines (306 loc) · 12.1 KB
/
metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from AccessControl.SecurityManagement import getSecurityManager
from datetime import datetime
from DateTime import DateTime
from plone.app.dexterity import _
from plone.app.z3cform.widgets.datetime import DatetimeFieldWidget
from plone.app.z3cform.widgets.select import AjaxSelectFieldWidget
from plone.app.z3cform.widgets.select import Select2FieldWidget
from plone.autoform import directives
from plone.autoform.interfaces import IFormFieldProvider
from plone.base.interfaces.siteroot import IPloneSiteRoot
from plone.dexterity.interfaces import IDexterityContent
from plone.dexterity.utils import safe_unicode
from plone.supermodel import model
from Products.CMFCore.utils import getToolByName
from z3c.form.interfaces import IAddForm
from z3c.form.interfaces import IEditForm
from z3c.form.widget import ComputedWidgetAttribute
from zope import schema
from zope.component import adapter
from zope.component.hooks import getSite
from zope.interface import Invalid
from zope.interface import invariant
from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory
# Behavior interfaces to display Dublin Core metadata fields on Dexterity
# content edit forms.
#
# These schemata duplicate the fields of zope.dublincore.IZopeDublinCore,
# in order to annotate them with form hints and more helpful titles
# and descriptions.
@provider(IContextAwareDefaultFactory)
def default_language(context):
# If we are adding a new object, context will be the folderish object where
# this new content is being added
language = None
# Try to get the language from context or parent(s)
while (
not language and context is not None and not IPloneSiteRoot.providedBy(context)
):
language = getattr(context.aq_base, "language", None)
if not language:
# If we are here, it means we were editing an object that didn't
# have its language set or that the container where we were adding
# the new content didn't have a language set. So we check its
# parent.
context = context.__parent__
language_tool = getToolByName(getSite(), "portal_languages")
default_language = language_tool.getDefaultLanguage()
if not language:
language = default_language
# Is the language supported/enabled at all?
if language not in language_tool.getAvailableLanguages():
language = default_language
return language
@provider(IFormFieldProvider)
class IBasic(model.Schema):
# default fieldset
title = schema.TextLine(title=_("label_title", default="Title"), required=True)
description = schema.Text(
title=_("label_description", default="Summary"),
description=_(
"help_description", default="Used in item listings and search results."
),
required=False,
missing_value="",
)
directives.order_before(description="*")
directives.order_before(title="*")
directives.omitted("title", "description")
directives.no_omit(IEditForm, "title", "description")
directives.no_omit(IAddForm, "title", "description")
@provider(IFormFieldProvider)
class ICategorization(model.Schema):
# categorization fieldset
model.fieldset(
"categorization",
label=_("label_schema_categorization", default="Categorization"),
fields=["subjects", "language"],
)
subjects = schema.Tuple(
title=_("label_tags", default="Tags"),
description=_(
"help_tags",
default="Tags are commonly used for ad-hoc organization of " + "content.",
),
value_type=schema.TextLine(),
required=False,
missing_value=(),
)
directives.widget(
"subjects", AjaxSelectFieldWidget, vocabulary="plone.app.vocabularies.Keywords"
)
language = schema.Choice(
title=_("label_language", default="Language"),
vocabulary="plone.app.vocabularies.SupportedContentLanguages",
required=False,
missing_value="",
defaultFactory=default_language,
)
directives.widget("language", Select2FieldWidget)
directives.omitted("subjects", "language")
directives.no_omit(IEditForm, "subjects", "language")
directives.no_omit(IAddForm, "subjects", "language")
class EffectiveAfterExpires(Invalid):
__doc__ = _(
"error_invalid_publication", default="Invalid effective or expires date"
)
@provider(IFormFieldProvider)
class IPublication(model.Schema):
# dates fieldset
model.fieldset(
"dates",
label=_("label_schema_dates", default="Dates"),
fields=["effective", "expires"],
)
effective = schema.Datetime(
title=_("label_effective_date", "Publishing Date"),
description=_(
"help_effective_date",
default="If this date is in the future, the content will "
"not show up in listings and searches until this date.",
),
required=False,
)
directives.widget("effective", DatetimeFieldWidget)
expires = schema.Datetime(
title=_("label_expiration_date", "Expiration Date"),
description=_(
"help_expiration_date",
default="When this date is reached, the content will no "
"longer be visible in listings and searches.",
),
required=False,
)
directives.widget("expires", DatetimeFieldWidget)
@invariant
def validate_start_end(data):
if data.effective and data.expires and data.effective > data.expires:
raise EffectiveAfterExpires(
_(
"error_expiration_must_be_after_effective_date",
default="Expiration date must be after publishing date.",
)
)
directives.omitted("effective", "expires")
directives.no_omit(IEditForm, "effective", "expires")
directives.no_omit(IAddForm, "effective", "expires")
@provider(IFormFieldProvider)
class IOwnership(model.Schema):
# ownership fieldset
model.fieldset(
"ownership",
label=_("label_schema_ownership", default="Ownership"),
fields=["creators", "contributors", "rights"],
)
creators = schema.Tuple(
title=_("label_creators", "Creators"),
description=_(
"help_creators",
default="Persons responsible for creating the content of "
"this item. Please enter a list of user names, one "
"per line. The principal creator should come first.",
),
value_type=schema.TextLine(),
required=False,
missing_value=(),
)
directives.widget(
"creators", AjaxSelectFieldWidget, vocabulary="plone.app.vocabularies.Users"
)
contributors = schema.Tuple(
title=_("contributors", "Contributors"),
description=_(
"help_contributors",
default="The names of people that have contributed "
"to this item. Each contributor should "
"be on a separate line.",
),
value_type=schema.TextLine(),
required=False,
missing_value=(),
)
directives.widget(
"contributors", AjaxSelectFieldWidget, vocabulary="plone.app.vocabularies.Users"
)
rights = schema.Text(
title=_("label_copyrights", default="Rights"),
description=_(
"help_copyrights",
default="Copyright statement or other rights information on this " "item.",
),
required=False,
)
directives.omitted("creators", "contributors", "rights")
directives.no_omit(IEditForm, "creators", "contributors", "rights")
directives.no_omit(IAddForm, "creators", "contributors", "rights")
# make sure the add form shows the default creator
def creatorsDefault(data):
user = getSecurityManager().getUser()
# NB: CMF users are UTF-8 encoded bytes, decode them before inserting
return user and (safe_unicode(user.getId()),)
CreatorsDefaultValue = ComputedWidgetAttribute(
creatorsDefault, field=IOwnership["creators"]
)
@provider(IFormFieldProvider)
class IDublinCore(IOwnership, IPublication, ICategorization, IBasic):
"""Metadata behavior providing all the DC fields"""
pass
@adapter(IDexterityContent)
class MetadataBase:
"""This adapter uses DCFieldProperty to store metadata directly on an
object using the standard CMF DefaultDublinCoreImpl getters and
setters.
"""
def __init__(self, context):
self.context = context
_marker = object()
class DCFieldProperty:
"""Computed attributes based on schema fields.
Based on zope.schema.fieldproperty.FieldProperty.
"""
def __init__(self, field, get_name=None, set_name=None):
if get_name is None:
get_name = field.__name__
self._field = field
self._get_name = get_name
self._set_name = set_name
def __get__(self, inst, klass):
if inst is None:
return self
attribute = getattr(inst.context, self._get_name, _marker)
if attribute is _marker:
field = self._field.bind(inst)
attribute = getattr(field, "default", _marker)
if attribute is _marker:
raise AttributeError(self._field.__name__)
elif callable(attribute):
attribute = attribute()
if isinstance(attribute, DateTime):
# Ensure datetime value is stripped of any timezone and seconds
# so that it can be compared with the value returned by the widget
return datetime(*list(map(int, attribute.parts()[:6])))
if attribute is None:
return
return attribute
def __set__(self, inst, value):
field = self._field.bind(inst)
field.validate(value)
if field.readonly:
raise ValueError(self._field.__name__, "field is readonly")
if isinstance(value, datetime):
# The ensures that the converted DateTime value is in the
# server's local timezone rather than GMT.
value = DateTime(
value.year, value.month, value.day, value.hour, value.minute
)
if self._set_name:
getattr(inst.context, self._set_name)(value)
elif inst.context.hasProperty(self._get_name):
inst.context._updateProperty(self._get_name, value)
else:
setattr(inst.context, self._get_name, value)
def __getattr__(self, name):
return getattr(self._field, name)
class Basic(MetadataBase):
def _get_title(self):
return self.context.title
def _set_title(self, value):
if not isinstance(value, str):
raise ValueError("Title must be text.")
self.context.title = value
title = property(_get_title, _set_title)
def _get_description(self):
return self.context.description
def _set_description(self, value):
if not isinstance(value, str):
raise ValueError("Description must be text.")
self.context.description = value
description = property(_get_description, _set_description)
class Categorization(MetadataBase):
def _get_subjects(self):
return self.context.subject
def _set_subjects(self, value):
self.context.subject = value
subjects = property(_get_subjects, _set_subjects)
language = DCFieldProperty(
ICategorization["language"], get_name="Language", set_name="setLanguage"
)
class Publication(MetadataBase):
effective = DCFieldProperty(IPublication["effective"], get_name="effective_date")
expires = DCFieldProperty(IPublication["expires"], get_name="expiration_date")
class Ownership(MetadataBase):
creators = DCFieldProperty(
IOwnership["creators"], get_name="listCreators", set_name="setCreators"
)
contributors = DCFieldProperty(
IOwnership["contributors"], get_name="Contributors", set_name="setContributors"
)
rights = DCFieldProperty(
IOwnership["rights"], get_name="Rights", set_name="setRights"
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.context.addCreator()
class DublinCore(Basic, Categorization, Publication, Ownership):
pass