source: main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/university/batching.py @ 5314

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

Move academics-specific components from utils.batching to university.batching.

File size: 7.6 KB
Line 
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)
188        parent.addCertificate(obj)
189        return
190
191    def delEntry(self, row, site):
192        parent = self.getParent(row, site)
193        del parent[row['code']]
194        return
195
196class CertificateCourseProcessor(BatchProcessor):
197    """A batch processor for ICertificateCourse objects.
198    """
199    grok.provides(IBatchProcessor)
200    grok.context(Interface)
201    util_name = 'certificatecourseimporter'
202    grok.name(util_name)
203
204    name = u'CertificateCourse importer'
205    iface = ICertificateCourse
206
207    location_fields = ['course', 'level', 'faculty_code', 'department_code',
208                       'certificate_code',]
209    factory_name = 'waeup.CertificateCourse'
210
211    def parentsExist(self, row, site):
212        if not 'faculties' in site.keys():
213            return False
214        if not row['faculty_code'] in site['faculties'].keys():
215            return False
216        faculty = site['faculties'][row['faculty_code']]
217        if not row['department_code'] in faculty.keys():
218            return False
219        dept = faculty[row['department_code']]
220        return row['certificate_code'] in dept.certificates.keys()
221
222    def entryExists(self, row, site):
223        if not self.parentsExist(row, site):
224            return False
225        parent = self.getParent(row, site)
226        code = "%s_%s" % (row['course'].code, row['level'])
227        return code in parent.keys()
228
229    def getParent(self, row, site):
230        dept = site['faculties'][row['faculty_code']][row['department_code']]
231        return dept.certificates[row['certificate_code']]
232
233    def getEntry(self, row, site):
234        if not self.entryExists(row, site):
235            return None
236        parent = self.getParent(row, site)
237        return parent.get(row['course'])
238
239    def addEntry(self, obj, row, site):
240        parent = self.getParent(row, site)
241        parent.addCourseRef(row['course'],
242                            row['level'], row['core_or_elective'])
243        return
244
245    def delEntry(self, row, site):
246        parent = self.getParent(row, site)
247        parent.delCourseRef(row['course'].code, row['level'])
248        return
Note: See TracBrowser for help on using the repository browser.