source: waeup/branches/ulif-namespace/src/sirp/utils/csvimport.py @ 4915

Last change on this file since 4915 was 4789, checked in by uli, 15 years ago

Merge changes from ulif-layout back into trunk (finally).

File size: 8.6 KB
Line 
1"""CSV importers.
2"""
3import grok
4from waeup.csvfile import CSVFile, toBool
5from waeup.csvfile.interfaces import ICSVFile
6from waeup.interfaces import IFacultyContainer, IWAeUPCSVImporter
7from waeup.utils.importexport import CSVImporter
8from zope.app.catalog.interfaces import ICatalog
9from zope.component import createObject, getUtility
10
11#
12# CSV import stuff
13#
14class IFacultyCSVFile(ICSVFile):
15    """A CSV file that contains faculty data.
16    """
17
18class FacultyCSVFile(CSVFile):
19    """An abstraction of a CSV file containing faculties.
20    """
21    grok.implements(IFacultyCSVFile)
22    grok.provides(IFacultyCSVFile)
23    required_fields = ['code', 'title', 'title_prefix']
24
25class FacultyCSVImporter(CSVImporter):
26    """Shuffle data from faculty CSV files into faculty containers.
27    """
28    # Tell, what kinds of objects we connect...
29    grok.adapts(IFacultyCSVFile, IFacultyContainer)
30    # Tell the world, that we are an importer...
31    grok.implements(IWAeUPCSVImporter)
32    grok.provides(IWAeUPCSVImporter)
33
34    datatype = u'Faculty Importer'
35
36    def doImport(self, clear_old_data=True, overwrite=True):
37        # CSVImporter instances have a `csvfile` and a `receiver`
38        # object defined which refer to the CSV file and the container.
39        for row in self.csvfile.getData():
40            new_item = createObject(u'waeup.Faculty')
41            for key, val in row.items():
42                setattr(new_item, key, val)
43            self.receiver.addFaculty(new_item)
44        return
45
46class IDepartmentCSVFile(ICSVFile):
47    """A CSV file that contains department data.
48    """
49
50class DepartmentCSVFile(CSVFile):
51    """An abstraction of a CSV file containing departments.
52    """
53    grok.implements(IDepartmentCSVFile)
54    grok.provides(IDepartmentCSVFile)
55    required_fields = ['code', 'title', 'title_prefix', 'faculty_code']
56
57class DepartmentCSVImporter(CSVImporter):
58    """Shuffle data from department CSV files into faculty containers.
59    """
60    # Tell, what kinds of objects we connect...
61    grok.adapts(IDepartmentCSVFile, IFacultyContainer)
62    # Tell the world, that we are an importer...
63    grok.implements(IWAeUPCSVImporter)
64    grok.provides(IWAeUPCSVImporter)
65
66    datatype = u'Department Importer'
67
68    def doImport(self, clear_old_data=True, overwrite=True):
69        # CSVImporter instances have a `csvfile` and a `receiver`
70        # object defined which refer to the CSV file and the container.
71        for row in self.csvfile.getData():
72            new_item = createObject(u'waeup.Department')
73            faculty_code = row['faculty_code']
74            faculty = self.receiver[faculty_code]
75            del row['faculty_code']
76            for key, val in row.items():
77                setattr(new_item, key, val)
78            faculty.addDepartment(new_item)
79        return
80
81class ICourseCSVFile(ICSVFile):
82    """A CSV file that contains course data.
83    """
84
85class CourseCSVFile(CSVFile):
86    """An abstraction of a CSV file containing courses.
87    """
88    grok.implements(ICourseCSVFile)
89    grok.provides(ICourseCSVFile)
90    required_fields = ['code', 'title', 'level', 'passmark', 'credits',
91                       'semester', 'faculty', 'department']
92
93class CourseCSVImporter(CSVImporter):
94    """Shuffle data from course CSV files into faculty containers.
95    """
96    # Tell, what kinds of objects we connect...
97    grok.adapts(ICourseCSVFile, IFacultyContainer)
98    # Tell the world, that we are an importer...
99    grok.implements(IWAeUPCSVImporter)
100    grok.provides(IWAeUPCSVImporter)
101
102    datatype = u'Course Importer'
103
104    def doImport(self, clear_old_data=True, overwrite=True):
105        # CSVImporter instances have a `csvfile` and a `receiver`
106        # object defined which refer to the CSV file and the container.
107        for row in self.csvfile.getData():
108
109            new_item = createObject(u'waeup.Course')
110           
111            faculty_code = row['faculty']
112            faculty = self.receiver[faculty_code]
113            del row['faculty']
114
115            dept_code = row['department']
116            dept = faculty[dept_code]
117            del row['department']
118
119            # Type casts...
120            if row['semester'] == '' or row['semester'] == 'None':
121                    row['semester'] = 0
122            if row['semester'] == '1,2':
123                row['semester'] = 3
124            for key in ['semester', 'credits', 'passmark']:
125                if row[key] == '' or row[key] == 'None':
126                    row[key] = None
127                    continue
128                row[key] = int(row[key])
129
130            for key, val in row.items():
131                setattr(new_item, key, val)
132            dept.courses.addCourse(new_item)
133        return
134
135class ICertificateCSVFile(ICSVFile):
136    """A CSV file that contains certificate data.
137    """
138
139class CertificateCSVFile(CSVFile):
140    """An abstraction of a CSV file containing certificates.
141    """
142    grok.implements(ICertificateCSVFile)
143    grok.provides(ICertificateCSVFile)
144    required_fields = ['code', 'title', 'faculty_code', 'department_code',
145                       'category', 'study_mode', 'start_level', 'end_level',
146                       'm_prefix', 'max_pass', 'application_category']
147
148class CertificateCSVImporter(CSVImporter):
149    """Shuffle data from certificate CSV files into faculty containers.
150    """
151    # Tell, what kinds of objects we connect...
152    grok.adapts(ICertificateCSVFile, IFacultyContainer)
153    # Tell the world, that we are an importer...
154    grok.implements(IWAeUPCSVImporter)
155    grok.provides(IWAeUPCSVImporter)
156
157    datatype = u'Certificate Importer'
158
159    def doImport(self, clear_old_data=True, overwrite=True):
160        # CSVImporter instances have a `csvfile` and a `receiver`
161        # object defined which refer to the CSV file and the container.
162        for row in self.csvfile.getData():
163
164            new_item = createObject(u'waeup.Certificate')
165           
166            faculty_code = row['faculty_code']
167            faculty = self.receiver[faculty_code]
168            del row['faculty_code']
169
170            dept_code = row['department_code']
171            dept = faculty[dept_code]
172            del row['department_code']
173
174            for key, val in row.items():
175                setattr(new_item, key, val)
176            dept.certificates.addCertificate(new_item)
177        return
178
179class ICertificateCourseCSVFile(ICSVFile):
180    """A CSV file that contains certificate-course data.
181    """
182
183class CertificateCourseCSVFile(CSVFile):
184    """An abstraction of a CSV file containing certificate-courses.
185    """
186    grok.implements(ICertificateCourseCSVFile)
187    grok.provides(ICertificateCourseCSVFile)
188    required_fields = ['code', 'faculty_code', 'department_code',
189                       'certificate_code', 'level', 'core_or_elective']
190
191class CertificateCourseCSVImporter(CSVImporter):
192    """Shuffle data from certificate CSV files into faculty containers.
193    """
194    # Tell, what kinds of objects we connect...
195    grok.adapts(ICertificateCourseCSVFile, IFacultyContainer)
196    # Tell the world, that we are an importer...
197    grok.implements(IWAeUPCSVImporter)
198    grok.provides(IWAeUPCSVImporter)
199
200    datatype = u'Certificate Course Importer'
201
202    def doImport(self, clear_old_data=True, overwrite=True):
203        # CSVImporter instances have a `csvfile` and a `receiver`
204        # object defined which refer to the CSV file and the container.
205        for row in self.csvfile.getData():
206            new_item = createObject(u'waeup.CertificateCourse')
207            code = row['code']
208            del row['code']
209           
210            faculty_code = row['faculty_code']
211            faculty = self.receiver[faculty_code]
212            del row['faculty_code']
213
214            dept_code = row['department_code']
215            dept = faculty[dept_code]
216            del row['department_code']
217
218            cert_code = row['certificate_code']
219            cert = dept.certificates[cert_code]
220            del row['certificate_code']
221
222            course = self.getCourse(code)
223           
224            # Type casts...
225            row['core_or_elective'] = toBool(row['core_or_elective'])
226            row['level'] = int(row['level'])
227           
228            cert.addCourseRef(course, **row)
229        return
230
231    def getCourse(self, code):
232        """Get the course with code `code`.
233
234        Courses can be anywhere in the site. Therefore we ask the
235        catalog.
236        """
237        cat = getUtility(ICatalog, name='courses_catalog')
238        result = cat.searchResults(code=(code, code))
239        if len(result) < 1:
240            raise KeyError('No such course found: %s' % code)
241
242        # We might get more than one result, but this should not
243        # happen, as course codes are supposed to be unique.
244        return list(result)[0]
245
246       
Note: See TracBrowser for help on using the repository browser.