Esempio n. 1
0
    def action(self):
        """ send SMS to district/region | email to PNLP for each new report """

        for report in MalariaReport.unvalidated\
                                   .filter(period=self.args.period):

            # entity has no parent. Either Mali or error
            if not report.entity.parent:
                # should never happen but we never know
                # drop if entity has no parent.
                if not report.entity.level == 0:
                    return

                # we now have a national report
                logger.info(u"Report %s found." % report)
                report._status = MalariaReport.STATUS_VALIDATED
                with reversion.create_revision():
                    report.save()
                    reversion.set_user(get_autobot().user)
                    #report.save()

                # send emails
                ct, oi = Access.target_data(Entity.objects.get(slug='mali'))
                nat_access = list(Access.objects.filter(content_type=ct, \
                                                        object_id=oi))
                providers = list(Provider.active\
                                    .select_related()\
                                    .filter(user__email__isnull=False, \
                                            access__in=nat_access)\
                                    .values_list('user__email', flat=True))

                rurl = full_url(path=reverse('raw_data', \
                                   kwargs={'entity_code': report.entity.slug, \
                       'period_str': report.period.middle().strftime('%m%Y')}))

                sent, sent_message = send_email(recipients=providers, \
                                        context={'report': report, \
                                                 'report_url': rurl, \
                                                 'url': full_url()},
                                 template='emails/mali_report_available.txt', \
                      title_template='emails/title.mali_report_available.txt')

                # the rest of the method is only for non-national
                return

            # we only send notifications for CSCom & District reports.
            if report.entity.type.slug not in ('cscom', 'district'):
                continue

            # we don't bug district if cscom period is over
            if report.entity.type.slug == 'cscom' \
               and time_cscom_over(period=self.args.period):
                continue

            # we don't bug region if district period is over
            if report.entity.type.slug == 'district' \
               and time_district_over(period=self.args.period):
                continue

            # create a dedicated alert so it can be tracked by report
            # then fire it.
            alert = IndividualMalariaReportCreated.create(report=report)
            if alert.can_trigger():
                alert.trigger()