Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / project_notif.py @ 9202a57d

History | View | Annotate | Download (5.6 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import logging
35
from django.utils.translation import ugettext as _
36
from astakos.im import settings
37
from astakos.im.notifications import build_notification, NotificationError
38
from astakos.im import messages
39

    
40
logger = logging.getLogger(__name__)
41

    
42
MEM_CHANGE_NOTIF = {
43
    'subject':   _(messages.PROJECT_MEMBERSHIP_CHANGE_SUBJECT),
44
    'template': 'im/projects/project_membership_change_notification.txt',
45
}
46

    
47
MEM_ENROLL_NOTIF = {
48
    'subject':   _(messages.PROJECT_MEMBERSHIP_ENROLL_SUBJECT),
49
    'template': 'im/projects/project_membership_enroll_notification.txt',
50
}
51

    
52
SENDER = settings.SERVER_EMAIL
53
NOTIFY_RECIPIENTS = [e[1] for e in settings.MANAGERS + settings.HELPDESK]
54

    
55

    
56
def membership_change_notify(project, user, action):
57
    try:
58
        notification = build_notification(
59
            SENDER,
60
            [user.email],
61
            MEM_CHANGE_NOTIF['subject'] % project.__dict__,
62
            template=MEM_CHANGE_NOTIF['template'],
63
            dictionary={'object': project, 'action': action})
64
        notification.send()
65
    except NotificationError, e:
66
        logger.error(e.message)
67

    
68

    
69
def membership_enroll_notify(project, user):
70
    try:
71
        notification = build_notification(
72
            SENDER,
73
            [user.email],
74
            MEM_ENROLL_NOTIF['subject'] % project.__dict__,
75
            template=MEM_ENROLL_NOTIF['template'],
76
            dictionary={'object': project})
77
        notification.send()
78
    except NotificationError, e:
79
        logger.error(e.message)
80

    
81

    
82
MEMBERSHIP_REQUEST_DATA = {
83
    "join": lambda p: (
84
        _(messages.PROJECT_MEMBERSHIP_REQUEST_SUBJECT) % p.__dict__,
85
        "im/projects/project_membership_request_notification.txt"),
86
    "leave": lambda p: (
87
        _(messages.PROJECT_MEMBERSHIP_LEAVE_REQUEST_SUBJECT) % p.__dict__,
88
        "im/projects/project_membership_leave_request_notification.txt"),
89
}
90

    
91

    
92
def membership_request_notify(project, requested_user, action):
93
    owner = project.owner
94
    if owner is None:
95
        return
96
    subject, template = MEMBERSHIP_REQUEST_DATA[action](project)
97
    try:
98
        build_notification(
99
            SENDER, [owner.email], subject,
100
            template=template,
101
            dictionary={'object': project, 'user': requested_user.email}
102
        ).send()
103
    except NotificationError, e:
104
        logger.error(e.message)
105

    
106

    
107
APPLICATION_DATA = {
108
    "submit": lambda a: (
109
        NOTIFY_RECIPIENTS,
110
        _(messages.PROJECT_CREATION_SUBJECT) % a.__dict__,
111
        "im/projects/project_creation_notification.txt"),
112
    "deny": lambda a: (
113
        [a.applicant.email],
114
        _(messages.PROJECT_DENIED_SUBJECT) % a.__dict__,
115
        "im/projects/project_denial_notification.txt"),
116
    "approve": lambda a: (
117
        [a.applicant.email],
118
        _(messages.PROJECT_APPROVED_SUBJECT) % a.__dict__,
119
        "im/projects/project_approval_notification.txt"),
120
}
121

    
122

    
123
def application_notify(application, action):
124
    recipients, subject, template = APPLICATION_DATA[action](application)
125
    try:
126
        build_notification(
127
            SENDER, recipients, subject,
128
            template=template,
129
            dictionary={'object': application}
130
        ).send()
131
    except NotificationError, e:
132
        logger.error(e.message)
133

    
134

    
135
PROJECT_DATA = {
136
    "terminate": lambda p: (
137
        _(messages.PROJECT_TERMINATION_SUBJECT) % p.__dict__,
138
        "im/projects/project_termination_notification.txt"),
139
    "reinstate": lambda p: (
140
        _(messages.PROJECT_REINSTATEMENT_SUBJECT) % p.__dict__,
141
        "im/projects/project_reinstatement_notification.txt"),
142
    "suspend": lambda p: (
143
        _(messages.PROJECT_SUSPENSION_SUBJECT) % p.__dict__,
144
        "im/projects/project_suspension_notification.txt"),
145
    "unsuspend": lambda p: (
146
        _(messages.PROJECT_UNSUSPENSION_SUBJECT) % p.__dict__,
147
        "im/projects/project_unsuspension_notification.txt"),
148
}
149

    
150

    
151
def project_notify(project, action):
152
    owner = project.owner
153
    if owner is None:
154
        return
155
    subject, template = PROJECT_DATA[action](project)
156
    try:
157
        build_notification(
158
            SENDER, [owner.email], subject,
159
            template=template,
160
            dictionary={'object': project}
161
        ).send()
162
    except NotificationError, e:
163
        logger.error(e.message)