def action(self): """ Send every reporter with an action left to do a reminder """ today = date.today() level = self.args.level if not level: return False logger.info(u"Level: %s" % level) if level == 'cscom': message = u"[PNLP] Votre rapport mensuel paludisme est " \ u"attendu au plus tard le %(date)s" \ % {'date': date(today.year, \ today.month, 5).strftime('%x')} for cscom in mobile_entity_gen(cscom_without_report(self.args.period)): contact = contact_for(cscom, recursive=False) if not contact or not contact.phone_number: continue # CSCOM: only cellphone transmiting ones. if not cscom.parent.slug in ('nion', 'maci'): continue logger.info(u"Sending cscom text to %s" % contact.phone_number) send_sms(to=contact.phone_number, text=message) # end of CSCom section return for stat in level_statistics(self.args.period, level).values(): contact = contact_for(stat['entity'], recursive=False) if not contact or not contact.phone_number: continue # nothing waiting validation. if not stat['unval']: continue logger.info(u"Sending %s text to %s" \ % (level, contact.phone_number)) dom = 15 if level == 'district' else 25 message = u"[PNLP] Vous avez %(unval)d rapports a valider " \ u"au plus tard le %(date)s." \ % {'unval': stat['unval'], \ 'date': date(today.year, \ today.month, dom).strftime('%x')} email_title = u"[PNLP] Rapports a valider" # send_sms(to=contact.phone_number, text=message) if not contact.email: continue sent, sent_message = send_email(recipients=contact.email, message=message, title=email_title)
def contact(request): category = 'contact' context = {'category': category} try: web_provider = request.user.get_profile() except: web_provider = None if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): try: dest_mail = [email for s, n, email \ in settings.SUPPORT_CONTACTS \ if s == 'pnlp'][0] except: dest_mail = [] mail_cont = {'provider': web_provider, \ 'name': form.cleaned_data.get('name'), 'email': form.cleaned_data.get('email'), 'phone_number': form.cleaned_data.get('phone_number'), 'subject': form.cleaned_data.get('subject'), 'message': form.cleaned_data.get('message')} sent, sent_message = send_email(recipients=dest_mail, \ context=mail_cont, template='emails/support_request.txt', \ title_template='emails/title.support_request.txt') if sent: messages.success(request, _(u"Support request sent.")) return redirect('support') else: messages.error(request, _(u"Unable to send request. Please " \ "try again later.")) if request.method == 'GET': if web_provider: initial_data = {'name': web_provider.name_access, \ 'email': web_provider.email, \ 'phone_number': web_provider.phone_number} else: initial_data = {} form = ContactForm(initial=initial_data) context.update({'form': form}) return render(request, 'contact.html', context)
def action(self): """ send SMS to district/region for each new report """ report = self.args.report # we have a district/cscom report logger.info(u"Report %s found." % report) contact = contact_for(report.entity.parent) message = u"[PNLP] Le rapport %(receipt)s de %(entity)s " \ u"a ete recu. Vous devez le valider." \ % {'receipt': report.receipt, 'entity': report.entity} # if contact.phone_number: # send_sms(contact.phone_number, message) if contact.email: send_email(recipients=contact.email, message=message, \ title=u"[PNLP] Nouveau rapport recu!") else: send_email(settings.HOTLINE_EMAIL, message, \ u"[PNLP] Unable to send report notification " \ u"to %(contact)s" \ % {'contact': contact.name_access()})
def action(self): """ send SMS to HOTLINE """ message = u"[PNLP] La periode %(period)s va commencer. " \ u"Il faut s'occuper de la surveillance de la collecte " \ u"des donnees primaires." \ % {'period': self.args.period.next()\ .full_name()} send_sms(to=settings.HOTLINE_NUMBER, text=message) # malitel_url = full_url(path=reverse('malitel')) title = u"[PNLP] La période va commencer." # sent, sent_message = send_email(recipients=settings.HOTLINE_EMAIL, \ # template='emails/send_airtime.txt', \ # context={'url': malitel_url}, \ # title=title) sent, sent_message = send_email(recipients=settings.HOTLINE_EMAIL, \ message=message, \ title=title)
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()
def action(self): """ Validate remaining reports and inform region """ # retrieve autobot provider if possible try: author = Provider.active.get(user__username='******') except: author = None if self.args.is_district: validate_level = 'cscom' aggregate_level = 'district' else: validate_level = 'district' aggregate_level = 'region' # validate non-validated reports for report in MalariaReport.unvalidated\ .filter(period=self.args.period, \ entity__type__slug=validate_level): report._status = MalariaReport.STATUS_VALIDATED if author: report.modified_by = author report.modified_on = datetime.now() with reversion.create_revision(): report.save() if author: reversion.set_user(author.user) else: reversion.set_user(get_autobot().user) #report.save() # create aggregated reports for entity in Entity.objects.filter(type__slug=aggregate_level): if entity.pnlp_core_malariareport_reports\ .filter(period=self.args.period).count() > 0: continue rauthor = contact_for(entity) if not author else author logger.info(u"Creating Aggregated report for %s" % entity) report = MalariaReport.create_aggregated(self.args.period, \ entity, rauthor) # region auto-validates their reports if not self.args.is_district: report._status = MalariaReport.STATUS_VALIDATED #report.save() with reversion.create_revision(): report.save() reversion.set_user(rauthor.user) # region-only section # create national report mali = Entity.objects.get(slug='mali') if not self.args.is_district \ and mali.pnlp_core_malariareport_reports\ .filter(period=self.args.period).count() == 0: rauthor = author if author else get_autobot() logger.info(u"Creating National report") report = MalariaReport.create_aggregated(self.args.period, \ mali, rauthor) # following only applies to districts (warn regions). return # district-only section # let region know there are reports to validate for region in Entity.objects.filter(type__slug='region'): contact = contact_for(region, recursive=False) # skip if not recipient if not contact or not contact.phone_number: continue nb_reports = MalariaReport.unvalidated\ .filter(period=self.args.period, \ entity__type__slug='district', \ entity__parent=region).count() message = u"[PNLP] La periode de validation CSRef est " \ u"terminee. Vous avez %(nb)d rapports de " \ u"CSRef a valider." % {'nb': nb_reports} email_title = u"[PNLP] Fin de la periode de validation CSRef" if not contact.email: continue send_email(contact.email, message=message, title=email_title)