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

Revision 2409, 6.7 KB checked in by anonymous, 11 years ago (diff)

Worked on #1743. *Important* Update first to latest tagged release ([2408]) and run archetype update and quickinstaller there before doing anything with this version. Major changes: removed workflows, created LeMillCatalogTool? to provide simpler, faster catalog, replaced Plone's globalize with simpler, faster version, removed reliance to Plone's Actions in page templates, simplified access to portlets Still to do: go through portlets code and remove slow and cumbersome checks and see if they all work.

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)
42# XXX Might want to use the first value provided to the script if there are many LeMill portals
43identifier_catalog = 'lemill'
44
45# A link to a licence used by the portal (Used for: Description of rights)
46license_link = 'http://creativecommons.org/licenses/by-sa/2.5/'
47
48# Learning resource types
49lre_types = {'PILOTMaterial':'exploration','PresentationMaterial':'presentation','MultimediaMaterial':'web resource','ExerciseMaterial':'assessment'}
50
51# Codes for Subject areas we use
52subj_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'}
53
54# Target groups and ages
55targ_groups = {'pre-school education':'U-6','1st grade':'6-8','2nd grade':'7-8','3rd grade':'8-10','4th grade':'9-11','5th grade':'10-12','6th grade':'11-13','7th grade':'12-14','8th grade':'13-15','9th grade':'14-16','10th grade':'15-17','11th grade':'16-18','12th grade':'17-19','higher education':'18-U','adult education':'18-U','teacher training':'21-65','teachers':'21-65','special education':'6-19'}
56
57def generateCSVfile(lemill_id,portal_url):
58    lemill = getattr(app,lemill_id)
59
60    pc = getToolByName(lemill, 'portal_catalog')
61    query = {'meta_type':MATERIAL_TYPES,
62        'getState':'public'}
63    results = pc.searchResults(query)
64
65    # Prefetch all the ids and names of the users and make a dictionary out of these
66    # Later this will be used to quickly get the Full name of the user by the id provided
67    all_users = {}
68    user_query = {'meta_type':'MemberFolder',
69        'getState':'public'}
70    user_results = pc.searchResults(user_query)
71
72    for user in user_results:
73        if user.getFull_name:
74            all_users[user.id] = user.getFull_name
75        else:
76            all_users[user.id] = user.id
77
78    for result in results:
79        fields = []
80        # Add Identifier catalog *
81        fields.append(identifier_catalog)
82        # Add Identifier entry *
83        fields.append(result.UID)
84        # Add Metadata language
85        fields.append(result.Language)
86#        if result.Language=='fi':
87#            import pdb
88#            pdb.set_trace()
89        # Add Title
90        fields.append(result.Title)
91        # Add Description
92        if result.meta_type == 'PILOTMaterial':
93            desc=result.getShortDescription
94        else:
95            # XXX This field in catalog is empty at the moment, but we should make a prefill mechanism for that
96            desc=result.getDescription
97        desc=desc.replace('\r','\n').split('\n')[0]
98        fields.append(desc)
99        # Add LO language
100        fields.append(result.Language)
101        # LRE keyword
102        subj_res = result.getSubject_area
103        mod_subj_res = []
104        for subj in subj_res:
105            mod_subj_res.append(subj_area_types[subj])
106        subjs = ','.join(mod_subj_res)
107        fields.append(subjs)
108        # Add Keyword
109        tags_res = result.getTags
110        tags = ','.join(tags_res)
111        fields.append(tags)
112        # Add URL
113        # This will only work with objects from Content section
114        res_url = portal_url+'/content/'+result.getId
115        fields.append(res_url)
116        # Add learning resource type
117        fields.append(lre_types[result.meta_type])
118        # Add Age range
119        age_ranges = result.getTarget_group
120        def getMinimum(category,current_min):
121            v = targ_groups[category].split('-')[0]
122            if v=='U': return 'U'
123            elif current_min=='U': return 'U'
124            v=int(v)
125            if current_min==None: return v
126            return min(v,current_min)
127        def getMaximum(category,current_max):
128            v = targ_groups[category].split('-')[1]
129            if v=='U': return 'U'
130            elif current_max=='U': return 'U'
131            v=int(v)
132            if current_max==None: return v
133            return max(v,current_max)
134        minage=maxage=None
135        for cat in age_ranges:
136            if cat=='special education' and len(age_ranges)>1:
137                continue
138            minage = getMinimum(cat,minage)
139            maxage = getMaximum(cat,maxage)
140           
141        if minage==None:
142            # XXX Or should that just be Undefined default: "U-U"
143            fields.append('')
144        else:
145            fields.append('%s-%s' % (minage,maxage))
146        # Add Description of rights
147        fields.append(license_link)
148        # Add Author
149        authors_res = result.listCreators
150        full_res = []
151        for author in authors_res:
152            if author in all_users.keys():
153                full_res.append(all_users[author])
154        authors = ','.join(full_res)
155        fields.append(authors)
156
157        # All the rest will just be left empty
158        # Add Language of translation
159        fields.append('')
160        # Add Translated title
161        fields.append('')
162        # Add Translated description
163        fields.append('')
164        # Add Translated keywords
165        fields.append('')
166        # Print out some stuff
167        print '\t'.join(fields)
168    # This function ends
169
170generateCSVfile(sys.argv[1],sys.argv[2])
Note: See TracBrowser for help on using the repository browser.