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