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): ...@@ -574,8 +574,8 @@ def leave_project(memb_id, request_user, reason=None):
reason=reason) reason=reason)
logger.info("User %s requested to leave %s." % logger.info("User %s requested to leave %s." %
(request_user.log_display, project)) (request_user.log_display, project))
project_notif.membership_leave_request_notify( project_notif.membership_request_notify(
project, membership.person) project, membership.person, "leave")
return auto_accepted return auto_accepted
...@@ -635,7 +635,8 @@ def join_project(project_id, request_user, reason=None): ...@@ -635,7 +635,8 @@ def join_project(project_id, request_user, reason=None):
logger.info("User %s joined %s." % logger.info("User %s joined %s." %
(request_user.log_display, project)) (request_user.log_display, project))
else: 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." % logger.info("User %s requested to join %s." %
(request_user.log_display, project)) (request_user.log_display, project))
return membership return membership
...@@ -724,7 +725,7 @@ def submit_application(owner=None, ...@@ -724,7 +725,7 @@ def submit_application(owner=None,
set_resource_policies(application, policies) set_resource_policies(application, policies)
logger.info("User %s submitted %s." % logger.info("User %s submitted %s." %
(request_user.log_display, application.log_display)) (request_user.log_display, application.log_display))
project_notif.application_submit_notify(application) project_notif.application_notify(application, "submit")
return application return application
...@@ -811,7 +812,7 @@ def deny_application(application_id, request_user=None, reason=""): ...@@ -811,7 +812,7 @@ def deny_application(application_id, request_user=None, reason=""):
application.deny(actor=request_user, reason=reason) application.deny(actor=request_user, reason=reason)
logger.info("%s has been denied with reason \"%s\"." % logger.info("%s has been denied with reason \"%s\"." %
(application.log_display, reason)) (application.log_display, reason))
project_notif.application_deny_notify(application) project_notif.application_notify(application, "deny")
def check_conflicting_projects(application): def check_conflicting_projects(application):
...@@ -860,7 +861,7 @@ def approve_application(app_id, request_user=None, reason=""): ...@@ -860,7 +861,7 @@ def approve_application(app_id, request_user=None, reason=""):
project.resume(actor=request_user, reason="APPROVE") project.resume(actor=request_user, reason="APPROVE")
quotas.qh_sync_locked_users(members) quotas.qh_sync_locked_users(members)
logger.info("%s has been approved." % (application.log_display)) 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): def check_expiration(execute=False):
...@@ -882,7 +883,7 @@ def terminate(project_id, request_user=None, reason=None): ...@@ -882,7 +883,7 @@ def terminate(project_id, request_user=None, reason=None):
quotas.qh_sync_project(project) quotas.qh_sync_project(project)
logger.info("%s has been terminated." % (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): def suspend(project_id, request_user=None, reason=None):
...@@ -894,7 +895,7 @@ 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) quotas.qh_sync_project(project)
logger.info("%s has been suspended." % (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): def unsuspend(project_id, request_user=None, reason=None):
...@@ -908,7 +909,7 @@ 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) project.resume(actor=request_user, reason=reason)
quotas.qh_sync_project(project) quotas.qh_sync_project(project)
logger.info("%s has been unsuspended." % (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): def reinstate(project_id, request_user=None, reason=None):
...@@ -924,7 +925,7 @@ 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) project.resume(actor=request_user, reason=reason)
quotas.qh_sync_project(project) quotas.qh_sync_project(project)
logger.info("%s has been reinstated" % (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): def _partition_by(f, l):
......
...@@ -79,119 +79,78 @@ def membership_enroll_notify(project, user): ...@@ -79,119 +79,78 @@ def membership_enroll_notify(project, user):
logger.error(e.message) logger.error(e.message)
def membership_request_notify(project, requested_user): MEMBERSHIP_REQUEST_DATA = {
try: "join": lambda p: (
notification = build_notification( _(messages.PROJECT_MEMBERSHIP_REQUEST_SUBJECT) % p.__dict__,
SENDER, "im/projects/project_membership_request_notification.txt"),
[project.application.owner.email], "leave": lambda p: (
_(messages.PROJECT_MEMBERSHIP_REQUEST_SUBJECT) % project.__dict__, _(messages.PROJECT_MEMBERSHIP_LEAVE_REQUEST_SUBJECT) % p.__dict__,
template='im/projects/project_membership_request_notification.txt', "im/projects/project_membership_leave_request_notification.txt"),
dictionary={'object': project, 'user': requested_user.email}) }
notification.send()
except NotificationError, e:
logger.error(e.message)
def membership_leave_request_notify(project, requested_user): def membership_request_notify(project, requested_user, action):
template = 'im/projects/project_membership_leave_request_notification.txt' subject, template = MEMBERSHIP_REQUEST_DATA[action](project)
try: try:
notification = build_notification( build_notification(
SENDER, SENDER, [project.application.owner.email], subject,
[project.application.owner.email],
_(messages.PROJECT_MEMBERSHIP_LEAVE_REQUEST_SUBJECT) %
project.__dict__,
template=template, template=template,
dictionary={'object': project, 'user': requested_user.email}) dictionary={'object': project, 'user': requested_user.email}
notification.send() ).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()
except NotificationError, e: except NotificationError, e:
logger.error(e.message) logger.error(e.message)
def project_termination_notify(project): APPLICATION_DATA = {
app = project.application "submit": lambda a: (
try: NOTIFY_RECIPIENTS,
build_notification( _(messages.PROJECT_CREATION_SUBJECT) % a.__dict__,
SENDER, "im/projects/project_creation_notification.txt"),
[project.application.owner.email], "deny": lambda a: (
_(messages.PROJECT_TERMINATION_SUBJECT) % app.__dict__, [a.owner.email],
template='im/projects/project_termination_notification.txt', _(messages.PROJECT_DENIED_SUBJECT) % a.__dict__,
dictionary={'object': project} "im/projects/project_denial_notification.txt"),
).send() "approve": lambda a: (
except NotificationError, e: [a.owner.email],
logger.error(e.message) _(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: try:
build_notification( build_notification(
SENDER, SENDER, recipients, subject,
[project.application.owner.email], template=template,
_(messages.PROJECT_SUSPENSION_SUBJECT) % project.__dict__, dictionary={'object': application}
template='im/projects/project_suspension_notification.txt',
dictionary={'object': project}
).send() ).send()
except NotificationError, e: except NotificationError, e:
logger.error(e.message) logger.error(e.message)
def project_unsuspension_notify(project): PROJECT_DATA = {
try: "terminate": lambda p: (
build_notification( _(messages.PROJECT_TERMINATION_SUBJECT) % p.application.__dict__,
SENDER, "im/projects/project_termination_notification.txt"),
[project.application.owner.email], "reinstate": lambda p: (
_(messages.PROJECT_UNSUSPENSION_SUBJECT) % project.__dict__, _(messages.PROJECT_REINSTATEMENT_SUBJECT) % p.__dict__,
template='im/projects/project_unsuspension_notification.txt', "im/projects/project_reinstatement_notification.txt"),
dictionary={'object': project} "suspend": lambda p: (
).send() _(messages.PROJECT_SUSPENSION_SUBJECT) % p.__dict__,
except NotificationError, e: "im/projects/project_suspension_notification.txt"),
logger.error(e.message) "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: try:
build_notification( build_notification(
SENDER, SENDER, [project.application.owner.email], subject,
[project.application.owner.email], template=template,
_(messages.PROJECT_REINSTATEMENT_SUBJECT) % project.__dict__,
template='im/projects/project_reinstatement_notification.txt',
dictionary={'object': project} dictionary={'object': project}
).send() ).send()
except NotificationError, e: 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