source: waeup/branches/ulif-rewrite/src/waeup/utils/csvimport.py @ 4390

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

Move imports for CSV imports to new module.

File size: 7.5 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.component import createObject
9
10#
11# CSV import stuff
12#
13class IFacultyCSVFile(ICSVFile):
14    """A CSV file that contains faculty data.
15    """
16
17class FacultyCSVFile(CSVFile):
18    """An abstraction of a CSV file containing faculties.
19    """
20    grok.implements(IFacultyCSVFile)
21    grok.provides(IFacultyCSVFile)
22    required_fields = ['code', 'title', 'title_prefix']
23
24class FacultyCSVImporter(CSVImporter):
25    """Shuffle data from faculty CSV files into faculty containers.
26    """
27    # Tell, what kinds of objects we connect...
28    grok.adapts(IFacultyCSVFile, IFacultyContainer)
29    # Tell the world, that we are an importer...
30    grok.implements(IWAeUPCSVImporter)
31    grok.provides(IWAeUPCSVImporter)
32
33    datatype = u'Faculty Importer'
34
35    def doImport(self, clear_old_data=True, overwrite=True):
36        # CSVImporter instances have a `csvfile` and a `receiver`
37        # object defined which refer to the CSV file and the container.
38        for row in self.csvfile.getData():
39            new_item = createObject(u'waeup.Faculty')
40            for key, val in row.items():
41                setattr(new_item, key, val)
42            self.receiver.addFaculty(new_item)
43        return
44
45class IDepartmentCSVFile(ICSVFile):
46    """A CSV file that contains department data.
47    """
48
49class DepartmentCSVFile(CSVFile):
50    """An abstraction of a CSV file containing departments.
51    """
52    grok.implements(IDepartmentCSVFile)
53    grok.provides(IDepartmentCSVFile)
54    required_fields = ['code', 'title', 'title_prefix', 'faculty_code']
55
56class DepartmentCSVImporter(CSVImporter):
57    """Shuffle data from department CSV files into faculty containers.
58    """
59    # Tell, what kinds of objects we connect...
60    grok.adapts(IDepartmentCSVFile, IFacultyContainer)
61    # Tell the world, that we are an importer...
62    grok.implements(IWAeUPCSVImporter)
63    grok.provides(IWAeUPCSVImporter)
64
65    datatype = u'Department Importer'
66
67    def doImport(self, clear_old_data=True, overwrite=True):
68        # CSVImporter instances have a `csvfile` and a `receiver`
69        # object defined which refer to the CSV file and the container.
70        for row in self.csvfile.getData():
71            new_item = createObject(u'waeup.Department')
72            faculty_code = row['faculty_code']
73            faculty = self.receiver[faculty_code]
74            del row['faculty_code']
75            for key, val in row.items():
76                setattr(new_item, key, val)
77            faculty.addDepartment(new_item)
78        return
79
80class ICourseCSVFile(ICSVFile):
81    """A CSV file that contains course data.
82    """
83
84class CourseCSVFile(CSVFile):
85    """An abstraction of a CSV file containing courses.
86    """
87    grok.implements(ICourseCSVFile)
88    grok.provides(ICourseCSVFile)
89    required_fields = ['code', 'title', 'level', 'passmark', 'credits',
90                       'semester', 'faculty', 'department']
91
92class CourseCSVImporter(CSVImporter):
93    """Shuffle data from course CSV files into faculty containers.
94    """
95    # Tell, what kinds of objects we connect...
96    grok.adapts(ICourseCSVFile, IFacultyContainer)
97    # Tell the world, that we are an importer...
98    grok.implements(IWAeUPCSVImporter)
99    grok.provides(IWAeUPCSVImporter)
100
101    datatype = u'Course Importer'
102
103    def doImport(self, clear_old_data=True, overwrite=True):
104        # CSVImporter instances have a `csvfile` and a `receiver`
105        # object defined which refer to the CSV file and the container.
106        for row in self.csvfile.getData():
107
108            new_item = createObject(u'waeup.Course')
109           
110            faculty_code = row['faculty']
111            faculty = self.receiver[faculty_code]
112            del row['faculty']
113
114            dept_code = row['department']
115            dept = faculty[dept_code]
116            del row['department']
117
118            for key, val in row.items():
119                setattr(new_item, key, val)
120            dept.courses.addCourse(new_item)
121        return
122
123class ICertificateCSVFile(ICSVFile):
124    """A CSV file that contains certificate data.
125    """
126
127class CertificateCSVFile(CSVFile):
128    """An abstraction of a CSV file containing certificates.
129    """
130    grok.implements(ICertificateCSVFile)
131    grok.provides(ICertificateCSVFile)
132    required_fields = ['code', 'title', 'faculty_code', 'department_code',
133                       'category', 'study_mode', 'start_level', 'end_level',
134                       'm_prefix', 'max_pass', 'application_category']
135
136class CertificateCSVImporter(CSVImporter):
137    """Shuffle data from certificate CSV files into faculty containers.
138    """
139    # Tell, what kinds of objects we connect...
140    grok.adapts(ICertificateCSVFile, IFacultyContainer)
141    # Tell the world, that we are an importer...
142    grok.implements(IWAeUPCSVImporter)
143    grok.provides(IWAeUPCSVImporter)
144
145    datatype = u'Certificate Importer'
146
147    def doImport(self, clear_old_data=True, overwrite=True):
148        # CSVImporter instances have a `csvfile` and a `receiver`
149        # object defined which refer to the CSV file and the container.
150        for row in self.csvfile.getData():
151
152            new_item = createObject(u'waeup.Certificate')
153           
154            faculty_code = row['faculty_code']
155            faculty = self.receiver[faculty_code]
156            del row['faculty_code']
157
158            dept_code = row['department_code']
159            dept = faculty[dept_code]
160            del row['department_code']
161
162            for key, val in row.items():
163                setattr(new_item, key, val)
164            dept.certificates.addCertificate(new_item)
165        return
166
167class ICertificateCourseCSVFile(ICSVFile):
168    """A CSV file that contains certificate-course data.
169    """
170
171class CertificateCourseCSVFile(CSVFile):
172    """An abstraction of a CSV file containing certificate-courses.
173    """
174    grok.implements(ICertificateCourseCSVFile)
175    grok.provides(ICertificateCourseCSVFile)
176    required_fields = ['code', 'faculty_code', 'department_code',
177                       'certificate_code', 'level', 'core_or_elective']
178
179class CertificateCourseCSVImporter(CSVImporter):
180    """Shuffle data from certificate CSV files into faculty containers.
181    """
182    # Tell, what kinds of objects we connect...
183    grok.adapts(ICertificateCourseCSVFile, IFacultyContainer)
184    # Tell the world, that we are an importer...
185    grok.implements(IWAeUPCSVImporter)
186    grok.provides(IWAeUPCSVImporter)
187
188    datatype = u'Certificate Importer'
189
190    def doImport(self, clear_old_data=True, overwrite=True):
191        # CSVImporter instances have a `csvfile` and a `receiver`
192        # object defined which refer to the CSV file and the container.
193        for row in self.csvfile.getData():
194
195            new_item = createObject(u'waeup.CertificateCourse')
196            code = row['code']
197            del row['code']
198           
199            faculty_code = row['faculty_code']
200            faculty = self.receiver[faculty_code]
201            del row['faculty_code']
202
203            dept_code = row['department_code']
204            dept = faculty[dept_code]
205            del row['department_code']
206
207            course = dept.courses[code]
208
209            cert_code = row['certificate_code']
210            cert = dept.certificates[cert_code]
211            del row['certificate_code']
212
213            # Type casts...
214            row['core_or_elective'] = toBool(row['core_or_elective'])
215            row['level'] = int(row['level'])
216           
217            cert.addCourseRef(course, **row)
218        return
219
Note: See TracBrowser for help on using the repository browser.