Skip to content

Base

hypha.apply.activity.adapters.base

neat_related = {DETERMINATION_OUTCOME: 'determination', BATCH_DETERMINATION_OUTCOME: 'determinations', UPDATE_LEAD: 'old_lead', NEW_REVIEW: 'review', TRANSITION: 'old_phase', BATCH_TRANSITION: 'transitions', APPLICANT_EDIT: 'revision', EDIT_SUBMISSION: 'revision', COMMENT: 'comment', SCREENING: 'old_status', REVIEW_OPINION: 'opinion', DELETE_REVIEW: 'review', DELETE_REVIEW_OPINION: 'review_opinion', EDIT_REVIEW: 'review', CREATED_PROJECT: 'submission', PROJECT_TRANSITION: 'old_stage', APPROVE_PAF: 'paf_approvals', UPDATE_PROJECT_LEAD: 'old_lead', APPROVE_CONTRACT: 'contract', UPLOAD_CONTRACT: 'contract', CREATE_INVOICE: 'create_invoice', UPDATE_INVOICE_STATUS: 'invoice', APPROVE_INVOICE: 'invoice', DELETE_INVOICE: 'invoice', UPDATE_INVOICE: 'invoice', SUBMIT_REPORT: 'report', SKIPPED_REPORT: 'report', REPORT_FREQUENCY_CHANGED: 'config', REPORT_NOTIFY: 'report', CREATE_REMINDER: 'reminder', DELETE_REMINDER: 'reminder', REVIEW_REMINDER: 'reminder'}

AdapterBase

messages class-attribute instance-attribute

messages = {}

always_send class-attribute instance-attribute

always_send = False

message

message(message_type, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
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)
    else:
        # Delegate all responsibility to the custom method
        return method(**kwargs)

render_message

render_message(message, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def render_message(self, message, **kwargs):
    return message.format(**kwargs)

extra_kwargs

extra_kwargs(message_type, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def extra_kwargs(self, message_type, **kwargs):
    return {}
get_neat_related(message_type, related)
Source code in hypha/apply/activity/adapters/base.py
def get_neat_related(self, message_type, related):
    # We translate the related kwarg into something we can understand
    try:
        neat_name = neat_related[message_type]
    except KeyError:
        # Message type doesn't expect a related object
        if related:
            raise ValueError(
                f"Unexpected 'related' kwarg provided for {message_type}"
            ) from None
        return {}
    else:
        if not related:
            raise ValueError(f"{message_type} expects a 'related' kwarg")
        return {neat_name: related}

recipients

recipients(message_type, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def recipients(self, message_type, **kwargs):
    raise NotImplementedError()

batch_recipients

batch_recipients(message_type, sources, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def batch_recipients(self, message_type, sources, **kwargs):
    # Default batch recipients is to send a message to each of the recipients that would
    # receive a message under normal conditions
    return [
        {
            "recipients": self.recipients(message_type, source=source, **kwargs),
            "sources": [source],
        }
        for source in sources
    ]

process_batch

process_batch(message_type, events, request, user, sources, related=None, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def process_batch(
    self, message_type, events, request, user, sources, related=None, **kwargs
):
    events_by_source = {event.source.id: event for event in events}
    for recipient in self.batch_recipients(
        message_type, sources, user=user, **kwargs
    ):
        recipients = recipient["recipients"]
        sources = recipient["sources"]
        events = [events_by_source[source.id] for source in sources]
        self.process_send(
            message_type,
            recipients,
            events,
            request,
            user,
            sources=sources,
            source=None,
            related=related,
            **kwargs,
        )

process

process(message_type, event, request, user, source, related=None, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def process(
    self, message_type, event, request, user, source, related=None, **kwargs
):
    recipients = self.recipients(
        message_type,
        source=source,
        related=related,
        user=user,
        request=request,
        **kwargs,
    )
    self.process_send(
        message_type,
        recipients,
        [event],
        request,
        user,
        source,
        related=related,
        **kwargs,
    )

process_send

process_send(message_type, recipients, events, request, user, source, sources=None, related=None, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def process_send(
    self,
    message_type,
    recipients,
    events,
    request,
    user,
    source,
    sources=None,
    related=None,
    **kwargs,
):
    if sources is None:
        sources = []
    try:
        # If this was a batch action we want to pull out the submission
        source = sources[0]
    except IndexError:
        pass

    kwargs = {
        "request": request,
        "user": user,
        "source": source,
        "sources": sources,
        "related": related,
        **kwargs,
    }
    kwargs.update(self.get_neat_related(message_type, related))
    kwargs.update(self.extra_kwargs(message_type, **kwargs))

    message = self.message(message_type, **kwargs)
    if not message:
        return

    for recipient in recipients:
        message_logs = self.create_logs(message, recipient, *events)

        if settings.SEND_MESSAGES or self.always_send:
            status = self.send_message(
                message, recipient=recipient, logs=message_logs, **kwargs
            )
        else:
            status = "Message not sent as SEND_MESSAGES==FALSE"

        message_logs.update_status(status)

        if not settings.SEND_MESSAGES:
            if recipient:
                debug_message = "{} [to: {}]: {}".format(
                    self.adapter_type, recipient, message
                )
            else:
                debug_message = "{}: {}".format(self.adapter_type, message)
            messages.add_message(request, messages.DEBUG, debug_message)

create_logs

create_logs(message, recipient, *events)
Source code in hypha/apply/activity/adapters/base.py
def create_logs(self, message, recipient, *events):
    from ..models import Message

    messages = Message.objects.bulk_create(
        Message(**self.log_kwargs(message, recipient, event)) for event in events
    )
    return Message.objects.filter(id__in=[message.id for message in messages])

log_kwargs

log_kwargs(message, recipient, event)
Source code in hypha/apply/activity/adapters/base.py
def log_kwargs(self, message, recipient, event):
    return {
        "type": self.adapter_type,
        "content": message,
        "recipient": recipient or "",
        "event": event,
    }

send_message

send_message(message, **kwargs)
Source code in hypha/apply/activity/adapters/base.py
def send_message(self, message, **kwargs):
    # Process the message, should return the result of the send
    # Returning None will not record this action
    raise NotImplementedError()