Commit 62db705f authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

astakos: Refactor notification code

Introduce generic notify functions that consult a dict for a given action.
parent 2482f9c2
......@@ -574,8 +574,8 @@ def leave_project(memb_id, request_user, reason=None):
reason=reason)
logger.info("User %s requested to leave %s." %
(request_user.log_display, project))
project_notif.membership_leave_request_notify(
project, membership.person)
project_notif.membership_request_notify(
project, membership.person, "leave")
return auto_accepted
......@@ -635,7 +635,8 @@ def join_project(project_id, request_user, reason=None):
logger.info("User %s joined %s." %
(request_user.log_display, project))
else:
project_notif.membership_request_notify(project, membership.person)
project_notif.membership_request_notify(
project, membership.person, "join")
logger.info("User %s requested to join %s." %
(request_user.log_display, project))
return membership
......@@ -724,7 +725,7 @@ def submit_application(owner=None,
set_resource_policies(application, policies)
logger.info("User %s submitted %s." %
(request_user.log_display, application.log_display))
project_notif.application_submit_notify(application)
project_notif.application_notify(application, "submit")
return application
......@@ -811,7 +812,7 @@ def deny_application(application_id, request_user=None, reason=""):
application.deny(actor=request_user, reason=reason)
logger.info("%s has been denied with reason \"%s\"." %
(application.log_display, reason))
project_notif.application_deny_notify(application)
project_notif.application_notify(application, "deny")
def check_conflicting_projects(application):
......@@ -860,7 +861,7 @@ def approve_application(app_id, request_user=None, reason=""):
project.resume(actor=request_user, reason="APPROVE")
quotas.qh_sync_locked_users(members)
logger.info("%s has been approved." % (application.log_display))
project_notif.application_approve_notify(application)
project_notif.application_notify(application, "approve")
def check_expiration(execute=False):
......@@ -882,7 +883,7 @@ def terminate(project_id, request_user=None, reason=None):
quotas.qh_sync_project(project)
logger.info("%s has been terminated." % (project))
project_notif.project_termination_notify(project)
project_notif.project_notify(project, "terminate")
def suspend(project_id, request_user=None, reason=None):
......@@ -894,7 +895,7 @@ def suspend(project_id, request_user=None, reason=None):
quotas.qh_sync_project(project)
logger.info("%s has been suspended." % (project))
project_notif.project_suspension_notify(project)
project_notif.project_notify(project, "suspend")
def unsuspend(project_id, request_user=None, reason=None):
......@@ -908,7 +909,7 @@ def unsuspend(project_id, request_user=None, reason=None):
project.resume(actor=request_user, reason=reason)
quotas.qh_sync_project(project)
logger.info("%s has been unsuspended." % (project))
project_notif.project_unsuspension_notify(project)
project_notif.project_notify(project, "unsuspend")
def reinstate(project_id, request_user=None, reason=None):
......@@ -924,7 +925,7 @@ def reinstate(project_id, request_user=None, reason=None):
project.resume(actor=request_user, reason=reason)
quotas.qh_sync_project(project)
logger.info("%s has been reinstated" % (project))
project_notif.project_reinstatement_notify(project)
project_notif.project_notify(project, "reinstate")
def _partition_by(f, l):
......
......@@ -79,119 +79,78 @@ def membership_enroll_notify(project, user):
logger.error(e.message)
def membership_request_notify(project, requested_user):
try:
notification = build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_MEMBERSHIP_REQUEST_SUBJECT) % project.__dict__,
template='im/projects/project_membership_request_notification.txt',
dictionary={'object': project, 'user': requested_user.email})
notification.send()
except NotificationError, e:
logger.error(e.message)
MEMBERSHIP_REQUEST_DATA = {
"join": lambda p: (
_(messages.PROJECT_MEMBERSHIP_REQUEST_SUBJECT) % p.__dict__,
"im/projects/project_membership_request_notification.txt"),
"leave": lambda p: (
_(messages.PROJECT_MEMBERSHIP_LEAVE_REQUEST_SUBJECT) % p.__dict__,
"im/projects/project_membership_leave_request_notification.txt"),
}
def membership_leave_request_notify(project, requested_user):
template = 'im/projects/project_membership_leave_request_notification.txt'
def membership_request_notify(project, requested_user, action):
subject, template = MEMBERSHIP_REQUEST_DATA[action](project)
try:
notification = build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_MEMBERSHIP_LEAVE_REQUEST_SUBJECT) %
project.__dict__,
build_notification(
SENDER, [project.application.owner.email], subject,
template=template,
dictionary={'object': project, 'user': requested_user.email})
notification.send()
except NotificationError, e:
logger.error(e.message)
def application_submit_notify(application):
try:
notification = build_notification(
SENDER, NOTIFY_RECIPIENTS,
_(messages.PROJECT_CREATION_SUBJECT) % application.__dict__,
template='im/projects/project_creation_notification.txt',
dictionary={'object': application})
notification.send()
except NotificationError, e:
logger.error(e.message)
def application_deny_notify(application):
try:
notification = build_notification(
SENDER,
[application.owner.email],
_(messages.PROJECT_DENIED_SUBJECT) % application.__dict__,
template='im/projects/project_denial_notification.txt',
dictionary={'object': application})
notification.send()
except NotificationError, e:
logger.error(e.message)
def application_approve_notify(application):
try:
notification = build_notification(
SENDER,
[application.owner.email],
_(messages.PROJECT_APPROVED_SUBJECT) % application.__dict__,
template='im/projects/project_approval_notification.txt',
dictionary={'object': application})
notification.send()
dictionary={'object': project, 'user': requested_user.email}
).send()
except NotificationError, e:
logger.error(e.message)
def project_termination_notify(project):
app = project.application
try:
build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_TERMINATION_SUBJECT) % app.__dict__,
template='im/projects/project_termination_notification.txt',
dictionary={'object': project}
).send()
except NotificationError, e:
logger.error(e.message)
APPLICATION_DATA = {
"submit": lambda a: (
NOTIFY_RECIPIENTS,
_(messages.PROJECT_CREATION_SUBJECT) % a.__dict__,
"im/projects/project_creation_notification.txt"),
"deny": lambda a: (
[a.owner.email],
_(messages.PROJECT_DENIED_SUBJECT) % a.__dict__,
"im/projects/project_denial_notification.txt"),
"approve": lambda a: (
[a.owner.email],
_(messages.PROJECT_APPROVED_SUBJECT) % a.__dict__,
"im/projects/project_approval_notification.txt"),
}
def project_suspension_notify(project):
def application_notify(application, action):
recipients, subject, template = APPLICATION_DATA[action](application)
try:
build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_SUSPENSION_SUBJECT) % project.__dict__,
template='im/projects/project_suspension_notification.txt',
dictionary={'object': project}
SENDER, recipients, subject,
template=template,
dictionary={'object': application}
).send()
except NotificationError, e:
logger.error(e.message)
def project_unsuspension_notify(project):
try:
build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_UNSUSPENSION_SUBJECT) % project.__dict__,
template='im/projects/project_unsuspension_notification.txt',
dictionary={'object': project}
).send()
except NotificationError, e:
logger.error(e.message)
PROJECT_DATA = {
"terminate": lambda p: (
_(messages.PROJECT_TERMINATION_SUBJECT) % p.application.__dict__,
"im/projects/project_termination_notification.txt"),
"reinstate": lambda p: (
_(messages.PROJECT_REINSTATEMENT_SUBJECT) % p.__dict__,
"im/projects/project_reinstatement_notification.txt"),
"suspend": lambda p: (
_(messages.PROJECT_SUSPENSION_SUBJECT) % p.__dict__,
"im/projects/project_suspension_notification.txt"),
"unsuspend": lambda p: (
_(messages.PROJECT_UNSUSPENSION_SUBJECT) % p.__dict__,
"im/projects/project_unsuspension_notification.txt"),
}
def project_reinstatement_notify(project):
def project_notify(project, action):
subject, template = PROJECT_DATA[action](project)
try:
build_notification(
SENDER,
[project.application.owner.email],
_(messages.PROJECT_REINSTATEMENT_SUBJECT) % project.__dict__,
template='im/projects/project_reinstatement_notification.txt',
SENDER, [project.application.owner.email], subject,
template=template,
dictionary={'object': project}
).send()
except NotificationError, e:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment