~ubuntu-branches/ubuntu/raring/schooltool.intervention/raring

« back to all changes in this revision

Viewing changes to src/schooltool/intervention/sendmail.py

  • Committer: Gediminas Paulauskas
  • Date: 2011-09-19 16:56:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: menesis@pov.lt-20110919165645-718diuud5tc4mjsx
Tags: 0.5.0-0ubuntu1
* New upstream release.
* debian/rules: move gradebook to Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from zope.traversing.browser.absoluteurl import absoluteURL
25
25
from zope.i18n import translate
26
26
 
 
27
from schooltool.app.interfaces import ISchoolToolApplication
 
28
from schooltool.contact.interfaces import IContactable, IContact
27
29
from schooltool.email.interfaces import IEmailUtility
28
30
from schooltool.email.mail import Email
 
31
from schooltool.person.interfaces import IPerson
29
32
 
30
33
from schooltool.intervention import InterventionGettext as _
31
 
from interfaces import IInterventionGoal
 
34
from interfaces import IInterventionGoal, IInterventionSchoolYear
32
35
import intervention
33
36
 
34
37
 
61
64
def sendInterventionMessageEmail(obj, event):
62
65
    """Send email corresponding to the intervention message just added."""
63
66
 
64
 
    sender = intervention.convertIdToEmail(obj.sender)
65
 
    sender_name = intervention.convertIdToName(obj.sender)
66
 
    recipients = intervention.convertIdsToEmail(obj.recipients)
67
 
    name = intervention.convertIdToName(obj.student.username)
 
67
    # there can be only one sender - if there are none, abort
 
68
    for contact in obj.sender:
 
69
        sender = contact.email
 
70
        sender_name = intervention.contactName(contact)
 
71
        break
 
72
    else:
 
73
        return
 
74
    recipients = intervention.contactsEmail(obj.recipients)
 
75
    name = intervention.contactName(IContact(IPerson(obj)))
68
76
    mapping = {'student': name}
69
77
    if obj.status_change:
70
78
        subject = translate(_('INTERVENTION STATUS CHANGE: ${student}',
83
91
def sendInterventionGoalAddEmail(obj, event):
84
92
    """Send email corresponding to the intervention goal just added."""
85
93
 
86
 
    if obj.creator:
87
 
        sender = intervention.convertIdToEmail(obj.creator)
 
94
    # there can be only one sender - if there are none, abort
 
95
    for contact in obj.creator:
 
96
        sender = contact.email
 
97
        break
88
98
    else:
89
 
        sender = intervention.convertIdToEmail('manager')
 
99
        return
90
100
 
91
 
    name = intervention.convertIdToName(obj.student.username)
 
101
    student = IPerson(obj)
 
102
    student_contacts = IContactable(student).contacts
 
103
    name = intervention.contactName(IContact(student))
92
104
    subject = translate(_('INTERVENTION GOAL ADDED: ${student}',
93
105
                          mapping={'student': name}),
94
106
                        context=intervention.getRequest())
95
107
 
96
 
    responsible = [id for id in obj.persons_responsible
97
 
                   if not id.split(':')[0] == obj.student.username]
98
 
    notified = [id for id in obj.persons_responsible
99
 
                if id.split(':')[0] == obj.student.username]
 
108
    responsible, notified = [], []
 
109
    for contact in obj.persons_responsible:
 
110
        if IPerson(contact, None) is student or contact in student_contacts:
 
111
            notified.append(contact)
 
112
        else:
 
113
            responsible.append(contact)
100
114
 
101
115
    if responsible:
102
 
        recipients = intervention.convertIdsToEmail(responsible)
 
116
        recipients = intervention.contactsEmail(responsible)
103
117
        body = buildInterventionGoalAddBody(obj)
104
118
        EmailMessage(sender, recipients, subject, body).send()
105
119
 
106
120
    if notified:
107
 
        recipients = intervention.convertIdsToEmail(notified)
 
121
        recipients = intervention.contactsEmail(notified)
108
122
        body = buildInterventionGoalAddBody(obj, False)
109
123
        EmailMessage(sender, recipients, subject, body).send()
110
124
 
111
125
 
112
126
def buildInterventionGoalAddBody(obj, staff=True):
113
 
    name = intervention.convertIdToName(obj.student.username)
 
127
    name = intervention.contactName(IContact(IPerson(obj)))
114
128
    body = translate(_('The following goal was added for ${student}:',
115
129
                       mapping={'student': name}),
116
130
                     context=intervention.getRequest())
123
137
        body += '\n%s\n%s\n\n' % (v.field.title, '-' * len(v.field.title))
124
138
        text = obj.__getattribute__(k)
125
139
        if k == 'persons_responsible':
126
 
            text = '\n'.join(intervention.convertIdsToNames(obj.persons_responsible))
 
140
            names = [intervention.contactName(contact)
 
141
                     for contact in obj.persons_responsible]
 
142
            text = '\n'.join(sorted(names))
127
143
        elif k == 'timeline':
128
144
            text = obj.timeline.strftime('%x')
129
145
        body += '%s\n' % text
147
163
    request = intervention.getRequest()
148
164
    now = date.today()
149
165
    goalsNotified = []
150
 
    interventionSchoolYear = intervention.getInterventionSchoolYear()
 
166
    app = ISchoolToolApplication(None)
 
167
    interventionSchoolYear = IInterventionSchoolYear(app)
151
168
    for interventionStudent in interventionSchoolYear.values():
152
169
        for interventionGoal in interventionStudent['goals'].values():
153
170
            if interventionGoal.timeline <= now and not interventionGoal.notified:
159
176
def notifyInterventionGoal(interventionGoal, request):
160
177
    """Send email to the persons responsible for the given goal."""
161
178
 
162
 
    sender = intervention.convertIdToEmail('manager')
163
 
    student = interventionGoal.student
164
 
    persons = [id for id in interventionGoal.persons_responsible
165
 
               if not id.split(':')[0] == student.username]
166
 
    recipients = intervention.convertIdsToEmail(persons)
167
 
    name = intervention.convertIdToName(student.username)
 
179
    # there can be only one sender - if there are none, abort
 
180
    for contact in interventionGoal.creator:
 
181
        sender = contact.email
 
182
        break
 
183
    else:
 
184
        return
 
185
 
 
186
    student = IPerson(interventionGoal)
 
187
    student_contacts = IContactable(student).contacts
 
188
    responsible = []
 
189
    for contact in interventionGoal.persons_responsible:
 
190
        if IPerson(contact, None) is student or contact in student_contacts:
 
191
            continue
 
192
        responsible.append(contact)
 
193
    recipients = intervention.contactsEmail(responsible)
 
194
 
 
195
    name = intervention.contactName(IContact(student))
168
196
    subject = translate(_('INTERVENTION GOAL DUE: ${student}',
169
197
                          mapping={'student': name}),
170
198
                        context=request)
171
 
    name = intervention.convertIdToName(student.username)
172
199
    url = absoluteURL(interventionGoal, request) + '/@@editGoal.html'
173
200
    body = translate(_("Please follow the link below to update the follow "
174
201
                       "up notes and, if appropriate, the goal met status "