source: trunk/LOM.py @ 1986

Revision 261, 8.2 KB checked in by jukka, 13 years ago (diff)

#324, #325 Things work better, but not yet perfect. Tests fail, body text in material keeps disappearing and I'm too tired to make any sense of it. LOM fields are hidden to separate LOM schema, which is not called at all. Author -field requires own visual widget to make it nice.

  • Property svn:eol-style set to native
Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from Products.Archetypes.public import Schema
20from Products.Archetypes.public import StringField, ComputedField, DateTimeField,\
21        LinesField, ReferenceField
22from Products.Archetypes.public import StringWidget, CalendarWidget, MultiSelectionWidget,\
23        SelectionWidget, LinesWidget
24from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget
25
26LOMSchema = Schema((
27    StringField('LOMCoverage',
28        searchable=1,
29        schemata='LOM',
30        accessor='getLOMCoverage',
31        widget=StringWidget(
32            visible = {'view':'invisible', 'edit':'invisible'},
33            i18n_domain = "lemill",
34            label_msgid='label_lom_coverage',
35            description_msgid='help_lom_coverage')),
36    StringField('LOMVersion',
37        searchable=1,
38        schemata='LOM',
39        accessor='getLOMVersion',
40        widget=StringWidget(
41            visible = {'view':'invisible', 'edit':'invisible'},
42            i18n_domain = "lemill",
43            label_msgid='label_lom_version',
44            description_msgid='help_lom_version')),
45    DateTimeField('LOMContributionDate',
46        searchable=1,
47        schemata='LOM',
48        accessor='getLOMContributionDate',
49        widget=CalendarWidget(
50            visible = {'view':'invisible', 'edit':'invisible'},
51            i18n_domain = "lemill",
52            label_msgid='label_lom_contribution_date',
53            description_msgid='help_lom_contribution_date')),
54    LinesField('LOMLearningResourceType',
55        searchable=1,
56        schemata='LOM',
57        vocabulary=('exercise', 'simulation', 'questionnaire', 'diagram',
58            'figure', 'graph', 'index', 'slide', 'table', 'narrative text',
59            'exam', 'experiment', 'problem statement', 'self assessment'),
60        default='',
61        accessor='getLOMLearningResourceType',
62        widget=MultiSelectionWidget(
63            i18n_domain = "lemill",
64            label_msgid='label_lom_learning_resource_type',
65            description_msgid='help_lom_learning_resource_type')),
66    LinesField('LOMIntendedEndUserRole',
67        searchable=1,
68        schemata='LOM',
69        vocabulary=('learner', 'teacher', 'author', 'manager'),
70        default='learner',
71        accessor='getLOMIntendedEndUserRole',
72        widget=MultiSelectionWidget(
73            i18n_domain = "lemill",
74            label_msgid='label_lom_intended_enduser_role',
75            description_msgid='help_lom_intended_enduser_role')),
76    LinesField('LOMContext',
77        searchable=1,
78        schemata='LOM',
79        vocabulary=('Primary Education', 'Secondary Education',' Higher Education',
80            'University First Cycle', 'University Second Cycle', 'University Postgrade',
81            'Technical School First Cycle', 'Technical School Second Cycle',
82            'Professional Formation', 'Continuous Formation', 'Vocational Training'),
83        default='',
84        accessor='getLOMContext',
85        widget=MultiSelectionWidget(
86            i18n_domain = "lemill",
87            label_msgid='label_lom_context',
88            description_msgid='help_lom_context')),
89    LinesField('LOMDifficulty',
90        searchable=1,
91        schemata='LOM',
92        vocabulary=('very easy', 'easy', 'medium', 'difficult', 'very difficult'),
93        default='medium',
94        accessor='getLOMDifficulty',
95        widget=SelectionWidget(
96            i18n_domain = "lemill",
97            label_msgid='label_lom_difficulty',
98            description_msgid='help_lom_difficulty')),
99    LinesField('LOMCopyrightAndOtherRestrictions',
100        searchable=1,
101        schemata='LOM',
102        vocabulary=('yes', 'no'),
103        default='no',
104        accessor='getLOMCopyrightAndOtherRestrictions',
105        widget=SelectionWidget(
106            i18n_domain = "lemill",
107            label_msgid='label_lom_copyright_and_other_restrictions',
108            description_msgid='help_lom_copyright_and_other_restrictions')),
109    LinesField('LOMSubjectArea',
110        searchable=1,
111        schemata='LOM',
112        accessor='getLOMSubjectArea',
113        widget=LinesWidget(
114            i18n_domain = "lemill",
115            label_msgid='label_lom_subject_area',
116            description_msgid='help_lom_subject_area')),
117    ReferenceField('LOMReferencePrerequisites',
118        searchable=1,
119        schemata='LOM',
120        accessor='getLOMReferencePrerequisites',
121        relationship='Prerequirements',
122        allowed_types=('Material',),
123        multiValued=True,
124        widget = ReferenceBrowserWidget(
125            visible = {'view':'invisible', 'edit':'invisible'},
126            allow_search = True,
127            allow_browse = True,
128            show_indexes = False,
129            force_close_on_insert = True,
130            startup_directory = "content",
131            size = 4,
132            label_msgid = "label_lom_reference_prerequisites",
133            description_msgid = "help_lom_reference_prerequisites",
134            i18n_domain = "lemill",)),
135))
136
137from xml.dom.minidom import parse, parseString, getDOMImplementation, Node
138
139def getDocument(element):
140    while element.nodeType != Node.DOCUMENT_NODE:
141        element=element.parentNode
142    return element
143
144def addText(element, text):
145    textNode = getDocument(element).createTextNode(text)
146    element.appendChild(textNode)
147    return textNode
148
149def addNode(element, node):
150    newNode = getDocument(element).createElement(node)
151    element.appendChild(newNode)
152    return newNode
153
154def addLangstring(element, language, text):
155    langstring = addNode(element, "langstring")
156    if not language:
157        language = 'en'
158    langstring.setAttribute("xml:lang", language)
159    addText(langstring, text)
160    return langstring
161
162def exportLOM(object):
163   
164    impl = getDOMImplementation()
165    document = impl.createDocument(None, "lom", None)
166    top_element = document.documentElement
167   
168    if object.meta_type=='Material':
169        general = addNode(top_element, "general")
170        general_identifier = addNode(general, "identifier")
171        addText(general_identifier, object.getId())
172        general_description = addNode(general, "description")
173        addLangstring(general_description, object.Language(), object.Description())
174        general_title = addNode(general, "title")
175        addLangstring(general_title, object.Language(), object.Title())
176       
177        rights = addNode(top_element, "rights")
178        rights_description = addNode(rights, "description")
179        addLangstring(rights_description, object.Language(), object.Rights())
180   
181    output = document.toxml('utf-8')
182
183    document.unlink()
184   
185    return output
186       
187   
188def miniXPath(element, path):
189    for part in path.split('/'):
190        if(element.getElementsByTagName(part)):
191            element = element.getElementsByTagName(part)[0]
192        else:
193            return None
194    return element
195
196def getText(element):
197    rc = ""
198    for node in element.childNodes:
199        if node.nodeType == node.TEXT_NODE:
200            rc = rc + node.data
201    return rc
202
203def constructLOM(folder):
204    id = folder.generateUniqueId()
205    folder.invokeFactory(type_name='Material',id=id)
206    return getattr(folder,id)
207
208def importLOM(folder, file):
209    dom = parseString(file)
210    document = dom.documentElement
211    # TODO: i18n
212    description_element = miniXPath(document, 'general/description/langstring')
213    title_element = miniXPath(document, 'general/title/langstring')
214    rights_element = miniXPath(document, 'rights/description/langstring')
215   
216    ob = constructLOM(folder)
217    ob.setDescription(getText(description_element))
218    ob.setTitle(getText(title_element))
219    ob.setRights(getText(rights_element))
220   
221    return ob
222   
223   
Note: See TracBrowser for help on using the repository browser.