source: main/waeup.sirp/trunk/src/waeup/sirp/students/batching.py @ 6842

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

Fix errors reported by tests.

  • Property svn:keywords set to Id
File size: 4.2 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 zope.schema import getFields
12from zope.component import queryUtility
13from zope.catalog.interfaces import ICatalog
14from waeup.sirp.interfaces import IBatchProcessor, FatalCSVError
15from waeup.sirp.students.interfaces import (
16    IStudent, IStudentStudyCourse, IStudentStudyCourseImport)
17from waeup.sirp.utils.batching import BatchProcessor
18
19class 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
73class 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 = 'studycourseupdater'
80    grok.name(util_name)
81
82    name = u'StudentStudyCourse Importer (update only)'
83    iface = IStudentStudyCourseImport
84    factory_name = 'waeup.StudentStudyCourse'
85
86    mode = None
87
88    @property
89    def available_fields(self):
90        result = []
91        return sorted(list(set(
92            ['student_id','reg_number'] + getFields(self.iface).keys())))
93
94    def checkHeaders(self, headerfields, mode='ignore'):
95        if not 'reg_number' in headerfields and not 'student_id' in headerfields:
96            raise FatalCSVError(
97                "Need at least columns student_id or reg_number for import!")
98        # Check for fields to be ignored...
99        not_ignored_fields = [x for x in headerfields
100                              if not x.startswith('--')]
101        if len(set(not_ignored_fields)) < len(not_ignored_fields):
102            raise FatalCSVError(
103                "Double headers: each column name may only appear once.")
104        return True
105
106    def parentsExist(self, row, site):
107        if not 'students' in site.keys():
108            return False
109        if 'student_id' in row.keys():
110            if row['student_id'] in site['students']:
111                student = site['students'][row['student_id']]
112                return student
113        else:
114            # Here we know that the reg_number is in row
115            reg_number = row['reg_number']
116            cat = queryUtility(ICatalog, name='students_catalog')
117            results = list(
118                cat.searchResults(reg_number=(reg_number, reg_number)))
119            if results:
120                return results[0]
121        return False
122
123    def entryExists(self, row, site):
124        student = self.parentsExist(row, site)
125        if not student:
126            return False
127        if 'studycourse' in student:
128            return student
129        return False
130
131    def getEntry(self, row, site):
132        student = self.entryExists(row, site)
133        if not student:
134            return None
135        return student.get('studycourse')
Note: See TracBrowser for help on using the repository browser.