def test_uselocale(self): """Test that uselocale does what it says on the tin.""" with uselocale('en-US'): eq_(get_language(), 'en-us') with uselocale('de'): eq_(get_language(), 'de') with uselocale('fr'): eq_(get_language(), 'fr')
def test_mocked_gettext(self): """I'm not entirely sure about the mocking, so test that.""" # Import tower now so it is affected by the mock. from tower import ugettext as _ with uselocale('en-US'): eq_(_('Hello'), 'Hello') with uselocale('fr'): eq_(_('Hello'), 'Bonjour') with uselocale('es'): eq_(_('Hello'), 'Hola')
def parse(self, text, show_toc=None, tags=None, attributes=None, styles=None, locale=settings.WIKI_DEFAULT_LANGUAGE, nofollow=False): """Given wiki markup, return HTML. Pass a locale to get all the hooks to look up Documents or Media (Video, Image) for that locale. We key Documents by title and locale, so both are required to identify it for a e.g. link. Since py-wikimarkup's hooks don't offer custom paramters for callbacks, we're using self.locale to keep things simple. """ self.locale = locale parser_kwargs = {'tags': tags} if tags else {} with email_utils.uselocale(locale): return super(WikiParser, self).parse( text, show_toc=show_toc, attributes=attributes or ALLOWED_ATTRIBUTES, styles=styles or ALLOWED_STYLES, nofollow=nofollow, strip_comments=True, **parser_kwargs)
def _send_email(self, confirmation_profile, url, subject, email_template, send_to, **kwargs): """ Send an email using a passed in confirmation profile. Use specified url, subject, email_template, and email to send_to. """ current_site = Site.objects.get_current() email_kwargs = {'activation_key': confirmation_profile.activation_key, 'domain': current_site.domain, 'activate_url': url, 'login_url': reverse('users.login')} email_kwargs.update(kwargs) # RegistrationProfile doesn't have a locale attribute. So if # we get one of those, then we have to get the real profile # from the user. if hasattr(confirmation_profile, 'locale'): locale = confirmation_profile.locale else: locale = confirmation_profile.user.profile.locale with email_utils.uselocale(locale): message = email_utils.render_email(email_template, email_kwargs) mail.send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [send_to])
def notification_mails(revision, subject, template, url, users_and_watches): """Return EmailMessages in the KB's standard notification mail format.""" document = revision.document for u, w in users_and_watches: if hasattr(u, 'profile'): locale = u.profile.locale else: locale = document.locale with email_utils.uselocale(locale): c = context_dict(revision) # TODO: Expose all watches c['watch'] = w[0] c['url'] = url subject = subject.format(title=document.title, creator=revision.creator, locale=document.locale) msg = email_utils.render_email(template, c) yield EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [u.email])
def _mails(self, users_and_watches): """Send readiness mails. """ revision = self.revision document = revision.document log.debug('Sending ready notifications for revision (id=%s)' % revision.id) for user, watches in users_and_watches: if hasattr(user, 'profile'): locale = user.profile.locale else: locale = document.locale with email_utils.uselocale(locale): subject = _(u'{title} has a revision ready for ' 'localization').format(title=document.title) template = 'wiki/email/ready_for_l10n.ltxt' c = context_dict(revision, ready_for_l10n=True) # TODO: Expose all watches c['watch'] = watches[0] c['url'] = django_reverse('wiki.select_locale', args=[document.slug]) msg = email_utils.render_email(template, c) yield EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])
def _mails(self, users_and_watches): question = self.instance # Cache solution.question as a workaround for replication lag # (bug 585029) question.solution = self.answer question.solution.question = question c = {'answerer': question.solution.creator, 'asker': question.creator.username, 'question_title': question.title, 'host': Site.objects.get_current().domain, 'solution_url': question.solution.get_absolute_url()} for u, w in users_and_watches: c['username'] = u.username # '' if anonymous c['watch'] = w[0] # TODO: Expose all watches. # u here can be a Django User model or a Tidings EmailUser # model. In the case of the latter, there is no associated # profile, so we set the locale to en-US. if hasattr(u, 'profile'): locale = u.profile.locale else: locale = 'en-US' with email_utils.uselocale(locale): subject = _(u'Solution found to Firefox Help question') content = email_utils.render_email( 'questions/email/solution.ltxt', c) yield EmailMessage( subject, content, settings.TIDINGS_FROM_ADDRESS, [u.email])
def send_group_email(announcement_id): """Build and send the announcement emails to a group.""" try: announcement = Announcement.objects.get(pk=announcement_id) except Announcement.DoesNotExist: return connection = get_connection(fail_silently=True) connection.open() group = announcement.group users = User.objects.filter(groups__in=[group]) plain_content = bleach.clean(announcement.content_parsed, tags=[], strip=True).strip() email_kwargs = {'content': plain_content, 'domain': Site.objects.get_current().domain} template = 'announcements/email/announcement.ltxt' try: for u in users: # Localize email each time. with uselocale(u.profile.locale or settings.LANGUAGE_CODE): subject = _('New announcement for {group}').format( group=group.name) msg = render_email(template, email_kwargs) m = EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [u.email]) connection.send_messages([m]) finally: connection.close()
def email_private_message(inbox_message_id): """Send notification of a new private message.""" inbox_message = InboxMessage.objects.get(id=inbox_message_id) log.debug('Sending email for user (%s)' % (inbox_message.to,)) user = inbox_message.to if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE with uselocale(locale): subject = _(u'[SUMO] You have a new private message from [{sender}]') subject = subject.format(sender=inbox_message.sender.username) context = { 'sender': inbox_message.sender.username, 'message': inbox_message.message, 'url': reverse('messages.read', kwargs={'msgid': inbox_message.id}), 'unsubscribe_url': reverse('users.edit_settings'), 'host': Site.objects.get_current().domain} template = 'messages/email/private_message.ltxt' msg = render_email(template, context) send_mail(subject, msg, settings.TIDINGS_FROM_ADDRESS, [inbox_message.to.email])
def send_contributor_notification(based_on, revision, document, message): """Send notification of review to the contributors of revisions.""" template = 'wiki/email/reviewed_contributors.ltxt' url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'url': url, 'host': Site.objects.get_current().domain} msgs = [] for r in based_on: # Send email to all contributors except the reviewer and the creator # of the approved revision. if r.creator in [revision.creator, revision.reviewer]: continue user = r.creator if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE with email_utils.uselocale(locale): if revision.is_approved: subject = _(u'A revision you contributed to has ' 'been approved: {title}') else: subject = _(u'A revision you contributed to has ' 'been reviewed: {title}') subject = subject.format(title=document.title) msg = email_utils.render_email(template, c) msgs.append(EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])) email_utils.send_messages(msgs)
def _mails(self, users_and_watches): """Send one kind of mail to the asker and another to other watchers.""" # Cache answer.question, similar to caching solution.question below. self.answer.question = self.instance asker_id = self.answer.question.creator.id c = {'answer': self.answer.content, 'answerer': self.answer.creator.username, 'question_title': self.instance.title, 'host': Site.objects.get_current().domain, 'answer_url': self.answer.get_absolute_url()} for u, w in users_and_watches: c['helpful_url'] = self.answer.get_helpful_answer_url() c['solution_url'] = self.answer.get_solution_url(watch=w[0]) c['username'] = u.username c['watch'] = w[0] # TODO: Expose all watches. # u here can be a Django User model or a Tidings EmailUser # model. In the case of the latter, there is no associated # profile, so we set the locale to en-US. if hasattr(u, 'profile'): locale = u.profile.locale else: locale = 'en-US' with email_utils.uselocale(locale): is_asker = asker_id == u.id if is_asker: subject = _(u'%s posted an answer to your question "%s"' % (self.answer.creator.username, self.instance.title)) template = 'questions/email/new_answer_to_asker.ltxt' else: subject = _(u'%s commented on a Firefox question ' "you're watching" % self.answer.creator.username) template = 'questions/email/new_answer.ltxt' msg = email_utils.render_email(template, c) yield EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [u.email])
def send_reviewed_notification(revision, document, message): """Send notification of review to the revision creator.""" if revision.reviewer == revision.creator: log.debug('Revision (id=%s) reviewed by creator, skipping email' % revision.id) return log.debug('Sending reviewed email for revision (id=%s)' % revision.id) url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'url': url, 'host': Site.objects.get_current().domain} msgs = [] for user in [revision.creator, revision.reviewer]: if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE with email_utils.uselocale(locale): if revision.is_approved: subject = _(u'Your revision has been approved: {title}') else: subject = _(u'Your revision has been reviewed: {title}') subject = subject.format(title=document.title) template = 'wiki/email/reviewed.ltxt' msg = email_utils.render_email(template, c) msgs.append(EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])) email_utils.send_messages(msgs)
def _activation_email(cls, watch, email): """Return an EmailMessage containing the activation URL to be sent to a new watcher. """ # If the watch has an associated user, use that # locale. Otherwise it's an anonymous watch and we don't know # what locale they want, so we give them en-US. if watch.user: locale = watch.user.profile.locale else: locale = 'en-US' with email_utils.uselocale(locale): subject = _('Please confirm your email address') email_kwargs = {'activation_url': cls._activation_url(watch), 'domain': Site.objects.get_current().domain, 'watch_description': cls.description_of_watch(watch)} template = 'questions/email/activate_watch.ltxt' message = email_utils.render_email(template, email_kwargs) return EmailMessage(subject, message, settings.TIDINGS_FROM_ADDRESS, [email])
def _mails(self, users_and_watches): """Send approval or readiness mails, as appropriate. If a given user is watching the Ready event and the revision is in fact ready, say so. Otherwise, just send the Approval email. """ revision = self._revision document = revision.document is_ready = revision.is_ready_for_localization log.debug('Sending approved/ready notifications for revision (id=%s)' % revision.id) for user, watches in users_and_watches: # Figure out the locale to use for l10n. if hasattr(user, 'profile'): locale = user.profile.locale else: locale = document.locale # Localize the subject and message with the appropriate # context. with email_utils.uselocale(locale): if (is_ready and ReadyRevisionEvent.event_type in (w.event_type for w in watches)): c = context_dict(revision, ready_for_l10n=True) # TODO: Expose all watches c['watch'] = watches[0] c['url'] = django_reverse('wiki.select_locale', args=[document.slug]) subject = _(u'{title} has a revision ready for ' 'localization') template = 'wiki/email/ready_for_l10n.ltxt' else: c = context_dict(revision, revision_approved=True) approved_url = reverse('wiki.document', locale=document.locale, args=[document.slug]) c['url'] = approved_url # TODO: Expose all watches. c['watch'] = watches[0] c['reviewer'] = revision.reviewer.username subject = _(u'{title} ({locale}) has a new approved ' 'revision ({reviewer})') template = 'wiki/email/approved.ltxt' subject = subject.format( title=document.title, reviewer=revision.reviewer.username, locale=document.locale) msg = email_utils.render_email(template, c) yield EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])