source: main/waeup.sirp/trunk/src/waeup/sirp/university/batching.py @ 6219

Last change on this file since 6219 was 6219, checked in by Henrik Bettermann, 13 years ago

Use the message output of CertificateContainer?.addCertificate in CertificateProcessor?.addEntry and finally in BatchProcessor?.doImport to notify that the import failed. This can maybe merged with CertificateProcessor?.entryExists?!

File size: 7.6 KB
RevLine 
[5009]1"""Batch processing components for academics objects.
2
3Batch processors eat CSV files to add, update or remove large numbers
4of certain kinds of objects at once.
5
6Here we define the processors for academics specific objects like
7faculties, departments and the like.
8"""
9import grok
10from zope.interface import Interface
11from waeup.sirp.interfaces import IBatchProcessor
12from waeup.sirp.university.interfaces import (
13    IFacultyContainer, IFaculty, ICourse, IDepartment, ICertificate,
14    ICertificateCourse)
15from waeup.sirp.utils.batching import BatchProcessor
16
17class FacultyProcessor(BatchProcessor):
18    """A batch processor for IFaculty objects.
19    """
20    grok.provides(IBatchProcessor)
21    grok.context(Interface)
22    util_name = 'facultyimporter'
23    grok.name(util_name)
24
25    name = u'Faculty importer'
26    iface = IFaculty
27
28    location_fields = ['code',]
29    factory_name = 'waeup.Faculty'
30
31    def parentsExist(self, row, site):
32        return 'faculties' in site.keys()
33
34    def entryExists(self, row, site):
35        return row['code'] in site['faculties'].keys()
36
37    def getParent(self, row, site):
38        return site['faculties']
39
40    def getEntry(self, row, site):
41        if not self.entryExists(row, site):
42            return None
43        parent = self.getParent(row, site)
44        return parent.get(row['code'])
45   
46    def addEntry(self, obj, row, site):
47        parent = self.getParent(row, site)
48        parent.addFaculty(obj)
49        return
50
51    def delEntry(self, row, site):
52        parent = self.getParent(row, site)
53        del parent[row['code']]
54        pass
55
56class DepartmentProcessor(BatchProcessor):
57    """A batch processor for IDepartment objects.
58    """
59    grok.provides(IBatchProcessor)
60    grok.context(Interface)
61    util_name = 'departmentimporter'
62    grok.name(util_name)
63
64    name = u'Department importer'
65    iface = IDepartment
66
67    location_fields = ['code', 'faculty_code']
68    factory_name = 'waeup.Department'
69
70    def parentsExist(self, row, site):
71        if not 'faculties' in site.keys():
72            return False
73        return row['faculty_code'] in site['faculties']
74
75    def entryExists(self, row, site):
76        if not self.parentsExist(row, site):
77            return False
78        parent = self.getParent(row, site)
79        return row['code'] in parent.keys()
80
81    def getParent(self, row, site):
82        return site['faculties'][row['faculty_code']]
83
84    def getEntry(self, row, site):
85        if not self.entryExists(row, site):
86            return None
87        parent = self.getParent(row, site)
88        return parent.get(row['code'])
89   
90    def addEntry(self, obj, row, site):
91        parent = self.getParent(row, site)
92        parent.addDepartment(obj)
93        return
94
95    def delEntry(self, row, site):
96        parent = self.getParent(row, site)
97        del parent[row['code']]
98        return
99
100class CourseProcessor(BatchProcessor):
101    """A batch processor for ICourse objects.
102    """
103    grok.provides(IBatchProcessor)
104    grok.context(Interface)
105    util_name = 'courseimporter'
106    grok.name(util_name)
107
108    name = u'Course importer'
109    iface = ICourse
110
111    location_fields = ['code', 'faculty_code', 'department_code']
112    factory_name = 'waeup.Course'
113
114    def parentsExist(self, row, site):
115        if not 'faculties' in site.keys():
116            return False
117        if not row['faculty_code'] in site['faculties'].keys():
118            return False
119        faculty = site['faculties'][row['faculty_code']]
120        return row['department_code'] in faculty.keys()
121
122    def entryExists(self, row, site):
123        if not self.parentsExist(row, site):
124            return False
125        parent = self.getParent(row, site)
126        return row['code'] in parent.keys()
127
128    def getParent(self, row, site):
129        dept = site['faculties'][row['faculty_code']][row['department_code']]
130        return dept.courses
131
132    def getEntry(self, row, site):
133        if not self.entryExists(row, site):
134            return None
135        parent = self.getParent(row, site)
136        return parent.get(row['code'])
137   
138    def addEntry(self, obj, row, site):
139        parent = self.getParent(row, site)
140        parent.addCourse(obj)
141        return
142
143    def delEntry(self, row, site):
144        parent = self.getParent(row, site)
145        del parent[row['code']]
146        return
147
148class CertificateProcessor(BatchProcessor):
149    """A batch processor for ICertificate objects.
150    """
151    grok.provides(IBatchProcessor)
152    grok.context(Interface)
153    util_name = 'certificateimporter'
154    grok.name(util_name)
155
156    name = u'Certificate importer'
157    iface = ICertificate
158
159    location_fields = ['code', 'faculty_code', 'department_code']
160    factory_name = 'waeup.Certificate'
161
162    def parentsExist(self, row, site):
163        if not 'faculties' in site.keys():
164            return False
165        if not row['faculty_code'] in site['faculties'].keys():
166            return False
167        faculty = site['faculties'][row['faculty_code']]
168        return row['department_code'] in faculty.keys()
169
170    def entryExists(self, row, site):
171        if not self.parentsExist(row, site):
172            return False
173        parent = self.getParent(row, site)
174        return row['code'] in parent.keys()
175
176    def getParent(self, row, site):
177        dept = site['faculties'][row['faculty_code']][row['department_code']]
178        return dept.certificates
179
180    def getEntry(self, row, site):
181        if not self.entryExists(row, site):
182            return None
183        parent = self.getParent(row, site)
184        return parent.get(row['code'])
185
186    def addEntry(self, obj, row, site):
187        parent = self.getParent(row, site)
[6219]188        message = parent.addCertificate(obj)
189        if not message == 'Certificate added.':
190            return False
191        return True
[5009]192
193    def delEntry(self, row, site):
194        parent = self.getParent(row, site)
195        del parent[row['code']]
196        return
197
198class CertificateCourseProcessor(BatchProcessor):
199    """A batch processor for ICertificateCourse objects.
200    """
201    grok.provides(IBatchProcessor)
202    grok.context(Interface)
203    util_name = 'certificatecourseimporter'
204    grok.name(util_name)
205
206    name = u'CertificateCourse importer'
207    iface = ICertificateCourse
208
209    location_fields = ['course', 'level', 'faculty_code', 'department_code',
210                       'certificate_code',]
211    factory_name = 'waeup.CertificateCourse'
212
213    def parentsExist(self, row, site):
214        if not 'faculties' in site.keys():
215            return False
216        if not row['faculty_code'] in site['faculties'].keys():
217            return False
218        faculty = site['faculties'][row['faculty_code']]
219        if not row['department_code'] in faculty.keys():
220            return False
221        dept = faculty[row['department_code']]
222        return row['certificate_code'] in dept.certificates.keys()
223
224    def entryExists(self, row, site):
225        if not self.parentsExist(row, site):
226            return False
227        parent = self.getParent(row, site)
228        code = "%s_%s" % (row['course'].code, row['level'])
229        return code in parent.keys()
230
231    def getParent(self, row, site):
232        dept = site['faculties'][row['faculty_code']][row['department_code']]
233        return dept.certificates[row['certificate_code']]
234
235    def getEntry(self, row, site):
236        if not self.entryExists(row, site):
237            return None
238        parent = self.getParent(row, site)
239        return parent.get(row['course'])
240
241    def addEntry(self, obj, row, site):
242        parent = self.getParent(row, site)
243        parent.addCourseRef(row['course'],
244                            row['level'], row['core_or_elective'])
245        return
246
247    def delEntry(self, row, site):
248        parent = self.getParent(row, site)
249        parent.delCourseRef(row['course'].code, row['level'])
250        return
Note: See TracBrowser for help on using the repository browser.