def send_notification(cls, account, account_type_slug, **kwargs): """Send account creation email to account user""" account_type = get_account_type(account_type_slug) notification_data = { "account": account, "login": account.mt4_id, "group": account_type } kwargs.pop("group", None) notification_data.update(kwargs) # Email activation link notification_data['activation_url'] = cls.activation_url(account.user) recipients = [account.user] if getattr(cls, "notification_name", None): notification_name = cls.notification_name else: # HACK until WE HAZ ENGLISH notification_name = account_type.notification_name if get_notification_language(account.user) == "ru" \ else "account_created" cls.get_notification().send(recipients, notification_name, notification_data, no_django_message=True)
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())
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())
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)
def send_notifications(users, label, extra_context=None, on_site=True, xmppresources=None, **etckwargs): ''' ... ''' if not notification: return import models remaining_users = [] logging.debug("users: %r" % users) # ! Note: queueing is disregarded for XMPP notifications. # !!! Most of this is copied from send_now if extra_context is None: extra_context = {} if xmppresources is None: xmppresources = {} notice_type = NoticeType.objects.get(label=label) current_site = Site.objects.get_current() notices_url = u"http://%s%s" % ( unicode(current_site), reverse("notification_notices"), ) current_language = get_language() # Probably can do ucontacts = ... filter(remote__in=userjids) for user in users: jid = get_user_jid(user) # None if no JID set. # If user has authenticated some bot. ucontacts = models.XMPPContact.objects.filter(remote=jid, auth_to=True) # Also, we don't care about user's status now. if not (jid and ucontacts): # Not jid/contact available. Leave him to e-mail notification. if user.email: remaining_users.append(user) else: logging.debug("No way to contact %r." % user) # ! XXX: Add user messageset? # Or to on-site. # ! Will on-site notifications work with that? continue # ! What if it's not XMPP-related notification at all? # srcjid = "*****@*****.**" srcjid = ucontacts[0].local # Choose first available authenticated bot JID. if user in xmppresources: srcjid += "/%s"%xmppresources[user] # ! Could be better to send status not every time. But not sure # how to find out if have to re-send it. send_xmpp_message(XmppPresence(src=srcjid, dst=jid, show='chat', status='Here here!', priority="0")) # 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) body = django.template.loader.render_to_string( 'notification/%s/xmpp.html'%label, context_instance=context) # ?.. #message = django.template.loader.render_to_string( # 'notification/%s/notice.html'%label, context_instance=context) #notice = Notice.objects.create(user=user, message=message, # notice_type=notice_type, on_site=on_site) noticemsg = XmppResponse(body, src=srcjid, dst=jid, user=user) send_xmpp_message(noticemsg) # reset environment to original language activate(current_language) logging.debug("remaining users: %r" % remaining_users) # Send remaining (non-XMPP) notifications. notification_send_orig(remaining_users, label, extra_context, on_site, **etckwargs)