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 zope.schema import getFields |
---|
12 | from zope.component import queryUtility |
---|
13 | from zope.catalog.interfaces import ICatalog |
---|
14 | from waeup.sirp.interfaces import IBatchProcessor, FatalCSVError |
---|
15 | from waeup.sirp.students.interfaces import ( |
---|
16 | IStudent, IStudentStudyCourse, IStudentStudyCourseImport) |
---|
17 | from waeup.sirp.utils.batching import BatchProcessor |
---|
18 | |
---|
19 | class StudentProcessor(BatchProcessor): |
---|
20 | """A batch processor for IStudent objects. |
---|
21 | """ |
---|
22 | grok.implements(IBatchProcessor) |
---|
23 | grok.provides(IBatchProcessor) |
---|
24 | grok.context(Interface) |
---|
25 | util_name = 'studentimporter' |
---|
26 | grok.name(util_name) |
---|
27 | |
---|
28 | name = u'Student Importer' |
---|
29 | iface = IStudent |
---|
30 | |
---|
31 | location_fields = ['student_id',] |
---|
32 | factory_name = 'waeup.Student' |
---|
33 | |
---|
34 | mode = None |
---|
35 | |
---|
36 | @property |
---|
37 | def req(self): |
---|
38 | result = dict( |
---|
39 | create = self.required_fields, |
---|
40 | update = self.location_fields, |
---|
41 | remove = self.location_fields, |
---|
42 | ) |
---|
43 | return result |
---|
44 | |
---|
45 | def parentsExist(self, row, site): |
---|
46 | return 'students' in site.keys() |
---|
47 | |
---|
48 | # The entry never exists in create mode. |
---|
49 | def entryExists(self, row, site): |
---|
50 | if row.has_key('student_id'): |
---|
51 | return row['student_id'] in site['students'].keys() |
---|
52 | return False |
---|
53 | |
---|
54 | def getParent(self, row, site): |
---|
55 | return site['students'] |
---|
56 | |
---|
57 | def getEntry(self, row, site): |
---|
58 | if not self.entryExists(row, site): |
---|
59 | return None |
---|
60 | parent = self.getParent(row, site) |
---|
61 | return parent.get(row['student_id']) |
---|
62 | |
---|
63 | def addEntry(self, obj, row, site): |
---|
64 | parent = self.getParent(row, site) |
---|
65 | parent.addStudent(obj) |
---|
66 | return |
---|
67 | |
---|
68 | def delEntry(self, row, site): |
---|
69 | parent = self.getParent(row, site) |
---|
70 | del parent[row['student_id']] |
---|
71 | pass |
---|
72 | |
---|
73 | class StudentStudyCourseProcessor(BatchProcessor): |
---|
74 | """A batch processor for IStudentStudyCourse objects. |
---|
75 | """ |
---|
76 | grok.implements(IBatchProcessor) |
---|
77 | grok.provides(IBatchProcessor) |
---|
78 | grok.context(Interface) |
---|
79 | util_name = 'studycourseimporter' |
---|
80 | grok.name(util_name) |
---|
81 | |
---|
82 | name = u'StudentStudyCourse Importer' |
---|
83 | iface = IStudentStudyCourseImport |
---|
84 | factory_name = 'waeup.StudentStudyCourse' |
---|
85 | |
---|
86 | @property |
---|
87 | def available_fields(self): |
---|
88 | result = [] |
---|
89 | return sorted(list(set( |
---|
90 | ['student_id','reg_number'] + getFields(self.iface).keys()))) |
---|
91 | |
---|
92 | def checkHeaders(self, headerfields, mode='update'): |
---|
93 | if not 'reg_number' in headerfields and not 'student_id' in headerfields: |
---|
94 | raise FatalCSVError( |
---|
95 | "Need at least columns student_id or reg_number for import!") |
---|
96 | # Check for fields to be ignored... |
---|
97 | not_ignored_fields = [x for x in headerfields |
---|
98 | if not x.startswith('--')] |
---|
99 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
100 | raise FatalCSVError( |
---|
101 | "Double headers: each column name may only appear once.") |
---|
102 | return True |
---|
103 | |
---|
104 | def parentsExist(self, row, site): |
---|
105 | if not 'students' in site.keys(): |
---|
106 | return False |
---|
107 | if 'student_id' in row.keys(): |
---|
108 | if row['student_id'] in site['students']: |
---|
109 | student = site['students'][row['student_id']] |
---|
110 | return student |
---|
111 | else: |
---|
112 | # Here we know that the reg_number is in row |
---|
113 | reg_number = row['reg_number'] |
---|
114 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
115 | results = list( |
---|
116 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
117 | if results: |
---|
118 | return results[0] |
---|
119 | return False |
---|
120 | |
---|
121 | def entryExists(self, row, site): |
---|
122 | student = self.parentsExist(row, site) |
---|
123 | if not student: |
---|
124 | return False |
---|
125 | if 'studycourse' in student: |
---|
126 | return student |
---|
127 | return False |
---|
128 | |
---|
129 | def getEntry(self, row, site): |
---|
130 | student = self.entryExists(row, site) |
---|
131 | if not student: |
---|
132 | return None |
---|
133 | return student.get('studycourse') |
---|