|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from zope.container.interfaces import IContainer |
|
20 from zope.location.interfaces import IContained |
|
21 |
|
22 # import packages |
|
23 from pyams_thesaurus.schema import ThesaurusTermField, ThesaurusTermsListField, ValidatedSet |
|
24 from zope.interface import Interface, Attribute, Invalid, invariant |
|
25 from zope.schema import Text, TextLine, Bool, Choice, Int, Datetime, Set |
|
26 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm |
|
27 |
|
28 from pyams_thesaurus import _ |
|
29 |
|
30 |
|
31 STATUS_CANDIDATE = 'candidate' |
|
32 STATUS_PUBLISHED = 'published' |
|
33 STATUS_ARCHIVED = 'archived' |
|
34 |
|
35 THESAURUS_STATUS = (STATUS_CANDIDATE, |
|
36 STATUS_PUBLISHED, |
|
37 STATUS_ARCHIVED) |
|
38 |
|
39 THESAURUS_STATUS_LABELS = (_("Candidate"), |
|
40 _("Published"), |
|
41 _("Archived")) |
|
42 |
|
43 THESAURUS_STATUS_VOCABULARY = SimpleVocabulary([SimpleTerm(THESAURUS_STATUS[i], t, t) |
|
44 for i, t in enumerate(THESAURUS_STATUS_LABELS)]) |
|
45 |
|
46 |
|
47 class IThesaurusTerm(IContained): |
|
48 """Thesaurus term base interface""" |
|
49 |
|
50 id = Attribute("Internal ID") |
|
51 |
|
52 label = TextLine(title=_("Term label"), |
|
53 description=_("Full keyword for the given term"), |
|
54 required=True) |
|
55 |
|
56 @invariant |
|
57 def check_label(self): |
|
58 if '/' in self.label: |
|
59 raise Invalid(_("'/' character is forbidden in term's label")) |
|
60 |
|
61 base_label = Attribute("Base label without uppercase or accentuated character") |
|
62 |
|
63 title = Attribute("Term external title") |
|
64 |
|
65 alt = TextLine(title=_("Alternate label"), |
|
66 description=_("Not to be confused with synonyms 'usage' label, given below..."), |
|
67 required=False) |
|
68 |
|
69 definition = Text(title=_("Definition"), |
|
70 description=_("Long definition, mostly for complicated terms"), |
|
71 required=False) |
|
72 |
|
73 note = Text(title=_("Term's application note"), |
|
74 description=_("Application note for the given term"), |
|
75 required=False) |
|
76 |
|
77 generic = ThesaurusTermField(title=_("Generic term"), |
|
78 description=_("Parent generic term of the current term"), |
|
79 required=False) |
|
80 |
|
81 specifics = ThesaurusTermsListField(title=_("Specifics terms"), |
|
82 description=_("Child more specifics terms of the current term"), |
|
83 required=False) |
|
84 |
|
85 associations = ThesaurusTermsListField(title=_("Associated terms"), |
|
86 description=_("Other terms associated to the current term"), |
|
87 required=False) |
|
88 |
|
89 usage = ThesaurusTermField(title=_("Usage"), |
|
90 description=_("For synonyms, specify here the term's descriptor to use"), |
|
91 required=False) |
|
92 |
|
93 used_for = ThesaurusTermsListField(title=_("Synonyms"), |
|
94 description=_("For a given allowed descriptor, specify here the list of " |
|
95 "synonyms"), |
|
96 required=False) |
|
97 |
|
98 @invariant |
|
99 def check_synonym(self): |
|
100 if self.usage is not None: |
|
101 if self.generic is not None: |
|
102 raise Invalid(_("A term can't be a synonym and attached to a generic term")) |
|
103 if self.used_for: |
|
104 raise Invalid(_("A term used as synonym can't have it's own synonyms " |
|
105 "(all synonyms should be attached to the descriptor)")) |
|
106 |
|
107 extracts = ValidatedSet(title=_("Extracts"), |
|
108 description=_("List of thesaurus extracts including this term"), |
|
109 required=False, |
|
110 value_type=Choice(vocabulary='PyAMS thesaurus extracts')) |
|
111 |
|
112 extensions = Set(title=_("Extensions"), |
|
113 description=_("List of available extensions applied to this term"), |
|
114 required=False, |
|
115 value_type=Choice(vocabulary='PyAMS thesaurus extensions')) |
|
116 |
|
117 status = Choice(title=_("Status"), |
|
118 description=_("Term status"), |
|
119 required=True, |
|
120 vocabulary=THESAURUS_STATUS_VOCABULARY, |
|
121 default=u'published') |
|
122 |
|
123 level = Int(title=_("Level"), |
|
124 description=_("Term's level in the thesaurus tree"), |
|
125 required=True, |
|
126 readonly=True, |
|
127 default=1) |
|
128 |
|
129 micro_thesaurus = Bool(title=_("Micro-thesaurus?"), |
|
130 description=_("Is the term part of a micro-thesaurus?"), |
|
131 required=False) |
|
132 |
|
133 parent = ThesaurusTermField(title=_("First level parent"), |
|
134 description=_("Parent at level 1 of the current term, or None"), |
|
135 required=False, |
|
136 schema=Interface) |
|
137 |
|
138 created = Datetime(title=_("Creation date"), |
|
139 required=False) |
|
140 |
|
141 modified = Datetime(title=_("Modification date"), |
|
142 required=False) |
|
143 |
|
144 def add_extract(self, extract, check=True): |
|
145 """Add given extract to the list of term extracts""" |
|
146 |
|
147 def remove_extract(self, extract, check=True): |
|
148 """Remove given extract from the list of term extracts""" |
|
149 |
|
150 def query_extensions(self): |
|
151 """Get list of extension utilities""" |
|
152 |
|
153 def get_parents(self): |
|
154 """Get list of term's parents""" |
|
155 |
|
156 def get_parent_childs(self): |
|
157 """Get 'brother's terms of current term""" |
|
158 |
|
159 def get_all_childs(self, terms=None, with_synonyms=False): |
|
160 """Get full list of term's specifics""" |
|
161 |
|
162 def merge(self, term, configuration): |
|
163 """Merge term attributes with given term, to avoid overwriting all entity""" |
|
164 |
|
165 |
|
166 IThesaurusTerm['generic'].schema = IThesaurusTerm |
|
167 IThesaurusTerm['usage'].schema = IThesaurusTerm |
|
168 IThesaurusTerm['parent'].schema = IThesaurusTerm |
|
169 |
|
170 |
|
171 class IThesaurusLoaderTerm(Interface): |
|
172 """Marker interface for temporary thesaurus loader terms""" |
|
173 |
|
174 |
|
175 class IThesaurusTermsContainer(IContainer): |
|
176 """Thesaurus terms container interface""" |