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 | # To do: Use reg_number and matric_number for locating as well. |
---|
50 | def entryExists(self, row, site): |
---|
51 | if row.has_key('student_id'): |
---|
52 | return row['student_id'] in site['students'].keys() |
---|
53 | return False |
---|
54 | |
---|
55 | def getParent(self, row, site): |
---|
56 | return site['students'] |
---|
57 | |
---|
58 | def getEntry(self, row, site): |
---|
59 | if not self.entryExists(row, site): |
---|
60 | return None |
---|
61 | parent = self.getParent(row, site) |
---|
62 | return parent.get(row['student_id']) |
---|
63 | |
---|
64 | def addEntry(self, obj, row, site): |
---|
65 | parent = self.getParent(row, site) |
---|
66 | parent.addStudent(obj) |
---|
67 | return |
---|
68 | |
---|
69 | def delEntry(self, row, site): |
---|
70 | parent = self.getParent(row, site) |
---|
71 | del parent[row['student_id']] |
---|
72 | pass |
---|
73 | |
---|
74 | class StudentStudyCourseProcessor(BatchProcessor): |
---|
75 | """A batch processor for IStudentStudyCourse objects. |
---|
76 | """ |
---|
77 | grok.implements(IBatchProcessor) |
---|
78 | grok.provides(IBatchProcessor) |
---|
79 | grok.context(Interface) |
---|
80 | util_name = 'studycourseupdater' |
---|
81 | grok.name(util_name) |
---|
82 | |
---|
83 | name = u'StudentStudyCourse Importer (update only)' |
---|
84 | iface = IStudentStudyCourseImport |
---|
85 | factory_name = 'waeup.StudentStudyCourse' |
---|
86 | |
---|
87 | mode = None |
---|
88 | |
---|
89 | @property |
---|
90 | def available_fields(self): |
---|
91 | result = [] |
---|
92 | return sorted(list(set( |
---|
93 | ['student_id','reg_number','matric_number'] + getFields( |
---|
94 | self.iface).keys()))) |
---|
95 | |
---|
96 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
97 | if not 'reg_number' in headerfields and not 'student_id' in headerfields and not 'matric_number' in headerfields: |
---|
98 | raise FatalCSVError( |
---|
99 | "Need at least columns student_id or reg_number or matric_number for import!") |
---|
100 | # Check for fields to be ignored... |
---|
101 | not_ignored_fields = [x for x in headerfields |
---|
102 | if not x.startswith('--')] |
---|
103 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
104 | raise FatalCSVError( |
---|
105 | "Double headers: each column name may only appear once.") |
---|
106 | return True |
---|
107 | |
---|
108 | def parentsExist(self, row, site): |
---|
109 | if not 'students' in site.keys() and row['student_id']: |
---|
110 | return False |
---|
111 | if 'student_id' in row.keys(): |
---|
112 | if row['student_id'] in site['students']: |
---|
113 | student = site['students'][row['student_id']] |
---|
114 | return student |
---|
115 | elif 'reg_number' in row.keys() and row['reg_number']: |
---|
116 | reg_number = row['reg_number'] |
---|
117 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
118 | results = list( |
---|
119 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
120 | if results: |
---|
121 | return results[0] |
---|
122 | elif 'matric_number' in row.keys() and row['matric_number']: |
---|
123 | #import pdb; pdb.set_trace() |
---|
124 | matric_number = row['matric_number'] |
---|
125 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
126 | results = list( |
---|
127 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
128 | if results: |
---|
129 | return results[0] |
---|
130 | return False |
---|
131 | |
---|
132 | def entryExists(self, row, site): |
---|
133 | student = self.parentsExist(row, site) |
---|
134 | if not student: |
---|
135 | return False |
---|
136 | if 'studycourse' in student: |
---|
137 | return student |
---|
138 | return False |
---|
139 | |
---|
140 | def getEntry(self, row, site): |
---|
141 | student = self.entryExists(row, site) |
---|
142 | if not student: |
---|
143 | return None |
---|
144 | return student.get('studycourse') |
---|