Пример #1
0
def send_pca_missing_notifications():
    """If the PD has en end date that is after PCA end date
    and the PD start date is in the previous CP cycle,
    and the current CP cycle has no PCA
    send a missing PCA notification.
    """
    # get PDs that have end date after PCA end date
    # this means that the CP is in previous cycle
    # (as PCA and CP end dates are always equal)
    # and PD start date in the previous CP cycle
    intervention_qs = Intervention.objects.filter(
        document_type=Intervention.PD,
        agreement__agreement_type=Agreement.PCA,
        agreement__country_programme__from_date__lt=F("start"),
        end__gt=F("agreement__end"))
    for pd in intervention_qs:
        # check that partner has no PCA in the current CP cycle
        cp_previous = pd.agreement.country_programme
        pca_next_qs = Agreement.objects.filter(
            partner=pd.agreement.partner,
            country_programme__from_date__gt=cp_previous.to_date)
        if not pca_next_qs.exists():
            recipients = [u.user.email for u in pd.unicef_focal_points.all()]
            context = {
                "reference_number":
                pd.reference_number,
                "partner_name":
                str(pd.agreement.partner),
                "pd_link":
                reverse("partners_api:intervention-detail", args=[pd.pk]),
            }
            send_notification_using_email_template(
                recipients=recipients,
                email_template_name='partners/intervention/pca_missing',
                context=context)
Пример #2
0
def send_pca_required_notifications():
    """If the PD has an end date that is after the CP to date
    and the it is 30 days prior to the end of the CP,
    send a PCA required notification.
    """
    days_lead = datetime.date.today() + datetime.timedelta(
        days=settings.PCA_REQUIRED_NOTIFICATION_LEAD)
    pd_list = set()
    for cp in CountryProgramme.objects.filter(to_date=days_lead):
        # For PDs related directly to CP
        for pd in cp.interventions.filter(document_type=Intervention.PD,
                                          end__gt=cp.to_date):
            pd_list.add(pd)

        # For PDs by way of agreement
        for agreement in cp.agreements.filter(
                interventions__end__gt=cp.to_date):
            for pd in agreement.interventions.filter(
                    document_type=Intervention.PD, end__gt=cp.to_date):
                pd_list.add(pd)

    for pd in pd_list:
        recipients = [u.user.email for u in pd.unicef_focal_points.all()]
        context = {
            "reference_number": pd.reference_number,
            "partner_name": str(pd.agreement.partner),
            "pd_link": reverse("partners_api:intervention-detail",
                               args=[pd.pk]),
        }
        send_notification_using_email_template(
            recipients=recipients,
            email_template_name='partners/intervention/new_pca_required',
            context=context)
Пример #3
0
    def _send_email(self,
                    recipients,
                    template_name,
                    context=None,
                    user=None,
                    include_token=False,
                    **kwargs):
        context = context or {}

        base_context = {
            'visit': self.get_mail_context(user=user,
                                           include_token=include_token),
            'environment': get_environment(),
        }
        base_context.update(context)
        context = base_context

        if isinstance(recipients, str):
            recipients = [
                recipients,
            ]
        else:
            recipients = list(recipients)

        # assert recipients
        if recipients:
            send_notification_using_email_template(
                recipients=recipients,
                email_template_name=template_name,
                context=context,
            )
Пример #4
0
def send_invite_email(staff):
    context = {
        'environment': get_environment(),
        'login_link': get_token_auth_link(staff.user)
    }

    send_notification_using_email_template(
        recipients=[staff.user.email],
        email_template_name='organisations/staff_member/invite',
        context=context)
Пример #5
0
    def test_extends(self):
        send_notification_using_email_template(
            recipients=['*****@*****.**'],
            from_address='*****@*****.**',
            email_template_name='template1',
            context={},
        )

        self.assertEqual(len(mail.outbox), 1)
        content = mail.outbox[0].alternatives[0]
        self.assertEqual(content[1], 'text/html')
        self.assertIn('Base template', content[0])
        self.assertIn('Template1', content[0])
Пример #6
0
def _notify_of_ended_interventions_with_mismatched_frs(country_name):
    """Implementation core of intervention_notification_ended_fr_outstanding() (q.v.)"""
    logger.info('Starting intervention signed but FRs Amount and actual '
                'do not match notifications for country {}'.format(country_name))

    ended_interventions = Intervention.objects.filter(status=Intervention.ENDED)
    for intervention in ended_interventions:
        if intervention.total_frs['total_actual_amt'] != intervention.total_frs['total_frs_amt']:
            email_context = get_intervention_context(intervention)
            send_notification_using_email_template(
                sender=intervention,
                recipients=email_context['unicef_focal_points'],
                email_template_name="partners/partnership/ended/frs/outstanding",
                context=email_context
            )
Пример #7
0
def _notify_of_signed_interventions_with_no_frs(country_name):
    """Implementation core of intervention_notification_signed_no_frs() (q.v.)"""
    logger.info('Starting intervention signed but no FRs notifications for country {}'.format(country_name))

    signed_interventions = Intervention.objects.filter(status=Intervention.SIGNED,
                                                       start__gte=datetime.date.today(),
                                                       frs__isnull=True)

    for intervention in signed_interventions:
        email_context = get_intervention_context(intervention)
        send_notification_using_email_template(
            sender=intervention,
            recipients=email_context['unicef_focal_points'],
            email_template_name="partners/partnership/signed/frs",
            context=email_context
        )
Пример #8
0
    def form_valid(self, form):
        login_link = get_token_auth_link(form.get_user())

        redirect_to = form.data.get('next', self.request.GET.get('next'))
        if redirect_to:
            login_link = update_url_with_kwargs(login_link, next=redirect_to)

        email_context = {
            'recipient': form.get_user().get_full_name(),
            'login_link': login_link,
        }

        send_notification_using_email_template(
            recipients=[form.get_user().email],
            email_template_name='email_auth/token/login',
            context=email_context)

        return self.render_to_response(
            self.get_context_data(email=form.get_user().email))
Пример #9
0
def _notify_interventions_ending_soon(country_name):
    """Implementation core of intervention_notification_ending() (q.v.)"""
    logger.info('Starting interventions almost ending notifications for country {}'.format(country_name))

    today = datetime.date.today()

    notify_end_dates = [today + datetime.timedelta(days=delta) for delta in _INTERVENTION_ENDING_SOON_DELTAS]

    interventions = Intervention.objects.filter(status=Intervention.ACTIVE, end__in=notify_end_dates)
    interventions = interventions.prefetch_related('unicef_focal_points', 'agreement', 'agreement__partner')

    for intervention in interventions:
        email_context = get_intervention_context(intervention)
        email_context["days"] = str((intervention.end - today).days)
        send_notification_using_email_template(
            sender=intervention,
            recipients=email_context['unicef_focal_points'],
            email_template_name="partners/partnership/ending",
            context=email_context
        )
Пример #10
0
    def _notify_focal_points(self, template_name, context=None):
        for focal_point in get_user_model().objects.filter(
                groups=UNICEFAuditFocalPoint.as_group()):
            # Build the context in the same order the previous version of the code did,
            # just in case something relies on it (intentionally or not).
            ctx = {
                'focal_point': focal_point.get_full_name(),
            }
            if context:
                ctx.update(context)
            base_context = {
                'engagement': self.get_mail_context(user=focal_point),
                'environment': get_environment(),
            }
            base_context.update(ctx)
            context = base_context

            send_notification_using_email_template(
                recipients=[focal_point.email],
                email_template_name=template_name,
                context=context,
            )
Пример #11
0
def notify_partner_hidden(partner_pk, tenant_name):

    with schema_context(tenant_name):
        partner = PartnerOrganization.objects.get(pk=partner_pk)
        pds = Intervention.objects.filter(
            agreement__partner__name=partner.name,
            status__in=[Intervention.SIGNED, Intervention.ACTIVE, Intervention.ENDED]
        )
        if pds:
            email_context = {
                'partner_name': partner.name,
                'pds': ', '.join(pd.number for pd in pds),
                'environment': get_environment(),
            }
            emails_to_pd = [pd.unicef_focal_points.values_list('email', flat=True) for pd in pds]
            recipients = set(itertools.chain.from_iterable(emails_to_pd))

            send_notification_using_email_template(
                recipients=list(recipients),
                email_template_name='partners/blocked_partner',
                context=email_context
            )