コード例 #1
0
def run():

    for notice_type in NoticeType.objects.all():
        label = notice_type.label
        print "-" * 72
        print "testing %s..." % label
        messages = get_formatted_messages(FORMATS, label, MOCK_CONTEXT)
        for format in FORMATS:
            print "%s:" % format
            print messages[format.split(".")[0]]
        print
コード例 #2
0
def run():

    for notice_type in NoticeType.objects.all():
        label = notice_type.label
        print "-" * 72
        print "testing %s..." % label
        try:
            messages = get_formatted_messages(FORMATS, label, MOCK_CONTEXT)
        except Exception, e:
            print e
        for format in FORMATS:
            print "%s:" % format
            print messages[format]
        print
コード例 #3
0
def run():

    for notice_type in NoticeType.objects.all():
        label = notice_type.label
        print "-" * 72
        print "testing %s..." % label
        try:
            messages = get_formatted_messages(FORMATS, label, MOCK_CONTEXT)
        except Exception, e:
            print e
        for format in FORMATS:
            print "%s:" % format
            print messages[format]
        print
コード例 #4
0
ファイル: utils.py プロジェクト: wildmercury/akvo-rsr
def send_now(users, label, extra_context=None, on_site=True, sender=None):
    """
    GvH: Modified version of notification.models.send_now

    Creates a new notice.

    This is intended to be how other apps create new notices.

    notification.send(user, 'friends_invite_sent', {
        'spam': 'eggs',
        'foo': 'bar',
    )

    You can pass in on_site=False to prevent the notice emitted from being
    displayed on the site.
    """
    logger.debug("Entering: %s()" % who_am_i())
    if extra_context is None:
        extra_context = {}

    notice_type = NoticeType.objects.get(label=label)

    protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
    current_site = getattr(settings, 'RSR_DOMAIN', 'rsr.akvo.org')

    notices_url = u"%s://%s%s" % (
        protocol,
        unicode(current_site),
        reverse("notification_notices"),
    )

    current_language = get_language()

    formats = (
        'short.txt',
        'full.txt',
        'sms.txt',
        'notice.html',
        'full.html',
    )  # TODO make formats configurable

    for user in users:
        recipients = []
        # get user language for user from language store defined in
        # NOTIFICATION_LANGUAGE_MODULE setting
        try:
            language = get_notification_language(user)
        except LanguageStoreNotAvailable:
            language = None

        if language is not None:
            # activate the user's language
            activate(language)

        # update context with user specific translations
        context = Context({
            "recipient": user,
            "sender": sender,
            "notice": ugettext(notice_type.display),
            "notices_url": notices_url,
            "current_site": current_site,
        })
        context.update(extra_context)

        # get prerendered format messages
        messages = get_formatted_messages(formats, label, context)

        # Strip newlines from subject
        subject = ''.join(
            render_to_string('notification/email_subject.txt', {
                'message': messages['short.txt'],
            }, context).splitlines())

        body = render_to_string('notification/email_body.txt', {
            'message': messages['full.txt'],
        }, context)

        notice = Notice.objects.create(recipient=user,
                                       message=messages['notice.html'],
                                       notice_type=notice_type,
                                       on_site=on_site,
                                       sender=sender)
        if user.is_active:
            if should_send(user, notice_type, "email") and user.email:  # Email
                recipients.append(user.email)
            logger.info("Sending email notification of type %s to %s" % (
                notice_type,
                recipients,
            ))
            # don't send anything in debug/develop mode
            if not getattr(settings, 'SMS_DEBUG', False):
                send_mail(
                    subject, body,
                    getattr(settings, "DEFAULT_FROM_EMAIL",
                            "*****@*****.**"), recipients)
            if should_send(user, notice_type,
                           "sms") and user.phone_number:  # SMS
                # strip newlines
                sms = ''.join(
                    render_to_string('notification/email_subject.txt', {
                        'message': messages['sms.txt'],
                    }, context).splitlines())
                #extra_context['gw_number'] holds a GatewayNumber object
                logger.info(
                    "Sending SMS notification of type %s to %s, mobile phone number: %s."
                    % (
                        notice_type,
                        user,
                        extra_context['phone_number'],
                    ))
                # don't send anything in debug/develop mode
                if not getattr(settings, 'SMS_DEBUG', False):
                    extra_context['gw_number'].send_sms(
                        extra_context['phone_number'], sms)

    # reset environment to original language
    activate(current_language)
    logger.debug("Exiting: %s()" % who_am_i())
コード例 #5
0
ファイル: utils.py プロジェクト: caetie/akvo-rsr
def send_now(users, label, extra_context=None, on_site=True, sender=None):
    """
    GvH: Modified version of notification.models.send_now
    
    Creates a new notice.

    This is intended to be how other apps create new notices.

    notification.send(user, 'friends_invite_sent', {
        'spam': 'eggs',
        'foo': 'bar',
    )
    
    You can pass in on_site=False to prevent the notice emitted from being
    displayed on the site.
    """
    logger.debug("Entering: %s()" % who_am_i())
    if extra_context is None:
        extra_context = {}

    notice_type = NoticeType.objects.get(label=label)

    protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
    current_site = getattr(settings, "DOMAIN_NAME", "www.akvo.org")

    notices_url = u"%s://%s%s" % (protocol, unicode(current_site), reverse("notification_notices"))

    current_language = get_language()

    formats = ("short.txt", "full.txt", "sms.txt", "notice.html", "full.html")  # TODO make formats configurable

    for user in users:
        recipients = []
        # get user language for user from language store defined in
        # NOTIFICATION_LANGUAGE_MODULE setting
        try:
            language = get_notification_language(user)
        except LanguageStoreNotAvailable:
            language = None

        if language is not None:
            # activate the user's language
            activate(language)

        # update context with user specific translations
        context = Context(
            {
                "recipient": user,
                "sender": sender,
                "notice": ugettext(notice_type.display),
                "notices_url": notices_url,
                "current_site": current_site,
            }
        )
        context.update(extra_context)

        # get prerendered format messages
        messages = get_formatted_messages(formats, label, context)

        # Strip newlines from subject
        subject = "".join(
            render_to_string("notification/email_subject.txt", {"message": messages["short.txt"]}, context).splitlines()
        )

        body = render_to_string("notification/email_body.txt", {"message": messages["full.txt"]}, context)

        notice = Notice.objects.create(
            recipient=user, message=messages["notice.html"], notice_type=notice_type, on_site=on_site, sender=sender
        )
        if user.is_active:
            if should_send(user, notice_type, "email") and user.email:  # Email
                recipients.append(user.email)
            logger.info("Sending email notification of type %s to %s" % (notice_type, recipients))
            # don't send anything in debug/develop mode
            if not getattr(settings, "SMS_DEBUG", False):
                send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)

            if should_send(user, notice_type, "sms") and user.get_profile().phone_number:  # SMS
                # strip newlines
                sms = "".join(
                    render_to_string(
                        "notification/email_subject.txt", {"message": messages["sms.txt"]}, context
                    ).splitlines()
                )
                # extra_context['gw_number'] holds a GatewayNumber object
                logger.info(
                    "Sending SMS notification of type %s to %s, mobile phone number: %s."
                    % (notice_type, user, extra_context["phone_number"])
                )
                # don't send anything in debug/develop mode
                if not getattr(settings, "SMS_DEBUG", False):
                    extra_context["gw_number"].send_sms(extra_context["phone_number"], sms)

    # reset environment to original language
    activate(current_language)
    logger.debug("Exiting: %s()" % who_am_i())
コード例 #6
0
def send_now(users,
             label,
             extra_context=None,
             on_site=True,
             no_django_message=False):
    """
    Creates a new notice.

    This is intended to be how other apps create new notices.

    notification.send(user, 'friends_invite_sent', {
        'spam': 'eggs',
        'foo': 'bar',
    )

    You can pass in on_site=False to prevent the notice emitted from being
    displayed on the site.
    """
    if extra_context is None:
        extra_context = {}

    #protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
    #current_site = Site.objects.get_current()

    #notices_url = u"%s://%s%s" % (
    #    protocol,
    #    unicode(current_site),
    #    reverse("notification_notices"),
    #)

    current_language = get_language()

    formats = (
        'short.txt',
        'full.txt',
        'full.html',
    )  # TODO make formats configurable

    for user in users:
        recipients = []
        # get user language for user from language store defined in
        # NOTIFICATION_LANGUAGE_MODULE setting
        try:
            language = get_notification_language(user)
        except LanguageStoreNotAvailable:
            language = None

        if language is not None:
            # activate the user's language
            activate(language)

        # update context with user specific translations
        context = Context({
            "user": user,
            #"notice": ugettext(notice_type.display),
            #"notices_url": notices_url,
            #"current_site": current_site,
        })
        context.update(extra_context)

        # get prerendered format messages
        messages = get_formatted_messages(formats, label, context)

        # Strip newlines from subject
        subject = ''.join(
            render_to_string('notification/email_subject.txt', {
                'message': messages['short.txt'],
            }, context).splitlines())

        body_txt = render_to_string('notification/email_body.txt', {
            'message': messages['full.txt'],
        }, context)

        body_html = render_to_string('notification/email_body.html', {
            'message': messages['full.html'],
        }, context)

        if user.email:  # Email
            recipients.append(user.email)

        profile = getattr(user, 'profile', None)
        site_version = profile.registered_from if profile else None
        send_mail(subject, body_txt, body_html, get_from_email(site_version),
                  recipients)

    # reset environment to original language
    activate(current_language)