def log_and_notify(action, comments, note_creator, version): log_kwargs = { 'user': note_creator, 'created': datetime.datetime.now(), } if comments: log_kwargs['details'] = { 'comments': comments, 'version': version.version} else: # Just use the name of the action if no comments provided. Alas we # can't know the locale of recipient, and our templates are English # only so prevent language jumble by forcing into en-US. with no_translation(): comments = '%s' % action.short note = amo.log(action, version.addon, version, **log_kwargs) # Collect reviewers involved with this version. review_perm = ('Review' if version.channel == amo.RELEASE_CHANNEL_LISTED else 'ReviewUnlisted') log_users = { alog.user for alog in ActivityLog.objects.for_version(version) if acl.action_allowed_user(alog.user, 'Addons', review_perm)} # Collect add-on authors (excl. the person who sent the email.) addon_authors = set(version.addon.authors.all()) - {note_creator} # Collect staff that want a copy of the email staff_cc = set( UserProfile.objects.filter(groups__name=ACTIVITY_MAIL_GROUP)) # If task_user doesn't exist that's no big issue (i.e. in tests) try: task_user = {get_task_user()} except UserProfile.DoesNotExist: task_user = set() # Collect reviewers on the thread (excl. the email sender and task user for # automated messages). reviewers = ((log_users | staff_cc) - addon_authors - task_user - {note_creator}) author_context_dict = { 'name': version.addon.name, 'number': version.version, 'author': note_creator.name, 'comments': comments, 'url': absolutify(version.addon.get_dev_url('versions')), 'SITE_URL': settings.SITE_URL, } reviewer_context_dict = author_context_dict.copy() reviewer_context_dict['url'] = absolutify( reverse('editors.review', args=[version.addon.pk], add_prefix=False)) # Not being localised because we don't know the recipients locale. subject = 'Mozilla Add-ons: %s Updated' % version.addon.name template = loader.get_template('activity/emails/developer.txt') send_activity_mail( subject, template.render(Context(author_context_dict)), version, addon_authors, settings.EDITORS_EMAIL) send_activity_mail( subject, template.render(Context(reviewer_context_dict)), version, reviewers, settings.EDITORS_EMAIL) return note
def test_no_translation(): """ `no_translation` provides a context where only the default language is active. """ old_lang = translation.get_language() try: translation.activate('pt-br') with no_translation(): assert (translation.get_language().lower() == settings.LANGUAGE_CODE.lower()) assert translation.get_language() == 'pt-br' with no_translation('es'): assert translation.get_language() == 'es' assert translation.get_language() == 'pt-br' finally: translation.activate(old_lang)
def send(self): obj = self.addon or self.user if self.reporter: user_name = '%s (%s)' % (self.reporter.name, self.reporter.email) else: user_name = 'An anonymous coward' with no_translation(): type_ = (gettext(amo.ADDON_TYPE[self.addon.type]) if self.addon else 'User') subject = u'[%s] Abuse Report for %s' % (type_, obj.name) msg = u'%s reported abuse for %s (%s%s).\n\n%s' % ( user_name, obj.name, settings.SITE_URL, obj.get_url_path(), self.message) send_mail(subject, msg, recipient_list=(settings.ABUSE_EMAIL, ))
def send(self): obj = self.addon or self.user if self.reporter: user_name = '%s (%s)' % (self.reporter.name, self.reporter.email) else: user_name = 'An anonymous coward' with no_translation(): type_ = (gettext(amo.ADDON_TYPE[self.addon.type]) if self.addon else 'User') subject = u'[%s] Abuse Report for %s' % (type_, obj.name) msg = u'%s reported abuse for %s (%s%s).\n\n%s' % ( user_name, obj.name, settings.SITE_URL, obj.get_url_path(), self.message) send_mail(subject, msg, recipient_list=(settings.ABUSE_EMAIL,))
def log_and_notify(action, comments, note_creator, version, perm_setting=None, detail_kwargs=None): log_kwargs = { 'user': note_creator, 'created': datetime.datetime.now(), } if detail_kwargs is None: detail_kwargs = {} if comments: detail_kwargs['version'] = version.version detail_kwargs['comments'] = comments else: # Just use the name of the action if no comments provided. Alas we # can't know the locale of recipient, and our templates are English # only so prevent language jumble by forcing into en-US. with no_translation(): comments = '%s' % action.short if detail_kwargs: log_kwargs['details'] = detail_kwargs note = ActivityLog.create(action, version.addon, version, **log_kwargs) if not note: return # Collect reviewers involved with this version. review_perm = (amo.permissions.ADDONS_REVIEW if version.channel == amo.RELEASE_CHANNEL_LISTED else amo.permissions.ADDONS_REVIEW_UNLISTED) log_users = { alog.user for alog in ActivityLog.objects.for_version(version) if acl.action_allowed_user(alog.user, review_perm)} # Collect add-on authors (excl. the person who sent the email.) addon_authors = set(version.addon.authors.all()) - {note_creator} # Collect staff that want a copy of the email staff = set( UserProfile.objects.filter(groups__name=ACTIVITY_MAIL_GROUP)) # If task_user doesn't exist that's no big issue (i.e. in tests) try: task_user = {get_task_user()} except UserProfile.DoesNotExist: task_user = set() # Collect reviewers on the thread (excl. the email sender and task user for # automated messages). reviewers = log_users - addon_authors - task_user - {note_creator} staff_cc = staff - reviewers - addon_authors - task_user - {note_creator} author_context_dict = { 'name': version.addon.name, 'number': version.version, 'author': note_creator.name, 'comments': comments, 'url': absolutify(version.addon.get_dev_url('versions')), 'SITE_URL': settings.SITE_URL, 'email_reason': 'you are an author of this add-on' } reviewer_context_dict = author_context_dict.copy() reviewer_context_dict['url'] = absolutify( reverse('reviewers.review', kwargs={'addon_id': version.addon.pk, 'channel': amo.CHANNEL_CHOICES_API[version.channel]}, add_prefix=False)) reviewer_context_dict['email_reason'] = 'you reviewed this add-on' staff_cc_context_dict = reviewer_context_dict.copy() staff_cc_context_dict['email_reason'] = ( 'you are member of the activity email cc group') # Not being localised because we don't know the recipients locale. with translation.override('en-US'): subject = u'Mozilla Add-ons: %s %s' % ( version.addon.name, version.version) template = template_from_user(note_creator, version) from_email = formataddr((note_creator.name, NOTIFICATIONS_FROM_EMAIL)) send_activity_mail( subject, template.render(author_context_dict), version, addon_authors, from_email, note.id, perm_setting) send_activity_mail( subject, template.render(reviewer_context_dict), version, reviewers, from_email, note.id, perm_setting) send_activity_mail( subject, template.render(staff_cc_context_dict), version, staff_cc, from_email, note.id, perm_setting) if action == amo.LOG.DEVELOPER_REPLY_VERSION: version.update(has_info_request=False) return note
def type(self): with no_translation(): type_ = (ugettext(amo.ADDON_TYPE[self.addon.type]) if self.addon else 'User' if self.user else 'Addon') return type_
def log_and_notify(action, comments, note_creator, version): log_kwargs = { 'user': note_creator, 'created': datetime.datetime.now(), } if comments: log_kwargs['details'] = { 'comments': comments, 'version': version.version } else: # Just use the name of the action if no comments provided. Alas we # can't know the locale of recipient, and our templates are English # only so prevent language jumble by forcing into en-US. with no_translation(): comments = '%s' % action.short note = ActivityLog.create(action, version.addon, version, **log_kwargs) # Collect reviewers involved with this version. review_perm = ('Review' if version.channel == amo.RELEASE_CHANNEL_LISTED else 'ReviewUnlisted') log_users = { alog.user for alog in ActivityLog.objects.for_version(version) if acl.action_allowed_user(alog.user, 'Addons', review_perm) } # Collect add-on authors (excl. the person who sent the email.) addon_authors = set(version.addon.authors.all()) - {note_creator} # Collect staff that want a copy of the email staff_cc = set( UserProfile.objects.filter(groups__name=ACTIVITY_MAIL_GROUP)) # If task_user doesn't exist that's no big issue (i.e. in tests) try: task_user = {get_task_user()} except UserProfile.DoesNotExist: task_user = set() # Collect reviewers on the thread (excl. the email sender and task user for # automated messages). reviewers = ((log_users | staff_cc) - addon_authors - task_user - {note_creator}) author_context_dict = { 'name': version.addon.name, 'number': version.version, 'author': note_creator.name, 'comments': comments, 'url': absolutify(version.addon.get_dev_url('versions')), 'SITE_URL': settings.SITE_URL, } reviewer_context_dict = author_context_dict.copy() reviewer_context_dict['url'] = absolutify( reverse('editors.review', args=[version.addon.pk], add_prefix=False)) # Not being localised because we don't know the recipients locale. with translation.override('en-US'): subject = u'Mozilla Add-ons: %s %s %s' % ( version.addon.name, version.version, action.short) template = loader.get_template('activity/emails/developer.txt') send_activity_mail(subject, template.render(Context(author_context_dict)), version, addon_authors, settings.EDITORS_EMAIL) send_activity_mail(subject, template.render(Context(reviewer_context_dict)), version, reviewers, settings.EDITORS_EMAIL) return note
def log_and_notify(action, comments, note_creator, version, perm_setting=None, detail_kwargs=None): log_kwargs = { 'user': note_creator, 'created': datetime.datetime.now(), } if detail_kwargs is None: detail_kwargs = {} if comments: detail_kwargs['version'] = version.version detail_kwargs['comments'] = comments else: # Just use the name of the action if no comments provided. Alas we # can't know the locale of recipient, and our templates are English # only so prevent language jumble by forcing into en-US. with no_translation(): comments = '%s' % action.short if detail_kwargs: log_kwargs['details'] = detail_kwargs note = ActivityLog.create(action, version.addon, version, **log_kwargs) if not note: return # Collect reviewers involved with this version. review_perm = (amo.permissions.ADDONS_REVIEW if version.channel == amo.RELEASE_CHANNEL_LISTED else amo.permissions.ADDONS_REVIEW_UNLISTED) log_users = { alog.user for alog in ActivityLog.objects.for_version(version) if acl.action_allowed_user(alog.user, review_perm) } # Collect add-on authors (excl. the person who sent the email.) addon_authors = set(version.addon.authors.all()) - {note_creator} # Collect staff that want a copy of the email staff = set(UserProfile.objects.filter(groups__name=ACTIVITY_MAIL_GROUP)) # If task_user doesn't exist that's no big issue (i.e. in tests) try: task_user = {get_task_user()} except UserProfile.DoesNotExist: task_user = set() # Collect reviewers on the thread (excl. the email sender and task user for # automated messages). reviewers = log_users - addon_authors - task_user - {note_creator} staff_cc = staff - reviewers - addon_authors - task_user - {note_creator} author_context_dict = { 'name': version.addon.name, 'number': version.version, 'author': note_creator.name, 'comments': comments, 'url': absolutify(version.addon.get_dev_url('versions')), 'SITE_URL': settings.SITE_URL, 'email_reason': 'you are an author of this add-on' } reviewer_context_dict = author_context_dict.copy() reviewer_context_dict['url'] = absolutify( reverse('editors.review', kwargs={ 'addon_id': version.addon.pk, 'channel': amo.CHANNEL_CHOICES_API[version.channel] }, add_prefix=False)) reviewer_context_dict['email_reason'] = 'you reviewed this add-on' staff_cc_context_dict = reviewer_context_dict.copy() staff_cc_context_dict['email_reason'] = ( 'you are member of the activity email cc group') # Not being localised because we don't know the recipients locale. with translation.override('en-US'): subject = u'Mozilla Add-ons: %s %s' % (version.addon.name, version.version) template = template_from_user(note_creator, version) from_email = formataddr((note_creator.name, NOTIFICATIONS_FROM_EMAIL)) send_activity_mail(subject, template.render(author_context_dict), version, addon_authors, from_email, note.id, perm_setting) send_activity_mail(subject, template.render(reviewer_context_dict), version, reviewers, from_email, note.id, perm_setting) send_activity_mail(subject, template.render(staff_cc_context_dict), version, staff_cc, from_email, note.id, perm_setting) if action == amo.LOG.DEVELOPER_REPLY_VERSION: version.update(has_info_request=False) return note
def notify_about_activity_log(addon, version, note, perm_setting=None, send_to_reviewers=True, send_to_staff=True): """Notify relevant users about an ActivityLog note.""" comments = (note.details or {}).get('comments') if not comments: # Just use the name of the action if no comments provided. Alas we # can't know the locale of recipient, and our templates are English # only so prevent language jumble by forcing into en-US. with no_translation(): comments = '%s' % amo.LOG_BY_ID[note.action].short else: htmlparser = HTMLParser() comments = htmlparser.unescape(comments) # Collect add-on authors (excl. the person who sent the email.) and build # the context for them. addon_authors = set(addon.authors.all()) - {note.user} author_context_dict = { 'name': addon.name, 'number': version.version, 'author': note.user.name, 'comments': comments, 'url': absolutify(addon.get_dev_url('versions')), 'SITE_URL': settings.SITE_URL, 'email_reason': 'you are listed as an author of this add-on', 'is_info_request': note.action == amo.LOG.REQUEST_INFORMATION.id, } # Not being localised because we don't know the recipients locale. with translation.override('en-US'): if note.action == amo.LOG.REQUEST_INFORMATION.id: if addon.pending_info_request: days_left = ( # We pad the time left with an extra hour so that the email # does not end up saying "6 days left" because a few # seconds or minutes passed between the datetime was saved # and the email was sent. addon.pending_info_request + timedelta(hours=1) - datetime.now()).days if days_left > 9: author_context_dict['number_of_days_left'] = ('%d days' % days_left) elif days_left > 1: author_context_dict['number_of_days_left'] = ( '%s (%d) days' % (apnumber(days_left), days_left)) else: author_context_dict['number_of_days_left'] = 'one (1) day' subject = u'Mozilla Add-ons: Action Required for %s %s' % ( addon.name, version.version) else: subject = u'Mozilla Add-ons: %s %s' % (addon.name, version.version) # Build and send the mail for authors. template = template_from_user(note.user, version) from_email = formataddr((note.user.name, NOTIFICATIONS_FROM_EMAIL)) send_activity_mail(subject, template.render(author_context_dict), version, addon_authors, from_email, note.id, perm_setting) if send_to_reviewers or send_to_staff: # If task_user doesn't exist that's no big issue (i.e. in tests) try: task_user = {get_task_user()} except UserProfile.DoesNotExist: task_user = set() if send_to_reviewers: # Collect reviewers on the thread (excl. the email sender and task user # for automated messages), build the context for them and send them # their copy. log_users = { alog.user for alog in ActivityLog.objects.for_version(version) if acl.is_user_any_kind_of_reviewer(alog.user) } reviewers = log_users - addon_authors - task_user - {note.user} reviewer_context_dict = author_context_dict.copy() reviewer_context_dict['url'] = absolutify( reverse('reviewers.review', kwargs={ 'addon_id': version.addon.pk, 'channel': amo.CHANNEL_CHOICES_API[version.channel] }, add_prefix=False)) reviewer_context_dict['email_reason'] = 'you reviewed this add-on' send_activity_mail(subject, template.render(reviewer_context_dict), version, reviewers, from_email, note.id, perm_setting) if send_to_staff: # Collect staff that want a copy of the email, build the context for # them and send them their copy. staff = set( UserProfile.objects.filter(groups__name=ACTIVITY_MAIL_GROUP)) staff_cc = (staff - reviewers - addon_authors - task_user - {note.user}) staff_cc_context_dict = reviewer_context_dict.copy() staff_cc_context_dict['email_reason'] = ( 'you are member of the activity email cc group') send_activity_mail(subject, template.render(staff_cc_context_dict), version, staff_cc, from_email, note.id, perm_setting)