예제 #1
0
def fire_email_event(reminder, handler, recipients, verified_numbers,
                     logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)
    email_usage = EmailUsage.get_or_create_usage_record(reminder.domain)
    is_trial = domain_is_on_trial(reminder.domain)

    uses_custom_content_handler, content_handler = get_custom_content_handler(
        handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        _add_recipient_to_template_params(recipient, template_params)
        logged_subevent = logged_event.create_subevent(handler, reminder,
                                                       recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(
                lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(
                lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(
                    MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or '(No Subject)'
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                if is_trial and EmailUsage.get_total_count(
                        reminder.domain) >= TRIAL_MAX_EMAILS:
                    logged_subevent.error(
                        MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
                    continue
                send_mail_async.delay(subject, message,
                                      settings.DEFAULT_FROM_EMAIL,
                                      [email_address])
                email_usage.update_count()
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
예제 #2
0
def submit_feedback(request, domain):
    rating = {
        '1': "Don't Like It / Not Useful",
        '2': "Not Sure / Confusing",
        '3': "Love It / Useful",
    }[request.POST.get('rating')]
    additional_feedback = request.POST.get('additionalFeedback')
    feature_name = request.POST.get('featureName')
    message = '\n'.join([
        '{user} left feedback for "{feature}" on {domain}.',
        '',
        'Rating: {rating}',
        '',
        'Additional Feedback:',
        '{additional_feedback}',
    ]).format(
        user=request.couch_user.username,
        domain=domain,
        feature=feature_name,
        rating=rating,
        additional_feedback=additional_feedback,
    )
    send_mail_async.delay(
        '{} Feedback Received'.format(feature_name),
        message,
        settings.DEFAULT_FROM_EMAIL,
        [settings.FEEDBACK_EMAIL],
    )
    return HttpResponse(json.dumps({
        'success': True,
    }),
                        content_type="application/json")
예제 #3
0
파일: utils.py 프로젝트: ekush/commcare-hq
def send_new_request_update_email(user, requesting_ip, entity_name, entity_type="domain", is_new_user=False, is_confirming=False):
    entity_texts = {"domain": ["project space", "Project"],
                   "org": ["organization", "Organization"]}[entity_type]
    if is_confirming:
        message = "A (basically) brand new user just confirmed his/her account. The %s requested was %s." % (entity_texts[0], entity_name)
    elif is_new_user:
        message = "A brand new user just requested a %s called %s." % (entity_texts[0], entity_name)
    else:
        message = "An existing user just created a new %s called %s." % (entity_texts[0], entity_name)
    message = u"""%s

Details include...

Username: %s
IP Address: %s

You can view the %s here: %s""" % (
        message,
        user.username,
        requesting_ip,
        entity_texts[0],
        get_url_base() + "/%s/%s/" % ("o" if entity_type == "org" else "a", entity_name))
    try:
        recipients = settings.NEW_DOMAIN_RECIPIENTS
        send_mail_async.delay(
            u"New %s: %s" % (entity_texts[0], entity_name),
            message, settings.SERVER_EMAIL, recipients
        )
    except Exception:
        logging.warning("Can't send email, but the message was:\n%s" % message)
예제 #4
0
def send_new_request_update_email(user, requesting_ip, entity_name, entity_type="domain", is_new_user=False, is_confirming=False):
    entity_texts = {"domain": ["project space", "Project"],
                   "org": ["organization", "Organization"]}[entity_type]
    if is_confirming:
        message = "A (basically) brand new user just confirmed his/her account. The %s requested was %s." % (entity_texts[0], entity_name)
    elif is_new_user:
        message = "A brand new user just requested a %s called %s." % (entity_texts[0], entity_name)
    else:
        message = "An existing user just created a new %s called %s." % (entity_texts[0], entity_name)
    message = """%s

Details include...

Username: %s
IP Address: %s

You can view the %s here: %s""" % (
        message,
        user.username,
        requesting_ip,
        entity_texts[0],
        get_url_base() + "/%s/%s/" % ("o" if entity_type == "org" else "a", entity_name))
    try:
        recipients = settings.NEW_DOMAIN_RECIPIENTS
        send_mail_async.delay(
            "New %s: %s" % (entity_texts[0], entity_name),
            message, settings.SERVER_EMAIL, recipients
        )
    except Exception:
        logging.warning("Can't send email, but the message was:\n%s" % message)
예제 #5
0
    def notify_error(self, message, details=None):
        from corehq.motech.views import ConnectionSettingsListView

        if not self.notify_addresses:
            return
        message_lines = [
            message,
            '',
            _('Project space: {}').format(self.domain_name),
            _('Remote API base URL: {}').format(self.base_url),
        ]
        if self.payload_id:
            message_lines.append(_('Payload ID: {}').format(self.payload_id))
        if details:
            message_lines.extend(['', details])
        connection_settings_url = absolute_reverse(
            ConnectionSettingsListView.urlname, args=[self.domain_name])
        message_lines.extend([
            '',
            _('*Why am I getting this email?*'),
            _('This address is configured in CommCare HQ as a notification '
              'address for integration errors.'),
            '',
            _('*How do I unsubscribe?*'),
            _('Open Connection Settings in CommCare HQ ({}) and remove your '
              'email address from the "Addresses to send notifications" field '
              'for remote connections. If necessary, please provide an '
              'alternate address.').format(connection_settings_url),
        ])
        send_mail_async.delay(
            _('MOTECH Error'),
            '\r\n'.join(message_lines),
            from_email=settings.DEFAULT_FROM_EMAIL,
            recipient_list=self.notify_addresses,
        )
예제 #6
0
    def send(self, recipient, logged_event, phone_entry=None):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)
        domain_obj = Domain.get_by_name(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            domain_obj,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            domain_obj,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address], logged_subevent.id)

        email = Email(
            domain=logged_event.domain,
            date=datetime.utcnow(),
            couch_recipient_doc_type=logged_subevent.recipient_type,
            couch_recipient=logged_subevent.recipient_id,
            messaging_subevent_id=logged_subevent.pk,
            recipient_address=email_address,
            subject=subject,
            body=message,
        )
        email.save()

        email_usage.update_count()
예제 #7
0
파일: api.py 프로젝트: kkrampa/commcare-hq
 def send_to_recipients(subject, message):
     send_mail_async.delay(
         # this prefix is automatically added in mail_admins
         # but not send mail
         subject=settings.EMAIL_SUBJECT_PREFIX + subject,
         message=message,
         from_email=settings.DEFAULT_FROM_EMAIL,
         recipient_list=to,
     )
예제 #8
0
 def send_to_recipients(subject, message):
     send_mail_async.delay(
         # this prefix is automatically added in mail_admins
         # but not send mail
         subject=settings.EMAIL_SUBJECT_PREFIX + subject,
         message=message,
         from_email=settings.DEFAULT_FROM_EMAIL,
         recipient_list=to,
     )
예제 #9
0
    def post(self, request, *args, **kwargs):
        if self.internal_settings_form.is_valid():
            old_attrs = copy.copy(self.domain_object.internal)
            old_ucr_permissions = AllowedUCRExpressionSettings.get_allowed_ucr_expressions(
                self.domain)
            self.internal_settings_form.save(self.domain_object)
            log_domain_changes(
                self.request.couch_user.username,
                self.domain,
                self.internal_settings_form.
                cleaned_data['active_ucr_expressions'],
                old_ucr_permissions,
            )
            eula_props_changed = (
                bool(old_attrs.custom_eula) != bool(
                    self.domain_object.internal.custom_eula)
                or bool(old_attrs.can_use_data) != bool(
                    self.domain_object.internal.can_use_data))

            if eula_props_changed and settings.EULA_CHANGE_EMAIL:
                message = '\n'.join([
                    '{user} changed either the EULA or data sharing properties for domain {domain}.',
                    '', 'The properties changed were:',
                    '- Custom eula: {eula_old} --> {eula_new}',
                    '- Can use data: {can_use_data_old} --> {can_use_data_new}'
                ]).format(
                    user=self.request.couch_user.username,
                    domain=self.domain,
                    eula_old=old_attrs.custom_eula,
                    eula_new=self.domain_object.internal.custom_eula,
                    can_use_data_old=old_attrs.can_use_data,
                    can_use_data_new=self.domain_object.internal.can_use_data,
                )
                send_mail_async.delay(
                    'Custom EULA or data use flags changed for {}'.format(
                        self.domain), message, settings.DEFAULT_FROM_EMAIL,
                    [settings.EULA_CHANGE_EMAIL])

            messages.success(
                request,
                _("The internal information for project %s was successfully updated!"
                  ) % self.domain)
            if self.internal_settings_form.cleaned_data['send_handoff_email']:
                self.send_handoff_email()
            return redirect(self.urlname, self.domain)
        else:
            messages.error(
                request,
                _("Your settings are not valid, see below for errors. Correct them and try again!"
                  ))
            return self.get(request, *args, **kwargs)
예제 #10
0
def fire_email_event(reminder, handler, recipients, verified_numbers,
                     logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)

    uses_custom_content_handler, content_handler = get_custom_content_handler(
        handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        logged_subevent = logged_event.create_subevent(handler, reminder,
                                                       recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(
                lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(
                lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(
                    MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or '(No Subject)'
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                send_mail_async.delay(subject, message,
                                      settings.DEFAULT_FROM_EMAIL,
                                      [email_address])
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
예제 #11
0
def _notify_ignored_form_submission(request, user_id):
    message = """
        Details:
        Method: {}
        URL: {}
        GET Params: {}
        User ID: {}
    """.format(request.method, request.get_raw_uri(), json.dumps(request.GET), user_id)
    send_mail_async.delay(
        "[%s] Unexpected practice mobile user submission received" % settings.SERVER_ENVIRONMENT,
        message,
        settings.DEFAULT_FROM_EMAIL,
        ['*****@*****.**']
    )
예제 #12
0
def fire_email_event(reminder, handler, recipients, verified_numbers, logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)
    email_usage = EmailUsage.get_or_create_usage_record(reminder.domain)
    is_trial = domain_is_on_trial(reminder.domain)

    uses_custom_content_handler, content_handler = get_custom_content_handler(handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        logged_subevent = logged_event.create_subevent(handler, reminder, recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or '(No Subject)'
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                if is_trial and EmailUsage.get_total_count(reminder.domain) >= TRIAL_MAX_EMAILS:
                    logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
                    continue
                send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
                email_usage.update_count()
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
예제 #13
0
 def post(self, request, domain, *args, **kwargs):
     self.domain_object.requested_report_builder_subscription.append(
         request.user.username)
     self.domain_object.save()
     send_mail_async.delay(
         "Report Builder Subscription Request: {}".format(domain),
         "User {} in the {} domain has requested a report builder subscription."
         " Current subscription is '{}'.".format(request.user.username,
                                                 domain, self.plan_name),
         settings.DEFAULT_FROM_EMAIL,
         [settings.REPORT_BUILDER_ADD_ON_EMAIL],
     )
     update_hubspot_properties.delay(
         request.couch_user, {'report_builder_subscription_request': 'yes'})
     return self.get(request, domain, *args, **kwargs)
예제 #14
0
 def post(self, request, domain, *args, **kwargs):
     self.domain_object.requested_report_builder_subscription.append(request.user.username)
     self.domain_object.save()
     send_mail_async.delay(
         "Report Builder Subscription Request: {}".format(domain),
         "User {} in the {} domain has requested a report builder subscription."
         " Current subscription is '{}'.".format(
             request.user.username,
             domain,
             self.plan_name
         ),
         settings.DEFAULT_FROM_EMAIL,
         [settings.REPORT_BUILDER_ADD_ON_EMAIL],
     )
     return self.get(request, domain, *args, **kwargs)
예제 #15
0
def update_calculated_properties():
    success = False
    try:
        _update_calculated_properties()
        success = True
    except Exception:
        notify_exception(
            None,
            message="update_calculated_properties task has errored",
        )
    send_mail_async.delay(
        subject="Calculated properties report task was " +
        ("successful" if success else "unsuccessful"),
        message="Sentry will have relevant exception in case of failure",
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=["{}@{}.com".format("dmore", "dimagi")])
예제 #16
0
파일: tasks.py 프로젝트: isaace/commcare-hq
def push_missing_docs_to_es():
    if settings.SERVER_ENVIRONMENT not in settings.ICDS_ENVS:
        return

    current_date = date.today() - timedelta(weeks=12)
    interval = timedelta(days=1)
    case_doc_type = 'CommCareCase'
    xform_doc_type = 'XFormInstance'
    doc_differences = dict()
    while current_date <= date.today() + interval:
        end_date = current_date + interval
        primary_xforms = get_primary_db_form_counts('icds-cas', current_date,
                                                    end_date).get(
                                                        xform_doc_type, -1)
        es_xforms = get_es_counts_by_doc_type(
            'icds-cas', (FormES, ),
            (submitted(gte=current_date, lt=end_date), )).get(
                xform_doc_type.lower(), -2)
        if primary_xforms != es_xforms:
            doc_differences[(current_date,
                             xform_doc_type)] = primary_xforms - es_xforms
            resave_documents.delay(xform_doc_type, current_date, end_date)

        primary_cases = get_primary_db_case_counts('icds-cas', current_date,
                                                   end_date).get(
                                                       case_doc_type, -1)
        es_cases = get_es_counts_by_doc_type(
            'icds-cas', (CaseES, ),
            (server_modified_range(gte=current_date, lt=end_date), )).get(
                case_doc_type, -2)
        if primary_cases != es_cases:
            doc_differences[(current_date,
                             case_doc_type)] = primary_xforms - es_xforms
            resave_documents.delay(case_doc_type, current_date, end_date)

        current_date += interval

    if doc_differences:
        message = "\n".join([
            "{}, {}: {}".format(k[0], k[1], v)
            for k, v in doc_differences.items()
        ])
        send_mail_async.delay(
            subject="Results from push_missing_docs_to_es",
            message=message,
            from_email=settings.DEFAULT_FROM_EMAIL,
            recipient_list=["{}@{}.com".format("jmoney", "dimagi")])
예제 #17
0
 def notify_error(self, message, details=None):
     if not self.notify_addresses:
         return
     message_body = '\r\n'.join((
         message,
         f'Project space: {self.domain_name}',
         f'Remote API base URL: {self.base_url}',
         f'Remote API username: {self.username}',
     ))
     if details:
         message_body += f'\r\n\r\n{details}'
     send_mail_async.delay(
         'MOTECH Error',
         message_body,
         from_email=settings.DEFAULT_FROM_EMAIL,
         recipient_list=self.notify_addresses,
     )
예제 #18
0
def export_all_rows_task(ReportClass,
                         report_state,
                         recipient_list=None,
                         subject=None):
    from corehq.apps.reports.standard.deployments import ApplicationStatusReport
    report = object.__new__(ReportClass)
    report.__setstate__(report_state)
    report.rendered_as = 'export'

    setattr(report.request, 'REQUEST', {})
    report_start = datetime.utcnow()
    file = report.excel_response
    report_class = report.__class__.__module__ + '.' + report.__class__.__name__

    if report.domain is None:
        # Some HQ-wide reports (e.g. accounting/smsbillables) will not have a domain associated with them
        # This uses the user's first domain to store the file in the blobdb
        report.domain = report.request.couch_user.get_domains()[0]

    hash_id = _store_excel_in_blobdb(report_class, file, report.domain)
    if not recipient_list:
        recipient_list = [report.request.couch_user.get_email()]
    for recipient in recipient_list:
        _send_email(report.request.couch_user,
                    report,
                    hash_id,
                    recipient=recipient,
                    subject=subject)

    report_end = datetime.utcnow()
    if settings.SERVER_ENVIRONMENT == 'icds' and isinstance(
            report, ApplicationStatusReport):
        message = """
            Duration: {dur},
            Total rows: {total},
            Warehouse Enabled: {enabled},
            User List: {users}
            """.format(dur=report_end - report_start,
                       total=report.total_records,
                       enabled=report.warehouse,
                       users=recipient_list)
        send_mail_async.delay(
            subject="ApplicationStatusReport Download metrics",
            message=message,
            from_email=settings.DEFAULT_FROM_EMAIL,
            recipient_list=["{}@{}.com".format("sreddy", "dimagi")])
예제 #19
0
def _notify_dimagi_users_on_domain(domain):
    from corehq.apps.users.models import WebUser
    from corehq.apps.hqwebapp.tasks import send_mail_async
    recipients = [
        user.get_email() for user in WebUser.by_domain(domain)
        if user.is_dimagi
    ]

    subject = 'CommCare HQ project migrated to the scale backend.'.format(
        domain)
    message = """
    The CommCare HQ project "{}" has been migrated to the scale backend.

    You should not notice anything different but if you do please report a bug.
    """.format(domain)
    send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL,
                          recipients)
예제 #20
0
    def send(self, recipient, logged_event):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            logged_event.domain,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            logged_event.domain,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
        email_usage.update_count()
        logged_subevent.completed()
예제 #21
0
    def send(self, recipient, logged_event, phone_entry=None):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            logged_event.domain,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            logged_event.domain,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
        email_usage.update_count()
        logged_subevent.completed()
예제 #22
0
def notify_repeater_admins(repeater):
    msg = (f'Forwarding data to {repeater} has consistently failed, '
           'and has been paused.')
    send_mail_async.delay(
        f'Repeater threshold exceeded on domain {repeater.domain}', msg,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=[MOTECH_DEV],
    )
    if repeater.notify_addresses:
        recipient_list = repeater.notify_addresses
    else:
        subs = Subscription.get_active_subscription_by_domain(repeater.domain)
        recipient_list = subs.account.billingcontactinfo.email_list
    send_mail_async.delay(
        'Data forwarding paused', msg,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=recipient_list,
    )
예제 #23
0
def _notify_dimagi_users_on_domain(domain):
    from corehq.apps.users.models import WebUser
    from corehq.apps.hqwebapp.tasks import send_mail_async
    recipients = [
        user.get_email() for user in WebUser.by_domain(domain) if user.is_dimagi
    ]

    subject = 'CommCare HQ project migrated to the scale backend.'.format(domain)
    message = """
    The CommCare HQ project "{}" has been migrated to the scale backend.

    You should not notice anything different but if you do please report a bug.
    """.format(domain)
    send_mail_async.delay(
        subject,
        message,
        settings.DEFAULT_FROM_EMAIL,
        recipients
    )
예제 #24
0
def fire_email_event(reminder, handler, recipients, verified_numbers, logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)

    uses_custom_content_handler, content_handler = get_custom_content_handler(handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        logged_subevent = logged_event.create_subevent(handler, reminder, recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or "(No Subject)"
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
예제 #25
0
 def notify_error(self, message, details=None):
     if not self.notify_addresses:
         return
     message_lines = [
         message,
         f'Project space: {self.domain_name}',
         f'Remote API base URL: {self.base_url}',
         f'Remote API username: {self.username}',
     ]
     if self.payload_id:
         message_lines.append(f'Payload ID: {self.payload_id}')
     if details:
         message_lines.extend(['', '', details])
     send_mail_async.delay(
         'MOTECH Error',
         '\r\n'.join(message_lines),
         from_email=settings.DEFAULT_FROM_EMAIL,
         recipient_list=self.notify_addresses,
     )
예제 #26
0
def push_missing_docs_to_es():
    if settings.SERVER_ENVIRONMENT not in settings.ICDS_ENVS:
        return

    current_date = date.today() - timedelta(weeks=12)
    interval = timedelta(days=1)
    case_doc_type = 'CommCareCase'
    xform_doc_type = 'XFormInstance'
    doc_differences = dict()
    while current_date <= date.today() + interval:
        end_date = current_date + interval
        primary_xforms = get_primary_db_form_counts(
            'icds-cas', current_date, end_date
        ).get(xform_doc_type, -1)
        es_xforms = get_es_counts_by_doc_type(
            'icds-cas', (FormES,), (submitted(gte=current_date, lt=end_date),)
        ).get(xform_doc_type.lower(), -2)
        if primary_xforms != es_xforms:
            doc_differences[(current_date, xform_doc_type)] = primary_xforms - es_xforms

        primary_cases = get_primary_db_case_counts(
            'icds-cas', current_date, end_date
        ).get(case_doc_type, -1)
        es_cases = get_es_counts_by_doc_type(
            'icds-cas', (CaseES,), (server_modified_range(gte=current_date, lt=end_date),)
        ).get(case_doc_type, -2)
        if primary_cases != es_cases:
            doc_differences[(current_date, case_doc_type)] = primary_xforms - es_xforms

        current_date += interval

    if doc_differences:
        message = "\n".join([
            "{}, {}: {}".format(k[0], k[1], v)
            for k, v in doc_differences.items()
        ])
        send_mail_async.delay(
            subject="Results from push_missing_docs_to_es",
            message=message,
            from_email=settings.DEFAULT_FROM_EMAIL,
            recipient_list=["{}@{}.com".format("jmoney", "dimagi")]
        )
예제 #27
0
    def post(self, request, *args, **kwargs):
        if self.internal_settings_form.is_valid():
            old_attrs = copy.copy(self.domain_object.internal)
            self.internal_settings_form.save(self.domain_object)
            eula_props_changed = (bool(old_attrs.custom_eula) != bool(self.domain_object.internal.custom_eula) or
                                  bool(old_attrs.can_use_data) != bool(self.domain_object.internal.can_use_data))

            if eula_props_changed and settings.EULA_CHANGE_EMAIL:
                message = '\n'.join([
                    '{user} changed either the EULA or data sharing properties for domain {domain}.',
                    '',
                    'The properties changed were:',
                    '- Custom eula: {eula_old} --> {eula_new}',
                    '- Can use data: {can_use_data_old} --> {can_use_data_new}'
                ]).format(
                    user=self.request.couch_user.username,
                    domain=self.domain,
                    eula_old=old_attrs.custom_eula,
                    eula_new=self.domain_object.internal.custom_eula,
                    can_use_data_old=old_attrs.can_use_data,
                    can_use_data_new=self.domain_object.internal.can_use_data,
                )
                send_mail_async.delay(
                    'Custom EULA or data use flags changed for {}'.format(self.domain),
                    message, settings.DEFAULT_FROM_EMAIL, [settings.EULA_CHANGE_EMAIL]
                )

            messages.success(request,
                             _("The internal information for project %s was successfully updated!") % self.domain)
            if self.internal_settings_form.cleaned_data['send_handoff_email']:
                self.send_handoff_email()
            return redirect(self.urlname, self.domain)
        else:
            messages.error(request, _(
                "Your settings are not valid, see below for errors. Correct them and try again!"))
            return self.get(request, *args, **kwargs)
예제 #28
0
def send_email_to_dev_more(domain, user, query, total_results):
    """Dev wanted an email with every query that is performed on the CLE.

    ¯\_(ツ)_/¯
    """

    message = """
    Hi Dev! Someone just performed a query with the case list explorer. Cool!

    Domain: {}
    User: {}
    Query: {}
    Total Results: {}

    Yours truly,
    CLEBOT
    """.format(domain, user, query if query else "Empty Query", total_results)

    send_mail_async.delay(
        subject="Case List Explorer Query Performed",
        message=message,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=["@".join(['dmore', 'dimagi.com'])],
    )
예제 #29
0
def send_email_to_dev_more(domain, user, query, total_results):
    """Dev wanted an email with every query that is performed on the CLE.

    ¯\_(ツ)_/¯
    """

    message = """
    Hi Dev! Someone just performed a query with the case list explorer. Cool!

    Domain: {}
    User: {}
    Query: {}
    Total Results: {}

    Yours truly,
    CLEBOT
    """.format(domain, user, query if query else "Empty Query", total_results)

    send_mail_async.delay(
        subject="Case List Explorer Query Performed",
        message=message,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=["@".join(['dmore', 'dimagi.com'])],
    )
예제 #30
0
def push_models(master_domain, models, linked_domains, build_apps, username):
    domain_links_by_linked_domain = {
        link.linked_domain: link
        for link in get_linked_domains(master_domain)
    }
    user = CouchUser.get_by_username(username)
    errors_by_domain = defaultdict(list)
    successes_by_domain = defaultdict(list)
    for linked_domain in linked_domains:
        if linked_domain not in domain_links_by_linked_domain:
            errors_by_domain[linked_domain].append(
                _("Project space {} is no longer linked to {}. No content "
                  "was released to it.").format(master_domain, linked_domain))
            continue
        domain_link = domain_links_by_linked_domain[linked_domain]
        for model in models:
            try:
                found = False
                updated_app = False
                built_app = False
                if model['type'] == MODEL_APP:
                    app_id = model['detail']['app_id']
                    for linked_app in get_apps_in_domain(linked_domain,
                                                         include_remote=False):
                        if is_linked_app(
                                linked_app) and linked_app.family_id == app_id:
                            found = True
                            if toggles.MULTI_MASTER_LINKED_DOMAINS.enabled(
                                    linked_domain):
                                errors_by_domain[linked_domain].append(
                                    textwrap.dedent(
                                        _("""
                                    Could not update {} because multi master flag is in use
                                """.strip()).format(model['name'])))
                                continue
                            app = update_linked_app(linked_app, app_id,
                                                    user.user_id)
                            updated_app = True
                            if build_apps:
                                build = app.make_build()
                                build.is_released = True
                                build.save(increment_version=False)
                                built_app = True
                elif model['type'] == MODEL_REPORT:
                    report_id = model['detail']['report_id']
                    for linked_report in get_report_configs_for_domain(
                            linked_domain):
                        if linked_report.report_meta.master_id == report_id:
                            found = True
                            update_linked_ucr(domain_link,
                                              linked_report.get_id)
                elif (model['type'] == MODEL_CASE_SEARCH and
                      not toggles.SYNC_SEARCH_CASE_CLAIM.enabled(linked_domain)
                      ):
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Could not update {} because case claim flag is not on
                    """.strip()).format(model['name'])))
                    continue
                else:
                    found = True
                    update_model_type(domain_link,
                                      model['type'],
                                      model_detail=model['detail'])
                    domain_link.update_last_pull(model['type'],
                                                 user._id,
                                                 model_details=model['detail'])
                if found:
                    successes_by_domain[linked_domain].append(
                        _("{} was updated").format(model['name']))
                else:
                    errors_by_domain[linked_domain].append(
                        _("Could not find {}").format(model['name']))
            except Exception as e:  # intentionally broad
                if model[
                        'type'] == MODEL_APP and updated_app and build_apps and not built_app:
                    # Updating an app can be a 2-step process, make it clear which one failed
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Updated {} but could not make and release build: {}
                    """.strip()).format(model['name'], str(e))))
                else:
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Could not update {}: {}
                    """.strip()).format(model['name'], str(e))))
                notify_exception(
                    None, "Exception pushing linked domains: {}".format(e))

    subject = _("Linked project release complete.")
    if errors_by_domain:
        subject += _(" Errors occurred.")

    error_domain_count = len(errors_by_domain)
    success_domain_count = len(linked_domains) - error_domain_count
    message = _("""
Release complete. {} project(s) succeeded. {}

The following content was released:
{}

The following linked project spaces received content:
    """).format(
        success_domain_count,
        _("{} project(s) encountered errors.").format(error_domain_count)
        if error_domain_count else "",
        "\n".join(["- " + m['name'] for m in models]))
    for linked_domain in linked_domains:
        if linked_domain not in errors_by_domain:
            message += _("\n- {} updated successfully").format(linked_domain)
        else:
            message += _("\n- {} encountered errors:").format(linked_domain)
            for msg in errors_by_domain[linked_domain] + successes_by_domain[
                    linked_domain]:
                message += "\n   - " + msg
    send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL,
                          [user.email or user.username])