def bom_notification(sender, instance, **kwargs):
    company_staff = instance.owner.get_owners_and_delegates()
    profile = get_current_profile()
    # If there is no JWT token in the request,
    # then we don't create notifications (Useful at admin & shell for debugging)
    if not profile:
        return

    try:
        content = "Mr <strong>%s %s</strong> have created this event. " % (profile.first_name, profile.last_name)
        if 'created' in kwargs:
            if kwargs['created']:
                subject = _('New Bom (%s) created in company (%s)' % (instance.title, instance.owner.name))
            else:
                subject = _('Bom (%s) updated in company (%s)' % (instance.title, instance.owner.name))
        else:
            subject = _('Bom (%s) deleted in company (%s)' % (instance.title, instance.owner.name))

        final_content = "For simplicity, the following button will redirect to the target page."
        body = get_html_message(content, final_content, os.path.join(settings.PROTOCOL+'://', settings.BASE_URL, 'preventivi'))
        type = ContentType.objects.get(model=sender.__name__.lower())

        notify_obj = notify_models.Notify.objects.create(
            sender=profile, subject=subject, body=body,
            content_type=type, object_id=instance.id,
            creator=profile.user, last_modifier=profile.user,
        )

        recipient_objs = []

        for staff in company_staff:
            bell_status = get_bell_notification_status(
                staff, sender.__name__.lower()
            )
            email_status = get_email_notification_status(
                staff, sender.__name__.lower()
            )

            if bell_status or email_status:
                recipient_objs.append(notify_models.NotificationRecipient(
                    notification=notify_obj, is_email=email_status,
                    is_notify=bell_status, recipient=staff,
                    creator=profile.user, last_modifier=profile.user)
                )

        notify_models.NotificationRecipient.objects.bulk_create(
            recipient_objs,
            batch_size=100
        )
    except Exception as e:
        print(e)
Exemple #2
0
def document_notification(sender, instance, **kwargs):
    company_staff = []
    profile = get_current_profile()
    # If there is no JWT token in the request,
    # then we don't create notifications (Useful at admin & shell for debugging)
    if not profile:
        return

    try:
        endpoint = os.path.join(settings.PROTOCOL + '://', settings.BASE_URL,
                                'dashboard')
        if instance.content_type.name == 'company':
            company_staff = instance.content_object.profiles.all()
            title = instance.content_object.name
        elif instance.content_type.name == 'project':
            # Level 1 project
            if instance.content_object.shared_project:
                level1_project = instance.content_object.shared_project.profiles.all(
                ).union(instance.content_object.shared_project.company.
                        get_owners_and_delegates())
                company_staff = instance.content_object.profiles.all().union(
                    instance.content_object.company.get_owners_and_delegates(),
                    level1_project)
            else:
                company_staff = instance.content_object.profiles.all().union(
                    instance.content_object.company.get_owners_and_delegates())
            endpoint = os.path.join(settings.PROTOCOL + '://',
                                    settings.BASE_URL,
                                    'project/%s' % instance.object_id)
            title = instance.content_object.name
        elif instance.content_type.name == 'bom':
            company_staff = instance.content_object.owner.profiles.all()
            endpoint = os.path.join(settings.PROTOCOL + '://',
                                    settings.BASE_URL, 'preventivi')
            title = instance.content_object.title

        content = "Mr <strong>%s %s</strong> have created this event. " % (
            profile.first_name, profile.last_name)

        if 'created' in kwargs:
            if kwargs['created']:
                subject = _(
                    'New Document (%s) created in %s (%s)' %
                    (instance.title, instance.content_type.name, title))
            else:
                subject = _(
                    'Document (%s) updated in %s (%s)' %
                    (instance.title, instance.content_type.name, title))
        else:
            subject = _('Document (%s) deleted in %s (%s)' %
                        (instance.title, instance.content_type.name, title))

        final_content = "For simplicity, the following button will redirect to the target page."
        body = get_html_message(content, final_content, endpoint)
        type = ContentType.objects.get(
            model=instance.content_type.name.lower())

        notify_obj = notify_models.Notify.objects.create(
            sender=profile,
            subject=subject,
            body=body,
            content_type=type,
            object_id=instance.object_id,
            creator=profile.user,
            last_modifier=profile.user)

        recipient_objs = []

        for staff in company_staff:
            bell_status = get_bell_notification_status(
                staff, instance.content_type.name)
            email_status = get_email_notification_status(
                staff, instance.content_type.name)

            if bell_status or email_status:
                recipient_objs.append(
                    notify_models.NotificationRecipient(
                        notification=notify_obj,
                        is_email=email_status,
                        is_notify=bell_status,
                        recipient=staff,
                        creator=profile.user,
                        last_modifier=profile.user))

        notify_models.NotificationRecipient.objects.bulk_create(recipient_objs,
                                                                batch_size=100)
    except Exception as e:
        print(e)
def message_notification(sender, instance, **kwargs):
    company_staff = []
    profile = get_current_profile()
    # If there is no JWT token in the request,
    # then we don't create notifications (Useful at admin & shell for debugging)
    if not profile:
        return

    try:
        endpoint = os.path.join(settings.PROTOCOL+'://', settings.BASE_URL, 'comunica')
        if instance.talk.content_type.name == 'company':
            company_staff = instance.talk.content_object.profiles.all()
            title = instance.talk.content_type.name
            source = instance.talk.content_object.name
        elif instance.talk.content_type.name == 'project':
            endpoint = os.path.join(settings.PROTOCOL+'://', settings.BASE_URL, 'project')
            title = instance.talk.content_type.name
            company_staff = instance.talk.content_object.profiles.all().union(
                instance.talk.content_object.company.get_owners_and_delegates(),
                instance.sender.company.get_owners_and_delegates()
            )
            all_staff = instance.talk.content_object.internal_projects.all().values_list('profiles', flat=True)
            if all_staff:
                company_staff = company_staff.union(profile_models.Profile.objects.filter(id__in=all_staff))
            source = instance.talk.content_object.name
        elif instance.talk.content_type.name == 'profile':
            company_staff = [instance.talk.content_object]
            title = 'staff'
            source = instance.sender.last_name

        content = "Mr <strong>%s %s</strong> have created this event. " % (profile.first_name, profile.last_name)

        if 'created' in kwargs:
            if kwargs['created']:
                subject = _('New Message from %s (%s)' % (title, source))
            else:
                subject = _('Message updated by %s (%s)' % (title, source))
        else:
            subject = _('Message deleted by %s (%s)' % (title, source))

        final_content = "For simplicity, the following button will redirect to the target page."
        body = get_html_message(content, final_content, endpoint)
        type = ContentType.objects.get(model=instance.talk.content_type.name.lower())

        notify_obj = notify_models.Notify.objects.create(
            sender=profile, subject=subject, body=body,
            content_type=type, object_id=instance.talk.object_id,
            creator=profile.user, last_modifier=profile.user
        )

        recipient_objs = []

        for staff in company_staff:
            bell_status = get_bell_notification_status(
                staff, instance.talk.content_type.name
            )
            email_status = get_email_notification_status(
                staff, instance.talk.content_type.name
            )

            if bell_status or email_status:
                recipient_objs.append(notify_models.NotificationRecipient(
                    notification=notify_obj, is_email=email_status,
                    is_notify=bell_status, recipient=staff,
                    creator=profile.user, last_modifier=profile.user)
                )

        notify_models.NotificationRecipient.objects.bulk_create(
            recipient_objs,
            batch_size=100
        )
    except Exception as e:
        print(e)
def bought_offer_notification(sender, instance, **kwargs):
    company_staff = instance.offer.owner.get_owners_and_delegates()
    profile = get_current_profile()
    # If there is no JWT token in the request,
    # then we don't create notifications (Useful at admin & shell for debugging)
    if not profile:
        return

    try:
        if 'created' in kwargs:
            if kwargs['created']:
                subject_en = _('Offer (%s) bought' % (instance.offer.title))
                subject_it = _('Acquisto vostra offerta (%s)' % (instance.offer.title))
                content_en = "Mr <strong>%s %s</strong> have bought your offer <strong>%s</strong>. " % (
                                profile.first_name, profile.last_name, instance.offer.title)
                final_content_en = '''<p>Please contact <strong>%s %s </strong> for further informations.<br><br>
                                   <strong>Company:</strong> %s<br>
                                   <strong>Mobile:</strong> %s<br>
                                   <strong>Tel:</strong> %s<br>
                                   <strong>Email:</strong> %s<br>
                                   <strong>WWW:</strong> %s<br></p>''' % (profile.first_name, profile.last_name,
                                        profile.company.name, profile.mobile, profile.phone, profile.email,
                                        profile.company.url)
                content_it = "<strong>%s %s</strong> ha acquistato la vostra offerta <strong>%s</strong>. " % (
                    profile.first_name, profile.last_name, instance.offer.title)
                final_content_it = '''<p>Si prega di contattare <strong>%s %s </strong> per ulteriori informazioni.<br>
                                       <br>
                                       <strong>Azienda:</strong> %s<br>
                                       <strong>Mobile:</strong> %s<br>
                                       <strong>Tel:</strong> %s<br>
                                       <strong>Email:</strong> %s<br>
                                       <strong>WWW:</strong> %s<br></p>''' % (
                                        profile.first_name, profile.last_name,
                                        profile.company.name, profile.mobile, profile.phone, profile.email,
                                        profile.company.url)

                body_en = get_html_message(content_en, final_content_en)
                body_it = get_html_message(content_it, final_content_it)
                type = ContentType.objects.get(model=quotation_models.Offer.__name__.lower())

                it_recipient = company_staff.filter(language='it')

                en_recipient = company_staff.filter(~Q(language='it'))

                recipient_objs = []
                if it_recipient:
                    notify_obj = notify_models.Notify.objects.create(
                        sender=profile, subject=subject_it, body=body_it,
                        content_type=type, object_id=instance.id,
                        creator=profile.user, last_modifier=profile.user,
                    )
                    for staff in it_recipient:
                        bell_status = get_bell_notification_status(
                            staff, sender.__name__.lower()
                        )
                        email_status = get_email_notification_status(
                            staff, sender.__name__.lower()
                        )

                        if bell_status or email_status:
                            recipient_objs.append(notify_models.NotificationRecipient(
                                notification=notify_obj, is_email=email_status,
                                is_notify=bell_status, recipient=staff,
                                creator=profile.user, last_modifier=profile.user)
                            )

                if en_recipient:
                    notify_obj = notify_models.Notify.objects.create(
                        sender=profile, subject=subject_en, body=body_en,
                        content_type=type, object_id=instance.id,
                        creator=profile.user, last_modifier=profile.user,
                    )
                    for staff in en_recipient:
                        bell_status = get_bell_notification_status(
                            staff, sender.__name__.lower()
                        )
                        email_status = get_email_notification_status(
                            staff, sender.__name__.lower()
                        )

                        if bell_status or email_status:
                            recipient_objs.append(notify_models.NotificationRecipient(
                                notification=notify_obj, is_email=email_status,
                                is_notify=bell_status, recipient=staff,
                                creator=profile.user, last_modifier=profile.user)
                            )

                notify_models.NotificationRecipient.objects.bulk_create(
                    recipient_objs,
                    batch_size=100
                )
    except Exception as e:
        print(e)
def profile_notification(sender, instance, **kwargs):
    company_staff = []
    profile = get_current_profile()
    # If there is no JWT token in the request,
    # then we don't create notifications (Useful at admin & shell for debugging)
    if not profile:
        if 'created' in kwargs:
            if not kwargs['created']:
                if not profile:
                    profile = instance

    if not profile:
        return

    try:
        if not instance.user and instance.email:
            if instance.__create:
                registration_link = os.path.join(settings.PROTOCOL + '://',
                                                 settings.BASE_URL, 'register')
                from_mail = settings.NOTIFY_NOTIFY_NO_REPLY_EMAIL
                subject = _('Your email is added to WhistlePRO')
                context = {
                    'logo_url':
                    os.path.join(settings.PROTOCOL + '://', settings.BASE_URL,
                                 'assets/images/patterns/logowhistle.png'),
                    'recipient_first_name':
                    instance.first_name,
                    'recipient_last_name':
                    instance.last_name,
                    'recipient_email':
                    instance.email,
                    'company_name':
                    instance.company.name,
                    'registration_page':
                    registration_link
                }

                lang = instance.language if instance.language else 'en'

                # Text message
                text_message = render_to_string(
                    'profile/profile/email/registration_phantom_request_{}.txt'
                    .format(lang), context)

                # Html message
                html_message = render_to_string(
                    'profile/profile/email/registration_phantom_request_{}.html'
                    .format(lang), context)

                try:
                    send_mail(
                        subject=subject,
                        message=text_message,
                        html_message=html_message,
                        recipient_list=[instance.email],
                        from_email=from_mail,
                    )
                except Exception as e:
                    print(e)

        else:
            endpoint = os.path.join(settings.PROTOCOL + '://',
                                    settings.BASE_URL, 'profile')
            content = "Mr <strong>%s %s</strong> have created this event. " % (
                profile.first_name, profile.last_name)
            if 'created' in kwargs:
                if kwargs['created']:
                    if instance.company_invitation_date:
                        company_staff = [instance]
                        subject = _('New invitation from company (%s) ' %
                                    instance.company.name)
                    else:
                        company_staff = instance.company.get_owners_and_delegates(
                        )
                        subject = _('New invitation from staff (%s) (%s) ' %
                                    (instance.first_name, instance.last_name))
                        endpoint = os.path.join(settings.PROTOCOL + '://',
                                                settings.BASE_URL, 'organico')
                else:
                    # Todo: beautify the following logic
                    old_obj = copy.deepcopy(
                        instance._Profile__original_instance)
                    instance.__dict__.pop('_Profile__original_instance', None)
                    new_obj = instance.__dict__

                    diffkeys = []

                    for key, value in old_obj.items():
                        if value != new_obj[key]:
                            diffkeys.append(key)

                    invitations_type = [
                        'invitation_refuse_date', 'company_invitation_date',
                        'profile_invitation_date'
                    ]
                    if any(x in invitations_type for x in diffkeys):

                        get_types = list(set(invitations_type) & set(diffkeys))
                        if get_types:
                            if len(get_types) >= 2:
                                if all(x in [
                                        'invitation_refuse_date',
                                        'company_invitation_date'
                                ] for x in get_types):
                                    company_staff = [instance]
                                    subject = _(
                                        'Invitation reaccepted by company (%s) '
                                        % instance.company.name)
                                elif all(x in [
                                        'invitation_refuse_date',
                                        'profile_invitation_date'
                                ] for x in get_types):
                                    company_staff = instance.company.get_owners_and_delegates(
                                    )
                                    subject = _(
                                        'Invitation reaccepted by staff (%s) (%s) '
                                        % (instance.first_name,
                                           instance.last_name))
                                else:
                                    return
                            else:
                                if get_types[0] == 'invitation_refuse_date':
                                    if not instance.company_invitation_date:
                                        company_staff = [instance]
                                        subject = _(
                                            'Invitation refused by company (%s) '
                                            % instance.company.name)
                                    else:
                                        company_staff = instance.company.get_owners_and_delegates(
                                        )
                                        subject = _(
                                            'Invitation refused by staff (%s) (%s) '
                                            % (instance.first_name,
                                               instance.last_name))
                                        endpoint = os.path.join(
                                            settings.PROTOCOL + '://',
                                            settings.BASE_URL, 'organico')
                                elif get_types[0] == 'company_invitation_date':
                                    company_staff = [instance]
                                    subject = _(
                                        'Invitation accepted by company (%s) '
                                        % instance.company.name)
                                elif get_types[0] == 'profile_invitation_date':
                                    company_staff = instance.company.get_owners_and_delegates(
                                    )
                                    subject = _(
                                        'Invitation accepted by staff (%s) (%s) '
                                        % (instance.first_name,
                                           instance.last_name))
                                else:
                                    return
                        else:
                            return
                    else:
                        company_staff = profile_models.Profile.objects.filter(
                            pk=instance.id).union(
                                instance.company.get_owners_and_delegates())
                        subject = _(
                            'Profile (%s %s) information has been changed by company (%s)'
                            % (instance.first_name, instance.last_name,
                               instance.company.name))
                        endpoint = os.path.join(settings.PROTOCOL + '://',
                                                settings.BASE_URL, 'organico')
            else:
                company_staff = profile_models.Profile.objects.filter(
                    id=instance.id).union(
                        instance.company.get_owners_and_delegates())
                subject = _('Profile (%s %s) deleted by company (%s)' %
                            (instance.first_name, instance.last_name,
                             instance.company.name))
                endpoint = os.path.join(settings.PROTOCOL + '://',
                                        settings.BASE_URL, 'organico')

            final_content = "For simplicity, the following button will redirect to the target page."
            body = get_html_message(content, final_content, endpoint)
            type = ContentType.objects.get(model='profile')

            notify_obj = notify_models.Notify.objects.create(
                sender=profile,
                subject=subject,
                body=body,
                content_type=type,
                object_id=instance.id,
                creator=profile.user,
                last_modifier=profile.user)

            recipient_objs = []

            for staff in company_staff:
                bell_status = get_bell_notification_status(staff, 'profile')
                email_status = get_email_notification_status(staff, 'profile')

                if bell_status or email_status:
                    recipient_objs.append(
                        notify_models.NotificationRecipient(
                            notification=notify_obj,
                            is_email=email_status,
                            is_notify=bell_status,
                            recipient=staff,
                            creator=profile.user,
                            last_modifier=profile.user))

            notify_models.NotificationRecipient.objects.bulk_create(
                recipient_objs, batch_size=100)
    except Exception as e:
        print(e)