[6821] | 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.students.interfaces import IStudent |
---|
| 13 | from waeup.sirp.utils.batching import BatchProcessor |
---|
| 14 | |
---|
| 15 | class StudentProcessor(BatchProcessor): |
---|
| 16 | """A batch processor for IStudent objects. |
---|
| 17 | """ |
---|
| 18 | grok.implements(IBatchProcessor) |
---|
| 19 | grok.provides(IBatchProcessor) |
---|
| 20 | grok.context(Interface) |
---|
| 21 | util_name = 'studentimporter' |
---|
| 22 | grok.name(util_name) |
---|
| 23 | |
---|
| 24 | name = u'Student Importer' |
---|
| 25 | iface = IStudent |
---|
| 26 | |
---|
| 27 | location_fields = ['student_id',] |
---|
| 28 | factory_name = 'waeup.Student' |
---|
| 29 | |
---|
| 30 | mode = None |
---|
| 31 | |
---|
| 32 | @property |
---|
| 33 | def req(self): |
---|
| 34 | result = dict( |
---|
| 35 | create = self.required_fields, |
---|
| 36 | update = self.location_fields, |
---|
| 37 | remove = self.location_fields, |
---|
| 38 | ) |
---|
| 39 | return result |
---|
| 40 | |
---|
| 41 | def parentsExist(self, row, site): |
---|
| 42 | return 'students' in site.keys() |
---|
| 43 | |
---|
| 44 | # The entry never exists in create mode. |
---|
| 45 | def entryExists(self, row, site): |
---|
| 46 | if row.has_key('student_id'): |
---|
| 47 | return row['student_id'] in site['students'].keys() |
---|
| 48 | return False |
---|
| 49 | |
---|
| 50 | def getParent(self, row, site): |
---|
| 51 | return site['students'] |
---|
| 52 | |
---|
| 53 | def getEntry(self, row, site): |
---|
| 54 | if not self.entryExists(row, site): |
---|
| 55 | return None |
---|
| 56 | parent = self.getParent(row, site) |
---|
| 57 | return parent.get(row['student_id']) |
---|
| 58 | |
---|
| 59 | def addEntry(self, obj, row, site): |
---|
| 60 | parent = self.getParent(row, site) |
---|
| 61 | parent.addStudent(obj) |
---|
| 62 | return |
---|
| 63 | |
---|
| 64 | def delEntry(self, row, site): |
---|
| 65 | parent = self.getParent(row, site) |
---|
| 66 | del parent[row['student_id']] |
---|
| 67 | pass |
---|