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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 8.6 KB
Line 
1## $Id: batching.py 7195 2011-11-25 07:34:07Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Batch processing components for academics objects.
19
20Batch processors eat CSV files to add, update or remove large numbers
21of certain kinds of objects at once.
22
23Here we define the processors for academics specific objects like
24faculties, departments and the like.
25"""
26import grok
27from zope.interface import Interface
28from waeup.sirp.interfaces import IBatchProcessor
29from waeup.sirp.university.interfaces import (
30    IFacultyContainer, IFaculty, ICourse, IDepartment, ICertificate,
31    ICertificateCourse)
32from waeup.sirp.utils.batching import BatchProcessor
33
34class FacultyProcessor(BatchProcessor):
35    """A batch processor for IFaculty objects.
36    """
37    grok.implements(IBatchProcessor)
38    grok.provides(IBatchProcessor)
39    grok.context(Interface)
40    util_name = 'facultyimporter'
41    grok.name(util_name)
42
43    name = u'Faculty Importer'
44    iface = IFaculty
45
46    location_fields = ['code',]
47    factory_name = 'waeup.Faculty'
48
49    mode = None
50
51    def parentsExist(self, row, site):
52        return 'faculties' in site.keys()
53
54    def entryExists(self, row, site):
55        return row['code'] in site['faculties'].keys()
56
57    def getParent(self, row, site):
58        return site['faculties']
59
60    def getEntry(self, row, site):
61        if not self.entryExists(row, site):
62            return None
63        parent = self.getParent(row, site)
64        return parent.get(row['code'])
65
66    def addEntry(self, obj, row, site):
67        parent = self.getParent(row, site)
68        parent.addFaculty(obj)
69        return
70
71    def delEntry(self, row, site):
72        parent = self.getParent(row, site)
73        del parent[row['code']]
74        pass
75
76class DepartmentProcessor(BatchProcessor):
77    """A batch processor for IDepartment objects.
78    """
79    grok.implements(IBatchProcessor)
80    grok.provides(IBatchProcessor)
81    grok.context(Interface)
82    util_name = 'departmentimporter'
83    grok.name(util_name)
84
85    name = u'Department Importer'
86    iface = IDepartment
87
88    location_fields = ['code', 'faculty_code']
89    factory_name = 'waeup.Department'
90
91    mode = None
92
93    def parentsExist(self, row, site):
94        if not 'faculties' in site.keys():
95            return False
96        return row['faculty_code'] in site['faculties']
97
98    def entryExists(self, row, site):
99        if not self.parentsExist(row, site):
100            return False
101        parent = self.getParent(row, site)
102        return row['code'] in parent.keys()
103
104    def getParent(self, row, site):
105        return site['faculties'][row['faculty_code']]
106
107    def getEntry(self, row, site):
108        if not self.entryExists(row, site):
109            return None
110        parent = self.getParent(row, site)
111        return parent.get(row['code'])
112
113    def addEntry(self, obj, row, site):
114        parent = self.getParent(row, site)
115        parent.addDepartment(obj)
116        return
117
118    def delEntry(self, row, site):
119        parent = self.getParent(row, site)
120        del parent[row['code']]
121        return
122
123class CourseProcessor(BatchProcessor):
124    """A batch processor for ICourse objects.
125    """
126    grok.implements(IBatchProcessor)
127    grok.provides(IBatchProcessor)
128    grok.context(Interface)
129    util_name = 'courseimporter'
130    grok.name(util_name)
131
132    name = u'Course Importer'
133    iface = ICourse
134
135    location_fields = ['code', 'faculty_code', 'department_code']
136    factory_name = 'waeup.Course'
137
138    mode = None
139
140    def parentsExist(self, row, site):
141        if not 'faculties' in site.keys():
142            return False
143        if not row['faculty_code'] in site['faculties'].keys():
144            return False
145        faculty = site['faculties'][row['faculty_code']]
146        return row['department_code'] in faculty.keys()
147
148    def entryExists(self, row, site):
149        if not self.parentsExist(row, site):
150            return False
151        parent = self.getParent(row, site)
152        return row['code'] in parent.keys()
153
154    def getParent(self, row, site):
155        dept = site['faculties'][row['faculty_code']][row['department_code']]
156        return dept.courses
157
158    def getEntry(self, row, site):
159        if not self.entryExists(row, site):
160            return None
161        parent = self.getParent(row, site)
162        return parent.get(row['code'])
163
164    def addEntry(self, obj, row, site):
165        parent = self.getParent(row, site)
166        parent.addCourse(obj)
167        return
168
169    def delEntry(self, row, site):
170        parent = self.getParent(row, site)
171        del parent[row['code']]
172        return
173
174class CertificateProcessor(BatchProcessor):
175    """A batch processor for ICertificate objects.
176    """
177    grok.implements(IBatchProcessor)
178    grok.provides(IBatchProcessor)
179    grok.context(Interface)
180    util_name = 'certificateimporter'
181    grok.name(util_name)
182
183    name = u'Certificate Importer'
184    iface = ICertificate
185
186    location_fields = ['code', 'faculty_code', 'department_code']
187    factory_name = 'waeup.Certificate'
188
189    mode = None
190
191    def parentsExist(self, row, site):
192        if not 'faculties' in site.keys():
193            return False
194        if not row['faculty_code'] in site['faculties'].keys():
195            return False
196        faculty = site['faculties'][row['faculty_code']]
197        return row['department_code'] in faculty.keys()
198
199    def entryExists(self, row, site):
200        if not self.parentsExist(row, site):
201            return False
202        parent = self.getParent(row, site)
203        return row['code'] in parent.keys()
204
205    def getParent(self, row, site):
206        dept = site['faculties'][row['faculty_code']][row['department_code']]
207        return dept.certificates
208
209    def getEntry(self, row, site):
210        if not self.entryExists(row, site):
211            return None
212        parent = self.getParent(row, site)
213        return parent.get(row['code'])
214
215    def addEntry(self, obj, row, site):
216        parent = self.getParent(row, site)
217        parent.addCertificate(obj)
218        return
219
220    def delEntry(self, row, site):
221        parent = self.getParent(row, site)
222        del parent[row['code']]
223        return
224
225class CertificateCourseProcessor(BatchProcessor):
226    """A batch processor for ICertificateCourse objects.
227    """
228    grok.implements(IBatchProcessor)
229    grok.provides(IBatchProcessor)
230    grok.context(Interface)
231    util_name = 'certificatecourseimporter'
232    grok.name(util_name)
233
234    name = u'CertificateCourse Importer'
235    iface = ICertificateCourse
236
237    location_fields = ['course', 'level', 'faculty_code', 'department_code',
238                       'certificate_code',]
239    factory_name = 'waeup.CertificateCourse'
240
241    mode = None
242
243    def parentsExist(self, row, site):
244        if not 'faculties' in site.keys():
245            return False
246        if not row['faculty_code'] in site['faculties'].keys():
247            return False
248        faculty = site['faculties'][row['faculty_code']]
249        if not row['department_code'] in faculty.keys():
250            return False
251        dept = faculty[row['department_code']]
252        return row['certificate_code'] in dept.certificates.keys()
253
254    def entryExists(self, row, site):
255        if not self.parentsExist(row, site):
256            return False
257        parent = self.getParent(row, site)
258        code = "%s_%s" % (row['course'].code, row['level'])
259        return code in parent.keys()
260
261    def getParent(self, row, site):
262        dept = site['faculties'][row['faculty_code']][row['department_code']]
263        return dept.certificates[row['certificate_code']]
264
265    def getEntry(self, row, site):
266        if not self.entryExists(row, site):
267            return None
268        parent = self.getParent(row, site)
269        return parent.get("%s_%s" % (row['course'].code, row['level']))
270
271    def addEntry(self, obj, row, site):
272        parent = self.getParent(row, site)
273        parent.addCourseRef(row['course'],
274                            row['level'], row['core_or_elective'])
275        return
276
277    def delEntry(self, row, site):
278        parent = self.getParent(row, site)
279        parent.delCourseRef(row['course'].code, row['level'])
280        return
Note: See TracBrowser for help on using the repository browser.