~aelkner/schooltool/schooltool.sla_august_fixes

« back to all changes in this revision

Viewing changes to src/schooltool/intervention/browser/csvimport.py

  • Committer: Justas Sadzevicius
  • Date: 2009-08-14 09:48:48 UTC
  • mfrom: (163.1.19 sla)
  • Revision ID: justas@pov.lt-20090814094848-ajt79v222ovkv9tj
Merge sla intervetion migration to the gradebook package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# SchoolTool - common information systems platform for school administration
3
 
# Copyright (c) 2005 Shuttleworth Foundation
4
 
#
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.
9
 
#
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.
14
 
#
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
18
 
#
19
 
"""
20
 
SLA person browser views.
21
 
 
22
 
$Id: person.py 7254 2007-10-15 16:20:41Z ignas $
23
 
"""
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
28
 
 
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
38
 
 
39
 
 
40
 
class PersonCSVImporter(BaseCSVImporter):
41
 
    """A Person CSV importer."""
42
 
 
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.
46
 
 
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.
50
 
        """
51
 
        if len(data) < 3:
52
 
            self.errors.fields.append(_("""Insufficient data provided."""))
53
 
            return
54
 
 
55
 
        if not data[2]:
56
 
            self.errors.fields.append(_('username may not be empty'))
57
 
            return
58
 
 
59
 
        # pad out to 28 fields
60
 
        data = (data + [''] * 25)[:28]
61
 
 
62
 
        if len(data[4].split('/')) > 2:
63
 
            self.errors.fields.append(_('Invalid advisors field: %s.' % data[3]))
64
 
            return
65
 
 
66
 
        if len(data[6].split(',')) > 2:
67
 
            self.errors.fields.append(_('Invalid city/state field: %s.' % data[5]))
68
 
            return
69
 
 
70
 
        if len(data[13].split(',')) > 2:
71
 
            self.errors.fields.append(_('Invalid city/state2 field: %s.' % data[12]))
72
 
            return
73
 
 
74
 
        if len(data[22].split(',')) > 2:
75
 
            self.errors.fields.append(_('Invalid city/state3 field: %s.' % data[21]))
76
 
            return
77
 
 
78
 
        username = data[2]
79
 
        district_id = data[3]
80
 
 
81
 
        if username in self.container:
82
 
            error_msg = _("Duplicate username: ${username}",
83
 
                          mapping={'username' : ', '.join(data)})
84
 
            self.errors.fields.append(error_msg)
85
 
            return
86
 
 
87
 
        obj = BasicPerson(username, data[1], data[0], data[18], data[8])
88
 
        obj.setPassword(username)
89
 
        
90
 
        if not dry_run:
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]
100
 
            demos.zip = data[7]
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]
121
 
 
122
 
            term = queryUtility(IDateManager).current_term
123
 
            groups = IGroupContainer(ISchoolYear(term))
124
 
            if district_id:
125
 
                obj.groups.add(groups['students'])
126
 
            else:
127
 
                obj.groups.add(groups['teachers'])
128
 
 
129
 
 
130
 
class PersonCSVImportView(BaseCSVImportView):
131
 
    """View for Person CSV importer."""
132
 
 
133
 
    importer_class = PersonCSVImporter
134