root / snf-astakos-app / astakos / im / notifications.py @ e7726e14
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 | dd6a7b9b | Sofia Papagiannaki | from django.core.mail import send_mail, get_connection |
40 | 71a38edf | Sofia Papagiannaki | from django.utils.translation import ugettext as _ |
41 | 734107ef | Kostas Papadimitriou | from synnefo_branding.utils import render_to_string |
42 | c0b26605 | Sofia Papagiannaki | |
43 | c0b26605 | Sofia Papagiannaki | import astakos.im.messages as astakos_messages |
44 | c0b26605 | Sofia Papagiannaki | |
45 | c0b26605 | Sofia Papagiannaki | logger = logging.getLogger(__name__) |
46 | c0b26605 | Sofia Papagiannaki | |
47 | c4d1b547 | Sofia Papagiannaki | def build_notification( |
48 | 73fbaec4 | Sofia Papagiannaki | sender, recipients, subject, message=None, template=None, dictionary=None): |
49 | c4d1b547 | Sofia Papagiannaki | return EmailNotification(
|
50 | 73fbaec4 | Sofia Papagiannaki | sender, recipients, subject, message, template, dictionary) |
51 | 29b87e7c | Sofia Papagiannaki | |
52 | 29b87e7c | Sofia Papagiannaki | class Notification(object): |
53 | c4d1b547 | Sofia Papagiannaki | def __init__( |
54 | c4d1b547 | Sofia Papagiannaki | self, sender, recipients, subject,
|
55 | 73fbaec4 | Sofia Papagiannaki | message=None, template=None, dictionary=None): |
56 | c4d1b547 | Sofia Papagiannaki | if not message and not template: |
57 | e30537f1 | Sofia Papagiannaki | raise IOError('message and template cannot be both None.') |
58 | c4d1b547 | Sofia Papagiannaki | dictionary = dictionary or {}
|
59 | 29b87e7c | Sofia Papagiannaki | self.sender = sender
|
60 | 29b87e7c | Sofia Papagiannaki | self.recipients = recipients
|
61 | 29b87e7c | Sofia Papagiannaki | self.subject = subject
|
62 | c4d1b547 | Sofia Papagiannaki | self.message = message or render_to_string(template, dictionary) |
63 | e7cb4085 | Kostas Papadimitriou | |
64 | 29b87e7c | Sofia Papagiannaki | def send(self): |
65 | c0b26605 | Sofia Papagiannaki | pass
|
66 | c0b26605 | Sofia Papagiannaki | |
67 | e1a80257 | Sofia Papagiannaki | class EmailNotification(Notification): |
68 | e1a80257 | Sofia Papagiannaki | def send(self): |
69 | e1a80257 | Sofia Papagiannaki | try:
|
70 | e1a80257 | Sofia Papagiannaki | send_mail( |
71 | 71a38edf | Sofia Papagiannaki | self.subject,
|
72 | 71a38edf | Sofia Papagiannaki | self.message,
|
73 | 71a38edf | Sofia Papagiannaki | self.sender,
|
74 | dd6a7b9b | Sofia Papagiannaki | self.recipients,
|
75 | dd6a7b9b | Sofia Papagiannaki | connection=get_connection()) |
76 | ccab6eb5 | Sofia Papagiannaki | except (SMTPException, socket.error), e:
|
77 | 71a38edf | Sofia Papagiannaki | logger.exception(e) |
78 | 3d4a0a9b | Sofia Papagiannaki | raise NotificationError(self) |
79 | c0b26605 | Sofia Papagiannaki | |
80 | ccab6eb5 | Sofia Papagiannaki | class NotificationError(Exception): |
81 | 3d4a0a9b | Sofia Papagiannaki | def __init__(self, nofication): |
82 | 3d4a0a9b | Sofia Papagiannaki | self.message = _(astakos_messages.DETAILED_NOTIFICATION_SEND_ERR) % nofication.__dict__
|
83 | 172ce682 | Sofia Papagiannaki | super(NotificationError, self).__init__() |