root / snf-astakos-app / astakos / im / notifications.py @ e47fb17a
History | View | Annotate | Download (3.1 kB)
1 | c0b26605 | Sofia Papagiannaki | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | c0b26605 | Sofia Papagiannaki | #
|
3 | c0b26605 | Sofia Papagiannaki | # Redistribution and use in source and binary forms, with or
|
4 | c0b26605 | Sofia Papagiannaki | # without modification, are permitted provided that the following
|
5 | c0b26605 | Sofia Papagiannaki | # conditions are met:
|
6 | c0b26605 | Sofia Papagiannaki | #
|
7 | c0b26605 | Sofia Papagiannaki | # 1. Redistributions of source code must retain the above
|
8 | c0b26605 | Sofia Papagiannaki | # copyright notice, this list of conditions and the following
|
9 | c0b26605 | Sofia Papagiannaki | # disclaimer.
|
10 | c0b26605 | Sofia Papagiannaki | #
|
11 | c0b26605 | Sofia Papagiannaki | # 2. Redistributions in binary form must reproduce the above
|
12 | c0b26605 | Sofia Papagiannaki | # copyright notice, this list of conditions and the following
|
13 | c0b26605 | Sofia Papagiannaki | # disclaimer in the documentation and/or other materials
|
14 | c0b26605 | Sofia Papagiannaki | # provided with the distribution.
|
15 | c0b26605 | Sofia Papagiannaki | #
|
16 | c0b26605 | Sofia Papagiannaki | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | c0b26605 | Sofia Papagiannaki | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | c0b26605 | Sofia Papagiannaki | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | c0b26605 | Sofia Papagiannaki | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | c0b26605 | Sofia Papagiannaki | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | c0b26605 | Sofia Papagiannaki | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | c0b26605 | Sofia Papagiannaki | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | c0b26605 | Sofia Papagiannaki | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | c0b26605 | Sofia Papagiannaki | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | c0b26605 | Sofia Papagiannaki | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | c0b26605 | Sofia Papagiannaki | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | c0b26605 | Sofia Papagiannaki | # POSSIBILITY OF SUCH DAMAGE.
|
28 | c0b26605 | Sofia Papagiannaki | #
|
29 | c0b26605 | Sofia Papagiannaki | # The views and conclusions contained in the software and
|
30 | c0b26605 | Sofia Papagiannaki | # documentation are those of the authors and should not be
|
31 | c0b26605 | Sofia Papagiannaki | # interpreted as representing official policies, either expressed
|
32 | c0b26605 | Sofia Papagiannaki | # or implied, of GRNET S.A.
|
33 | c0b26605 | Sofia Papagiannaki | |
34 | c0b26605 | Sofia Papagiannaki | import logging |
35 | c0b26605 | Sofia Papagiannaki | import socket |
36 | c0b26605 | Sofia Papagiannaki | |
37 | ccab6eb5 | Sofia Papagiannaki | from smtplib import SMTPException |
38 | ccab6eb5 | Sofia Papagiannaki | |
39 | c0b26605 | Sofia Papagiannaki | from django.conf import settings |
40 | dd6a7b9b | Sofia Papagiannaki | from django.core.mail import send_mail, get_connection |
41 | 71a38edf | Sofia Papagiannaki | from django.utils.translation import ugettext as _ |
42 | c4d1b547 | Sofia Papagiannaki | from django.template.loader import render_to_string |
43 | c0b26605 | Sofia Papagiannaki | |
44 | c0b26605 | Sofia Papagiannaki | import astakos.im.messages as astakos_messages |
45 | c0b26605 | Sofia Papagiannaki | |
46 | c0b26605 | Sofia Papagiannaki | logger = logging.getLogger(__name__) |
47 | c0b26605 | Sofia Papagiannaki | |
48 | c4d1b547 | Sofia Papagiannaki | def build_notification( |
49 | 73fbaec4 | Sofia Papagiannaki | sender, recipients, subject, message=None, template=None, dictionary=None): |
50 | c4d1b547 | Sofia Papagiannaki | return EmailNotification(
|
51 | 73fbaec4 | Sofia Papagiannaki | sender, recipients, subject, message, template, dictionary) |
52 | 29b87e7c | Sofia Papagiannaki | |
53 | 29b87e7c | Sofia Papagiannaki | class Notification(object): |
54 | c4d1b547 | Sofia Papagiannaki | def __init__( |
55 | c4d1b547 | Sofia Papagiannaki | self, sender, recipients, subject,
|
56 | 73fbaec4 | Sofia Papagiannaki | message=None, template=None, dictionary=None): |
57 | c4d1b547 | Sofia Papagiannaki | if not message and not template: |
58 | c4d1b547 | Sofia Papagiannaki | raise InputError('message and template cannot be both None.') |
59 | c4d1b547 | Sofia Papagiannaki | dictionary = dictionary or {}
|
60 | 29b87e7c | Sofia Papagiannaki | self.sender = sender
|
61 | 29b87e7c | Sofia Papagiannaki | self.recipients = recipients
|
62 | 29b87e7c | Sofia Papagiannaki | self.subject = subject
|
63 | c4d1b547 | Sofia Papagiannaki | self.message = message or render_to_string(template, dictionary) |
64 | 29b87e7c | Sofia Papagiannaki | |
65 | 29b87e7c | Sofia Papagiannaki | def send(self): |
66 | c0b26605 | Sofia Papagiannaki | pass
|
67 | c0b26605 | Sofia Papagiannaki | |
68 | e1a80257 | Sofia Papagiannaki | class EmailNotification(Notification): |
69 | e1a80257 | Sofia Papagiannaki | def send(self): |
70 | e1a80257 | Sofia Papagiannaki | try:
|
71 | e1a80257 | Sofia Papagiannaki | send_mail( |
72 | 71a38edf | Sofia Papagiannaki | self.subject,
|
73 | 71a38edf | Sofia Papagiannaki | self.message,
|
74 | 71a38edf | Sofia Papagiannaki | self.sender,
|
75 | dd6a7b9b | Sofia Papagiannaki | self.recipients,
|
76 | dd6a7b9b | Sofia Papagiannaki | connection=get_connection()) |
77 | ccab6eb5 | Sofia Papagiannaki | except (SMTPException, socket.error), e:
|
78 | 71a38edf | Sofia Papagiannaki | logger.exception(e) |
79 | 3d4a0a9b | Sofia Papagiannaki | raise NotificationError(self) |
80 | c0b26605 | Sofia Papagiannaki | |
81 | ccab6eb5 | Sofia Papagiannaki | class NotificationError(Exception): |
82 | 3d4a0a9b | Sofia Papagiannaki | def __init__(self, nofication): |
83 | 3d4a0a9b | Sofia Papagiannaki | self.message = _(astakos_messages.DETAILED_NOTIFICATION_SEND_ERR) % nofication.__dict__
|
84 | 172ce682 | Sofia Papagiannaki | super(NotificationError, self).__init__() |