source: trunk/utilities/scam-export.py @ 1983

Revision 1983, 5.7 KB checked in by pjotr, 12 years ago (diff)

References #1512, some new stuff is in

Line 
1#!/usr/bin/env python
2# Generates CSV file for SCAM export
3
4import os, sys
5import codecs
6
7from Products.CMFCore.utils import getToolByName
8from Products.LeMill.config import MATERIAL_TYPES
9
10""" This script will generate a CSV file out of LeMill instance publiched object types that should be searchable from the Calibrate portal. Later it will be imported into SCAM. SCAM repository will be serving these objects to Calibrate portal thus allowing portal users to search for materials created in LeMill.
11
12Structure of the CSV file (mandatory fields are marked with *)
13
14Identifier catalog *
15Identifier entry *
16Metadata language *
17Title
18Description
19LO language *
20LRE keyword
21Keyword
22URL
23Learning resource type
24Age range
25Description of rights *
26Author
27
28FOLLOWING WILL BE LEFT EMPTY
29Language of translation
30Translated title
31Translated description
32Translated keywords
33"""
34
35
36# The script is run line this:
37# Go to the Zope instance folder and write bin/zopectl run Products/LeMill/utilities/scam-export.py [name of your portal] [link to your instance plus /content] and write to the file of your choice
38# The actual example is something like this:
39# bin/zopectl run Products/LeMill/utilities/scam-export.py  lemill http://dev.htk.tlu.ee:7778/lemill >lre.csv
40
41# LeMill identifier (Used for: Identifier catalog)
42identifier_catalog = 'lemill'
43
44# A link to a licence used by the portal (Used for: Description of rights)
45license_link = 'http://creativecommons.org/licenses/by-sa/2.5/'
46
47# Learning resource types
48lre_types = {'PILOTMaterial':'exploration','PresentationMaterial':'presentation','MultimediaMaterial':'web resource'}
49
50# Codes for Subject areas we use
51subj_area_types = {'Natural sciences':'875','Geography':'544','Chemistry':'195','Physics':'978','Biology':'144','Environmental education':'431','Language and literature':'758','Foreign languages':'508','Mathematics':'790','Informatics or ICT':'1034','Art':'91','Music':'856','History':'590','Religion':'1085','Philosophy':'968','Ethics':'441','Psychology':'1040','Social sciences':'1204','Culture':'303','Citizenship':'209','Politics':'1001','Economics':'383','Media education':'798','Physical education':'974','School-community relationship':'1139','Educational administration':'761','Cross-curricular education':'292'}
52
53# Target groups and ages
54targ_groups = {'pre-school education':'U-6','1st grade':'6-7','2nd grade':'7-8','3rd grade':'8-9','4th grade':'9-10','5th grade':'10-11','6th grade':'11-12','7th grade':'12-13','8th grade':'13-14','9th grade':'14-15','10th grade':'15-16','11th grade':'16-17','12th grade':'17-18','higher education':'18-U','adult education':'18-U','teachers':'21-65','special education':'6-19'}
55
56def generateCSVfile(lemill_id,portal_url):
57    lemill = getattr(app,lemill_id)
58
59    pc = getToolByName(lemill, 'portal_catalog')
60    query = {'meta_type':MATERIAL_TYPES,
61        'review_state':'public'}
62    results = pc.searchResults(query)
63
64    for result in results:
65        fields = []
66        # Add Identifier catalog *
67        fields.append('lemill')
68
69        # Add Identifier entry *
70        fields.append(result.UID)
71        # Add Metadata language
72        fields.append(result.Language)
73        # Add Title
74        fields.append(result.Title)
75        # Add Description
76        if result.meta_type == 'PILOTMaterial':
77            fields.append(result.getShortDescription)
78        else:
79            fields.append(result.getDescription)
80        # Add LO language
81        fields.append(result.Language)
82        # LRE keyword
83        subj_res = result.getSubject_area
84        mod_subj_res = []
85        for subj in subj_res:
86            mod_subj_res.append(subj_area_types[subj])
87        subjs = ','.join(mod_subj_res)
88        fields.append(subjs)
89        # Add Keyword
90        # Tags list needs to be transformed into a string
91        tags_res = result.getTags
92        tags = ','.join(tags_res)
93        fields.append(tags)
94        # Add URL
95        # This will only work with objects from Content section
96        res_url = portal_url+'/content/'+result.getId
97        fields.append(res_url)
98        # Add learning resource type
99        fields.append(lre_types[result.meta_type])
100        # Add Age range
101        age_ranges = result.getTarget_group
102        def getMinimum(category,current_min):
103            v = targ_groups[category].split('-')[0]
104            if v=='U': return 'U'
105            elif current_min==None: return v
106            elif current_min=='U': return 'U'
107            return min(v,current_min)
108        def getMaximum(category,current_max):
109            v = targ_groups[category].split('-')[1]
110            if v=='U': return 'U'
111            elif current_max==None: return v
112            elif current_max=='U': return 'U'
113            return max(v,current_max)
114        minage=maxage=None
115        for cat in age_ranges:
116            if cat=='special education' and len(age_ranges)>1:
117                continue
118            minage = getMinimum(cat,minage)
119            maxage = getMaximum(cat,maxage)
120           
121        if minage==None:
122            fields.append('')
123        else:
124            fields.append('-'.join((minage,maxage)))
125        # Add Description of rights
126        fields.append(license_link)
127        # Add Author
128        # We might need to get the nice Full names for the authors
129        authors_res = result.listCreators
130        authors = ','.join(authors_res)
131        fields.append(authors)
132
133        # All the rest will just be left empty
134        # Add Language of translation
135        fields.append('')
136        # Add Translated title
137        fields.append('')
138        # Add Translated description
139        fields.append('')
140        # Add Translated keywords
141        fields.append('')
142        # Print out some stuff
143        print '\t'.join(fields)
144    # This function ends
145
146generateCSVfile(sys.argv[1],sys.argv[2])
Note: See TracBrowser for help on using the repository browser.