Newer
Older
import requests
from django.conf import settings
from django.contrib import messages
from django.template.loader import render_to_string
from .models import Activity
def link_to(target, request):
return request.scheme + '://' + request.get_host() + target.get_absolute_url()
class MESSAGES(Enum):
UPDATE_LEAD = 'update_lead'
DETERMINATION_OUTCOME = 'determination_outcome'
INVITED_TO_PROPOSAL = 'invited_to_proposal'
REVIEWERS_UPDATED = 'reviewers_updated'
PROPOSAL_SUBMITTED = 'proposal_submitted'
def message(self, message_type, **kwargs):
try:
message = self.messages[message_type]
except KeyError:
# We don't know how to handle that message type
return
try:
# see if its a method on the adapter
method = getattr(self, message)
except AttributeError:
return self.render_message(message, **kwargs)
# Delegate all responsibility to the custom method
def render_message(self, message, **kwargs):
return message.format(**kwargs)
def process(self, message_type, **kwargs):
message = self.message(message_type, **kwargs)
kwargs.update(self.extra_kwargs(message_type, **kwargs))
if not message:
if settings.SEND_MESSAGES or self.always_send:
self.send_message(message, **kwargs)
if not settings.SEND_MESSAGES:
messages.add_message(kwargs['request'], messages.INFO, message)
def send_message(self, message, **kwargs):
raise NotImplementedError()
adapter_type = "Activity Feed"
MESSAGES.TRANSITION: 'Progressed from {old_phase.display_name} to {submission.phase}',
MESSAGES.NEW_SUBMISSION: 'Submitted {submission.title} for {submission.page.title}',
MESSAGES.UPDATE_LEAD: 'Lead changed from {old.lead} to {submission.lead}',
MESSAGES.DETERMINATION_OUTCOME: 'Sent a determination. Outcome: {submission.determination.clean_outcome}',
MESSAGES.INVITED_TO_PROPOSAL: 'Invited to submit a proposal',
MESSAGES.REVIEWERS_UPDATED: 'reviewers_updated',
MESSAGES.NEW_REVIEW: '{user} submitted a review'
def reviewers_updated(self, added, removed, **kwargs):
message = ['Reviewers updated.']
if added:
message.append('Added:')
message.append(', '.join([str(user) for user in added]) + '.')
if removed:
message.append('Removed:')
message.append(', '.join([str(user) for user in removed]) + '.')
return ' '.join(message)
def send_message(self, message, user, submission, **kwargs):
message=message,
MESSAGES.NEW_SUBMISSION: 'A new submission has been submitted for {submission.page.title}: <{link}|{submission.title}>',
MESSAGES.UPDATE_LEAD: 'The lead of <{link}|{submission.title}> has been updated from {old.lead} to {submission.lead} by {user}',
MESSAGES.COMMENT: 'A new comment has been posted on <{link}|{submission.title}>',
MESSAGES.REVIEWERS_UPDATED: '{user} has updated the reviewers on <{link}|{submission.title}>',
MESSAGES.TRANSITION: '{user} has updated the status of <{link}|{submission.title}>: {old_phase.display_name} → {submission.phase}',
MESSAGES.DETERMINATION_OUTCOME: 'A determination for <{link}|{submission.title}> was sent by email. Outcome: {submission.determination.clean_outcome}',
MESSAGES.PROPOSAL_SUBMITTED: 'A proposal has been submitted for review: <{link}|{submission.title}>',
MESSAGES.INVITED_TO_PROPOSAL: '<{link}|{submission.title}> by {submission.user} has been invited to submit a proposal',
MESSAGES.NEW_REVIEW: '{user} has submitted a review for <{link}|{submission.title}>. Outcome: {review.outcome} Score: {review.score}',
MESSAGES.READY_FOR_REVIEW: 'notify_reviewers',
}
def __init__(self):
super().__init__()
self.destination = settings.SLACK_DESTINATION_URL
self.target_room = settings.SLACK_DESTINATION_ROOM
message = super().message(message_type, **kwargs)
user = kwargs['user']
submission = kwargs['submission']
slack_target = self.slack_id(submission.lead)
message = ' '.join([slack_target, message]).strip()
def extra_kwargs(self, message_type, **kwargs):
submission = kwargs['submission']
request = kwargs['request']
link = link_to(submission, request)
return {'link': link}
def notify_reviewers(self, submission, **kwargs):
reviewers_to_notify = []
for reviewer in submission.reviewers.all():
if submission.phase.permissions.can_review(reviewer):
reviewers_to_notify.append(reviewer)
reviewers = ', '.join(
self.slack_id(reviewer) or str(reviewer) for reviewer in reviewers_to_notify
)
return (
'<{link}|{submission.title}> is ready for review. The following are assigned as reviewers: {reviewers}'.format(
reviewers=reviewers,
submission=submission,
**kwargs
)
)
if user.slack:
return f'<{user.slack}>'
return ''
def send_message(self, message, **kwargs):
if not self.destination or not self.target_room:
"room": self.target_room,
requests.post(self.destination, json=data)
class EmailAdapter(AdapterBase):
adapter_type = 'Email'
messages = {
MESSAGES.NEW_SUBMISSION: 'funds/email/confirmation.html',
MESSAGES.TRANSITION: 'messages/email/transition.html',
MESSAGES.DETERMINATION_OUTCOME: 'messages/email/determination.html',
MESSAGES.INVITED_TO_PROPOSAL: 'messages/email/invited_to_proposal.html',
MESSAGES.READY_FOR_REVIEW: 'messages/email/ready_to_review.html',
}
recipients = {
MESSAGES.READY_FOR_REVIEW: 'get_reviewers',
def notify_comment(self, **kwargs):
comment = kwargs['comment']
if not comment.private:
return self.render_message('messages/email/comment.html', **kwargs)
def get_recipients(self, submission):
try:
return getattr(self, self.recipients[''])
def get_reviewers(self, )
return [
[reviewer.email]
for reviewer in submission.reviewers.all()
if submission.phase.permissions.can_review(reviewer)
]
def render_message(self, template, **kwargs):
return render_to_string(template, kwargs)
def send_message(self, message, submission, **kwargs):
subject = submission.page.specific.subject or 'Your application to Open Technology Fund: {submission.title}'.format(submission=submission)
recipients = self.get_recipients(submission) or [(submission.user.email,)]
for recipient in recipients:
send_mail(
subject,
message,
submission.page.specific.from_address,
recipient,
)
def __init__(self, *adpaters):
self.adapters = adpaters
def __call__(self, message_type, request, user, submission, **kwargs):
return self.send(message_type, request=request, user=user, submission=submission, **kwargs)
def send(self, message_type, **kwargs):
for adapter in self.adapters:
adapter.process(message_type, **kwargs)
]
messenger = MessengerBackend(*adapters)