Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / project_notif.py @ 84498bca

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
    subject, template = MEMBERSHIP_REQUEST_DATA[action](project)
94
    try:
95
        build_notification(
96
            SENDER, [project.application.owner.email], subject,
97
            template=template,
98
            dictionary={'object': project, 'user': requested_user.email}
99
        ).send()
100
    except NotificationError, e:
101
        logger.error(e.message)
102

    
103

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

    
119

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

    
131

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

    
147

    
148
def project_notify(project, action):
149
    subject, template = PROJECT_DATA[action](project)
150
    try:
151
        build_notification(
152
            SENDER, [project.application.owner.email], subject,
153
            template=template,
154
            dictionary={'object': project}
155
        ).send()
156
    except NotificationError, e:
157
        logger.error(e.message)