2
# SchoolTool - common information systems platform for school administration
3
# Copyright (c) 2005 Shuttleworth Foundation
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
SLA person browser views.
22
$Id: person.py 7254 2007-10-15 16:20:41Z ignas $
24
from zope.app.container.interfaces import INameChooser
25
from zope.component import queryUtility
26
from zope.event import notify
27
from zope.lifecycleevent import ObjectModifiedEvent
29
from schooltool.app.browser.csvimport import BaseCSVImporter, BaseCSVImportView
30
from schooltool.app.interfaces import ISchoolToolApplication
31
from schooltool.basicperson.interfaces import IBasicPerson
32
from schooltool.basicperson.person import BasicPerson
33
from schooltool.common import SchoolToolMessage as _
34
from schooltool.group.interfaces import IGroupContainer
35
from schooltool.intervention.interfaces import IDemographics
36
from schooltool.schoolyear.interfaces import ISchoolYear
37
from schooltool.term.interfaces import IDateManager
40
class PersonCSVImporter(BaseCSVImporter):
41
"""A Person CSV importer."""
43
def createAndAdd(self, data, dry_run=True):
44
"""Create BasicPerson object and add to container.
45
Also create corresponding Demographics object and add to its container.
47
We are requiring that we have a last name, first name, and username
48
set. If any duplicates are found then an error is reported and the
49
duplicate entries are reported back to the user.
52
self.errors.fields.append(_("""Insufficient data provided."""))
56
self.errors.fields.append(_('username may not be empty'))
59
# pad out to 28 fields
60
data = (data + [''] * 25)[:28]
62
if len(data[4].split('/')) > 2:
63
self.errors.fields.append(_('Invalid advisors field: %s.' % data[3]))
66
if len(data[6].split(',')) > 2:
67
self.errors.fields.append(_('Invalid city/state field: %s.' % data[5]))
70
if len(data[13].split(',')) > 2:
71
self.errors.fields.append(_('Invalid city/state2 field: %s.' % data[12]))
74
if len(data[22].split(',')) > 2:
75
self.errors.fields.append(_('Invalid city/state3 field: %s.' % data[21]))
81
if username in self.container:
82
error_msg = _("Duplicate username: ${username}",
83
mapping={'username' : ', '.join(data)})
84
self.errors.fields.append(error_msg)
87
obj = BasicPerson(username, data[1], data[0], data[18], data[8])
88
obj.setPassword(username)
91
self.container[username] = obj
92
notify(ObjectModifiedEvent(obj))
93
demos = IDemographics(obj)
94
demos.district_id = district_id
95
advisors = [fld.strip() for fld in data[4].split('/')]
96
demos.advisor1, demos.advisor2 = (advisors + [''])[:2]
97
demos.address = data[5]
98
citystate = [fld.strip() for fld in data[6].split(',')]
99
demos.city, demos.state = (citystate + [''])[:2]
101
demos.cell_phone = data[9]
102
demos.parent_first_name = data[10]
103
demos.parent_last_name = data[11]
104
demos.address2 = data[12]
105
citystate2 = [fld.strip() for fld in data[13].split(',')]
106
demos.city2, demos.state2 = (citystate2 + [''])[:2]
107
demos.zip2 = data[14]
108
demos.home_phone2 = data[15]
109
demos.cell_phone2 = data[16]
110
demos.work_phone = data[17]
111
demos.parent2_first_name = data[19]
112
demos.parent2_last_name = data[20]
113
demos.address3 = data[21]
114
citystate3 = [fld.strip() for fld in data[22].split(',')]
115
demos.city3, demos.state3 = (citystate3 + [''])[:2]
116
demos.zip3 = data[23]
117
demos.home_phone3 = data[24]
118
demos.cell_phone3 = data[25]
119
demos.work_phone2 = data[26]
120
demos.email2 = data[27]
122
term = queryUtility(IDateManager).current_term
123
groups = IGroupContainer(ISchoolYear(term))
125
obj.groups.add(groups['students'])
127
obj.groups.add(groups['teachers'])
130
class PersonCSVImportView(BaseCSVImportView):
131
"""View for Person CSV importer."""
133
importer_class = PersonCSVImporter