示例#1
0
    def pvr_notification(previous_state, credit_trade):
        notification_map = defaultdict(lambda: [])
        government = Organization.objects.filter(
            type__type='Government').first()

        StatusChange = namedtuple('StatusChange', ['new_status'])
        ResultingNotification = namedtuple('ResultingNotification',
                                           ['recipient', 'notification_type'])

        if previous_state and \
                previous_state.status.status == 'Recommended' and \
                previous_state.update_user == credit_trade.update_user:
            notification_map[StatusChange('Draft')] = [
                ResultingNotification(government,
                                      NotificationType.PVR_PULLED_BACK)
            ]
        elif previous_state and \
                previous_state.status.status == 'Recommended' and \
                previous_state.update_user != credit_trade.update_user:
            notification_map[StatusChange('Draft')] = [
                ResultingNotification(government,
                                      NotificationType.PVR_RETURNED_TO_ANALYST)
            ]
        else:
            notification_map[StatusChange('Draft')] = [
                ResultingNotification(government, NotificationType.PVR_CREATED)
            ]

        notification_map[StatusChange('Recommended')] = [
            ResultingNotification(
                government, NotificationType.PVR_RECOMMENDED_FOR_APPROVAL),
        ]

        notification_map[StatusChange('Approved')] = [
            ResultingNotification(government, NotificationType.PVR_APPROVED),
        ]

        notification_map[StatusChange('Declined')] = [
            ResultingNotification(credit_trade.respondent,
                                  NotificationType.PVR_DECLINED),
            ResultingNotification(government, NotificationType.PVR_DECLINED),
        ]

        notifications_to_send = notification_map[StatusChange(
            credit_trade.status.status)]

        for notification in notifications_to_send:
            AMQPNotificationService.send_notification(
                interested_organization=notification.recipient,
                message=notification.notification_type.name,
                notification_type=notification.notification_type,
                related_credit_trade=credit_trade,
                related_organization=credit_trade.respondent,
                originating_user=credit_trade.update_user)
示例#2
0
def async_send_notification(message: str = None,
                            interested_organization_id=None,
                            interested_role_names: List[str] = [],
                            related_credit_trade_id=None,
                            related_document_id=None,
                            related_organization_id=None,
                            related_report_id=None,
                            related_user_id=None,
                            is_error: bool = False,
                            is_warning: bool = False,
                            is_global: bool = False,
                            notification_type: NotificationType = None,
                            originating_user_id=None):
    print('sending notification')

    interested_organization = Organization.objects.get(
        id=interested_organization_id) if interested_organization_id else None

    interested_roles = Role.objects.filter(
        name__in=interested_role_names).all()

    related_credit_trade = CreditTrade.objects.get(
        id=related_credit_trade_id) if related_credit_trade_id else None

    related_document = Document.objects.get(
        id=related_document_id) if related_document_id else None

    related_organization = Organization.objects.get(
        id=related_organization_id) if related_organization_id else None

    related_report = ComplianceReport.objects.get(
        id=related_report_id) if related_report_id else None

    related_user = User.objects.get(
        id=related_user_id) if related_user_id else None

    originating_user = User.objects.get(
        id=originating_user_id) if originating_user_id else None

    notification_type_value = NotificationType(notification_type)

    AMQPNotificationService.send_notification(
        message, interested_organization, interested_roles,
        related_credit_trade, related_document, related_organization,
        related_report, related_user, is_error, is_warning, is_global,
        notification_type_value, originating_user)
示例#3
0
    def update_subscription(self, request):
        """
        Updates the User's subscriptions to specified notification types
        """
        if isinstance(request.data, list):
            serializer = self.get_serializer(data=request.data, many=True)
            serializer.is_valid(raise_exception=True)
            updated_subscriptions = serializer.validated_data
        else:
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            updated_subscriptions = [serializer.validated_data]

        for subscription in updated_subscriptions:
            AMQPNotificationService.update_subscription(
                user=request.user,
                channel=subscription['channel'],
                notification_type=subscription['notification_type'],
                subscribed=subscription['subscribed'])

        return Response(None, status=status.HTTP_200_OK)
示例#4
0
    def send_notification(document, originating_user):
        interested_organizations = []

        government = Organization.objects.filter(
            type__type='Government').first()

        user = document.create_user

        notification_type = None

        if document.status.status == "Submitted":
            interested_organizations.extend([government, user.organization])
            notification_type = NotificationType.DOCUMENT_SUBMITTED

        elif document.status.status == "Pending Submission":
            interested_organizations.append(user.organization)
            notification_type = NotificationType.DOCUMENT_PENDING_SUBMISSION

        elif document.status.status == "Received":
            interested_organizations.extend([government, user.organization])
            notification_type = NotificationType.DOCUMENT_RECEIVED

        elif document.status.status == "Archived":
            interested_organizations.extend([government, user.organization])
            notification_type = NotificationType.DOCUMENT_ARCHIVED

        elif document.status.status == "Security Scan Failed":
            interested_organizations.append(user.organization)
            notification_type = NotificationType.DOCUMENT_SCAN_FAILED

        if notification_type:
            for organization in interested_organizations:
                AMQPNotificationService.send_notification(
                    interested_organization=organization,
                    message=notification_type.name,
                    notification_type=notification_type,
                    originating_user=originating_user,
                    related_document=document)
示例#5
0
 def subscriptions(self, request):
     user = request.user
     data = AMQPNotificationService.compute_effective_subscriptions(user)
     serializer = EffectiveSubscriptionSerializer(data, many=True)
     return Response(serializer.data)
示例#6
0
    def credit_trade_notification(_previous_state, credit_trade):
        notification_map = defaultdict(lambda: [])
        government = Organization.objects.filter(
            type__type='Government').first()

        StatusChange = namedtuple('StatusChange', ['new_status'])
        ResultingNotification = namedtuple('ResultingNotification', [
            'recipient', 'notification_type'])

        notification_map[StatusChange('Draft')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_CREATED)
        ]
        notification_map[StatusChange('Submitted')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_SIGNED_1OF2),
            ResultingNotification(
                credit_trade.respondent,
                NotificationType.CREDIT_TRANSFER_SIGNED_1OF2)
        ]
        notification_map[StatusChange('Accepted')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_SIGNED_2OF2),
            ResultingNotification(
                credit_trade.respondent,
                NotificationType.CREDIT_TRANSFER_SIGNED_2OF2),
            ResultingNotification(
                government, NotificationType.CREDIT_TRANSFER_SIGNED_2OF2),
        ]
        notification_map[StatusChange('Refused')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_PROPOSAL_REFUSED),
            ResultingNotification(
                credit_trade.respondent,
                NotificationType.CREDIT_TRANSFER_PROPOSAL_REFUSED)
        ]
        notification_map[StatusChange('Recommended')] = [
            ResultingNotification(
                government,
                NotificationType.CREDIT_TRANSFER_RECOMMENDED_FOR_APPROVAL),
        ]

        notification_map[StatusChange('Not Recommended')] = [
            ResultingNotification(
                government,
                NotificationType.CREDIT_TRANSFER_RECOMMENDED_FOR_DECLINATION),
        ]

        notification_map[StatusChange('Approved')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_APPROVED),
            ResultingNotification(
                credit_trade.respondent,
                NotificationType.CREDIT_TRANSFER_APPROVED),
            ResultingNotification(
                government,
                NotificationType.CREDIT_TRANSFER_APPROVED),
        ]

        notification_map[StatusChange('Declined')] = [
            ResultingNotification(
                credit_trade.initiator,
                NotificationType.CREDIT_TRANSFER_DECLINED),
            ResultingNotification(
                credit_trade.respondent,
                NotificationType.CREDIT_TRANSFER_DECLINED),
            ResultingNotification(
                government,
                NotificationType.CREDIT_TRANSFER_DECLINED),
        ]

        notifications_to_send = notification_map[
            StatusChange(
                credit_trade.status.status
            )
        ]

        for notification in notifications_to_send:
            AMQPNotificationService.send_notification(
                interested_organization=notification.recipient,
                message=notification.notification_type.name,
                notification_type=notification.notification_type,
                related_credit_trade=credit_trade,
                related_organization=credit_trade.respondent,
                originating_user=credit_trade.update_user
            )