def video_url_create(request): output = {} form = CreateVideoUrlForm(request.user, request.POST) if form.is_valid(): obj = form.save() video = form.cleaned_data["video"] users = video.notification_list_all(request.user) for user in users: subject = u'New video URL added by %(username)s to "%(video_title)s" on universalsubtitles.org' subject = subject % {"url": obj.url, "username": obj.added_by, "video_title": video} context = { "video": video, "video_url": obj, "user": user, "domain": Site.objects.get_current().domain, "hash": user.hash_for_video(video.video_id), } send_templated_email( user.email, subject, "videos/email_video_url_add.html", context, "*****@*****.**", fail_silently=not settings.DEBUG, ) else: output["errors"] = form.get_errors() return HttpResponse(json.dumps(output))
def send_new_message_notification(message_id): try: message = Message.objects.get(pk=message_id) except Message.DoesNotExist: msg = '**send_new_message_notification**. Message does not exist. ID: %s' % message_id client.create_from_text(msg, logger='celery') return user = message.user if not user.email or not user.is_active or not user.changes_notification: return to = "%s <%s>" % (user, user.email) subject = _(u"%(author)s sent you a message on Universal Subtitles: %(subject)s") subject = subject % { 'author': message.author, 'subject': message.subject } context = { "message": message, "domain": Site.objects.get_current().domain } send_templated_email(to, subject, "messages/email/message_received.html", context)
def video_url_create(request): output = {} form = CreateVideoUrlForm(request.user, request.POST) if form.is_valid(): obj = form.save() video = form.cleaned_data['video'] users = video.notification_list(request.user) for user in users: subject = u'New video URL added by %(username)s to "%(video_title)s" on universalsubtitles.org' subject = subject % {'url': obj.url, 'username': obj.added_by, 'video_title': video} context = { 'video': video, 'video_url': obj, 'user': user, 'domain': Site.objects.get_current().domain, 'hash': user.hash_for_video(video.video_id) } send_templated_email(user.email, subject, 'videos/email_video_url_add.html', context, fail_silently=not settings.DEBUG) else: output['errors'] = form.get_errors() return HttpResponse(json.dumps(output))
def send_change_title_email(video_id, user_id, old_title, new_title): from videos.models import Video from auth.models import CustomUser as User domain = Site.objects.get_current().domain try: video = Video.objects.get(id=video_id) user = user_id and User.objects.get(id=user_id) except ObjectDoesNotExist: return users = video.notification_list(user) for obj in users: subject = u'Video\'s title changed on Universal Subtitles' context = { 'user': obj, 'domain': domain, 'video': video, 'editor': user, 'old_title': old_title, 'hash': obj.hash_for_video(video.video_id), 'new_title': new_title } send_templated_email(obj.email, subject, 'videos/email_title_changed.html', context, fail_silently=not settings.DEBUG)
def add_videos_notification(*args, **kwargs): from teams.models import TeamVideo, Team domain = Site.objects.get_current().domain qs = Team.objects.filter(teamvideo__created__gt=F('last_notification_time')).distinct() for team in qs: team_videos = TeamVideo.objects.filter(team=team, created__gt=team.last_notification_time) team.last_notification_time = datetime.now() team.save() members = team.users.filter(changes_notification=True, is_active=True) \ .filter(teammember__changes_notification=True).distinct() subject = _(u'New %(team)s videos ready for subtitling!') % dict(team=team) for user in members: if not user.email: continue context = { 'domain': domain, 'user': user, 'team': team, 'team_videos': team_videos, "STATIC_URL": settings.STATIC_URL, } send_templated_email(user.email, subject, 'teams/email_new_videos.html', context, fail_silently=not settings.DEBUG)
def add_video_notification(team_video_id): """ Celery task for sending emails to members about new video in team. NOTE: It not used now(see command fill_team_video_language), but can be useful in future. """ from teams.models import TeamVideo, Team domain = Site.objects.get_current().domain try: tv = TeamVideo.objects.get(pk=team_video_id) except TeamVideo.DoesNotExist: return qs = tv.team.users.exclude(pk=tv.added_by).filter(changes_notification=True, is_active=True) \ .filter(teammember__changes_notification=True) subject = u'New %s videos ready for subtitling!' % tv.team for user in qs: if not user.email: continue context = { 'domain': domain, 'user': user, 'team': tv.team, 'team_video': tv } send_templated_email(user.email, subject, 'teams/email_new_video.html', context, '*****@*****.**', fail_silently=not settings.DEBUG)
def log_session(self, request, draft_pk, log): dialog_log = WidgetDialogLog(draft_pk=draft_pk, browser_id=request.browser_id, log=log) dialog_log.save() send_templated_email( settings.WIDGET_LOG_EMAIL, "Subtitle save failure", "widget/session_log_email.txt", {"log_pk": dialog_log.pk}, fail_silently=False, ) return {"response": "ok"}
def log_session(self, request, draft_pk, log): dialog_log = WidgetDialogLog( draft_pk=draft_pk, browser_id=request.browser_id, log=log) dialog_log.save() send_templated_email( settings.WIDGET_LOG_EMAIL, 'Subtitle save failure', 'widget/session_log_email.txt', { 'log_pk': dialog_log.pk }, fail_silently=False) return { 'response': 'ok' }
def _send_letter_caption(caption_version): from videos.models import SubtitleVersion domain = Site.objects.get_current().domain language = caption_version.language video = language.video qs = SubtitleVersion.objects.filter(language=language) \ .filter(version_no__lt=caption_version.version_no).order_by('-version_no') if qs.count() == 0: return most_recent_version = qs[0] captions = _make_caption_data(caption_version, most_recent_version) context = { 'version': caption_version, 'domain': domain, 'translation': not language.is_original, 'video': caption_version.video, 'language': language, 'last_version': most_recent_version, 'captions': captions, 'video_url': video.get_absolute_url(), 'language_url': language.get_absolute_url(), 'user_url': caption_version.user and caption_version.user.get_absolute_url() } subject = u'New edits to "%s" by %s on Universal Subtitles' % (language.video, caption_version.user) users = [] followers = set(video.notification_list(caption_version.user)) followers.update(language.notification_list(caption_version.user)) for item in qs: if item.user and item.user in followers: context['your_version'] = item context['user'] = item.user context['hash'] = item.user.hash_for_video(context['video'].video_id) send_templated_email(item.user.email, subject, 'videos/email_notification.html', context, fail_silently=not settings.DEBUG) followers.discard(item.user) for user in followers: context['user'] = user context['hash'] = user.hash_for_video(context['video'].video_id) send_templated_email(user.email, subject, 'videos/email_notification_non_editors.html', context, fail_silently=not settings.DEBUG)
def _send_letter_caption(caption_version): from videos.models import SubtitleVersion domain = Site.objects.get_current().domain language = caption_version.language video = language.video qs = SubtitleVersion.objects.filter(language=language) \ .filter(version_no__lt=caption_version.version_no).order_by('-version_no') if qs.count() == 0: return most_recent_version = qs[0] captions = _make_caption_data(caption_version, most_recent_version) context = { 'version': caption_version, 'domain': domain, 'translation': not language.is_original, 'video': caption_version.video, 'language': language, 'last_version': most_recent_version, 'captions': captions, 'video_url': language.get_absolute_url(), 'user_url': caption_version.user and caption_version.user.get_absolute_url() } subject = u'New edits to "%s" by %s on Universal Subtitles' % (language.video, caption_version.user) users = [] video_followers = video.notification_list(caption_version.user) language_followers = language.notification_list(caption_version.user) for item in qs: if item.user and not item.user in users and (item.user in video_followers or \ item.user in language_followers): context['your_version'] = item context['user'] = item.user context['hash'] = item.user.hash_for_video(context['video'].video_id) send_templated_email(item.user.email, subject, 'videos/email_notification.html', context, '*****@*****.**', fail_silently=not settings.DEBUG) users.append(item.user)
def send_reject_notification(task_pk, sent_back): raise NotImplementedError() from teams.models import Task from videos.models import Action from messages.models import Message try: task = Task.objects.select_related( "team_video__video", "team_video", "assignee", "subtitle_version").get( pk=task_pk) except Task.DoesNotExist: return False version = task.get_subtitle_version() subject = ugettext(u"Your subtitles were not accepted") if task.new_review_base_version: user = task.new_review_base_version.author else: user = version.author task_language = get_language_label(task.language) reviewer = task.assignee video = task.team_video.video subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={ 'video_id': video.video_id, 'lang': task.language, 'lang_id': version.subtitle_language.pk, })) reviewer_message_url = "%s%s?user=%s" % ( get_url_base(), reverse("messages:new"), reviewer.username) context = { "team":task.team, "title": version.subtitle_language.get_title(), "user":user, "task_language": task_language, "url_base":get_url_base(), "task":task, "reviewer":reviewer, "note":task.body, "sent_back": sent_back, "subs_url": subs_url, "reviewer_message_url": reviewer_message_url, } msg = None if user.notify_by_message: template_name = "messages/team-task-rejected.txt" msg = Message() msg.subject = subject msg.content = render_to_string(template_name,context) msg.user = user msg.object = task.team msg.save() template_name = "messages/email/team-task-rejected.html" email_res = send_templated_email(user, subject, template_name, context) Action.create_rejected_video_handler(version, reviewer) return msg, email_res
def videos_imported_message(user_pk, imported_videos): from messages.models import Message user = User.objects.get(pk=user_pk) subject = _(u"Your videos were imported!") url = "%s%s" % (get_url_base(), reverse("profiles:my_videos")) context = { "user": user, "imported_videos": imported_videos, "my_videos_url": url } if user.notify_by_message: body = render_to_string("messages/videos-imported.txt", context) message = Message(user=user, subject=subject, content=body) message.save() template_name = "messages/email/videos-imported.html" Meter('templated-emails-sent-by-type.videos-imported').inc() send_templated_email(user, subject, template_name, context)
def _send_letter_translation_start(translation_version): domain = Site.objects.get_current().domain video = translation_version.language.video language = translation_version.language for user in video.notification_list(translation_version.user): context = { 'version': translation_version, 'domain': domain, 'video_url': 'http://%s%s' % (domain, video.get_absolute_url()), 'user': user, 'language': language, 'video': video, 'hash': user.hash_for_video(video.video_id) } subject = 'New %s translation by %s of "%s"' % \ (language.language_display(), translation_version.user.__unicode__(), video.__unicode__()) send_templated_email(user.email, subject, 'videos/email_start_notification.html', context, fail_silently=not settings.DEBUG)
def ajax_change_video_title(request): video_id = request.POST.get("video_id") title = request.POST.get("title") user = request.user try: video = Video.objects.get(video_id=video_id) if title and not video.title or video.is_html5() or user.is_superuser: old_title = video.title_display() video.title = title video.slug = slugify(video.title) video.save() action = Action(new_video_title=video.title, video=video) action.user = user.is_authenticated() and user or None action.created = datetime.now() action.action_type = Action.CHANGE_TITLE action.save() users = video.notification_list(user) for obj in users: subject = u"Video's title changed on Universal Subtitles" context = { "user": obj, "domain": Site.objects.get_current().domain, "video": video, "editor": user, "old_title": old_title, "hash": obj.hash_for_video(video.video_id), } send_templated_email( obj.email, subject, "videos/email_title_changed.html", context, "*****@*****.**", fail_silently=not settings.DEBUG, ) except Video.DoesNotExist: pass return HttpResponse("")
def videos_imported_message(user_pk, imported_videos): from messages.models import Message user = User.objects.get(pk=user_pk) subject = u"Your videos were imported!" url = "%s%s" % (get_url_base(), reverse("profiles:videos", kwargs={'user_id': user_pk})) context = {"user": user, "imported_videos": imported_videos, "my_videos_url": url} if user.notify_by_message: body = render_to_string("messages/videos-imported.txt", context) message = Message( user=user, subject=subject, content=body ) message.save() template_name = "messages/email/videos-imported.html" send_templated_email(user, subject, template_name, context)
def send_letter_translation_start(self, translation_version): video = translation_version.language.video language = translation_version.language for user in video.notification_list(translation_version.user): context = { 'version': translation_version, 'domain': self.domain, 'video_url': 'http://{0}{1}'.format( self.domain, reverse('videos:video', [video.id])), 'user': user, 'language': language, 'video': video, 'hash': user.hash_for_video(video.video_id) } subject = 'New %s translation by %s of "%s"' % \ (language.language_display(), translation_version.user.__unicode__(), video.__unicode__()) send_templated_email(user.email, subject, 'videos/email_start_notification.html', context, '*****@*****.**', fail_silently=not settings.DEBUG)
def _send_letter_translation_start(translation_version): domain = Site.objects.get_current().domain video = translation_version.language.video language = translation_version.language for user in video.notification_list(translation_version.user): context = { 'version': translation_version, 'domain': domain, 'video_url': 'http://%s%s' % (domain, video.get_absolute_url()), 'user': user, 'language': language, 'video': video, 'hash': user.hash_for_video(video.video_id) } subject = 'New %s translation by %s of "%s"' % \ (language.language_display(), translation_version.user.__unicode__(), video.__unicode__()) send_templated_email(user.email, subject, 'videos/email_start_notification.html', context, '*****@*****.**', fail_silently=not settings.DEBUG)
def application_sent(application_pk): if getattr(settings, "MESSAGES_DISABLED", False): return from messages.models import Message from teams.models import Application, TeamMember application = Application.objects.get(pk=application_pk) if not team_sends_notification(application.team, 'block_application_sent_message'): return False notifiable = TeamMember.objects.filter( team=application.team, user__is_active=True, role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER]) for m in notifiable: template_name = "messages/application-sent.txt" context = { "application": application, "applicant": application.user, "url_base": get_url_base(), "team": application.team, "note": application.note, "user": m.user, } body = render_to_string(template_name, context) subject = fmt(ugettext(u'%(user)s is applying for team %(team)s'), user=application.user, team=application.team.name) if m.user.notify_by_message: msg = Message() msg.message_type = 'S' msg.subject = subject msg.content = body msg.user = m.user msg.object = application.team msg.author = application.user msg.save() send_templated_email(m.user, subject, "messages/email/application-sent-email.html", context) return True
def ajax_change_video_title(request): video_id = request.POST.get('video_id') title = request.POST.get('title') user = request.user try: video = Video.objects.get(video_id=video_id) if title and not video.title or video.is_html5() or user.is_superuser: old_title = video.title_display() video.title = title video.slug = slugify(video.title) video.save() action = Action(new_video_title=video.title, video=video) action.user = user.is_authenticated() and user or None action.created = datetime.now() action.action_type = Action.CHANGE_TITLE action.save() users = video.notification_list(user) for obj in users: subject = u'Video\'s title changed on Universal Subtitles' context = { 'user': obj, 'domain': Site.objects.get_current().domain, 'video': video, 'editor': user, 'old_title': old_title, 'hash': obj.hash_for_video(video.video_id) } send_templated_email(obj.email, subject, 'videos/email_title_changed.html', context, '*****@*****.**', fail_silently=not settings.DEBUG) except Video.DoesNotExist: pass return HttpResponse('')
def add_videos_notification(*args, **kwargs): from messages.tasks import _team_sends_notification from teams.models import TeamVideo, Team domain = Site.objects.get_current().domain qs = Team.objects.filter( teamvideo__created__gt=F('last_notification_time')).distinct() for team in qs: if not _team_sends_notification(team, 'block_new_video_message'): continue team_videos = TeamVideo.objects.filter( team=team, created__gt=team.last_notification_time) team.last_notification_time = datetime.now() team.save() members = team.users.filter( notify_by_email=True, is_active=True) \ .distinct() subject = _(u'New %(team)s videos ready for subtitling!') % dict( team=team) for user in members: if not user.email: continue context = { 'domain': domain, 'user': user, 'team': team, 'team_videos': team_videos, "STATIC_URL": settings.STATIC_URL, } Meter('templated-emails-sent-by-type.team.new-videos-ready').inc() send_templated_email(user, subject, 'teams/email_new_videos.html', context, fail_silently=not settings.DEBUG)
def send_new_translation_notification(translation_version): domain = settings.HOSTNAME video = translation_version.language.video language = translation_version.language for user in video.notification_list(translation_version.user): context = { 'version': translation_version, 'domain': domain, 'video_url': '%s://%s%s' % (DEFAULT_PROTOCOL, domain, video.get_absolute_url()), 'user': user, 'language': language, 'video': video, 'hash': user.hash_for_video(video.video_id), "STATIC_URL": settings.STATIC_URL, } subject = 'New %s translation by %s of "%s"' % \ (language.language_display(), translation_version.user.__unicode__(), video.__unicode__()) send_templated_email(user, subject, 'videos/email_start_notification.html', context, fail_silently=not settings.DEBUG) return True
def add_videos_notification(*args, **kwargs): from teams.models import TeamVideo, Team domain = Site.objects.get_current().domain qs = Team.objects.filter( teamvideo__created__gt=F('last_notification_time')).distinct() for team in qs: team_videos = TeamVideo.objects.filter( team=team, created__gt=team.last_notification_time) team.last_notification_time = datetime.now() team.save() members = team.users.filter(changes_notification=True, is_active=True) \ .filter(teammember__changes_notification=True).distinct() subject = _(u'New %(team)s videos ready for subtitling!') % dict( team=team) for user in members: if not user.email: continue context = { 'domain': domain, 'user': user, 'team': team, 'team_videos': team_videos, "STATIC_URL": settings.STATIC_URL, } send_templated_email(user.email, subject, 'teams/email_new_videos.html', context, fail_silently=not settings.DEBUG)
def send_templated_email_async(to, subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False): return send_templated_email(to, subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False)
def send_letter_caption(self, caption_version): language = caption_version.language video = language.video qs = SubtitleVersion.objects.filter(language=language) \ .filter(version_no__lt=caption_version.version_no).order_by('-version_no') if qs.count() == 0: return most_recent_version = qs[0] captions = self._make_caption_data(caption_version, most_recent_version) context = { 'version': caption_version, 'domain': self.domain, 'translation': not language.is_original, 'video': caption_version.video, 'language': language, 'last_version': most_recent_version, 'captions': captions, 'video_url': language.get_absolute_url() } subject = 'New edits to "%s" by %s on Universal Subtitles' % \ (language.video.__unicode__(), caption_version.user.__unicode__()) users = [] video_followers = video.notification_list([caption_version.user]) for item in qs: users.append(item.user) if item.user and not item.user in users and item.user in video_followers: context['your_version'] = item context['user'] = item.user context['hash'] = item.user.hash_for_video(context['video'].video_id) send_templated_email(item.user.email, subject, 'videos/email_notification.html', context, '*****@*****.**', fail_silently=not settings.DEBUG)
def send_notification(self, team, date): team_videos = team.teamvideo_set.filter(created__gte=date) if not team_videos: return subject = u'New %s videos ready for subtitling!' % team qs = team.users.filter(changes_notification=True, is_active=True) \ .filter(teammember__changes_notification=True) for user in qs: if not user.email: continue context = { 'domain': self.domain, 'user': user, 'team': team, 'team_videos': team_videos } send_templated_email(user.email, subject, 'teams/email_new_videos.html', context, fail_silently=not settings.DEBUG)
def _notify_teams_of_new_videos(team_qs): from messages.tasks import team_sends_notification from teams.models import TeamVideo domain = Site.objects.get_current().domain for team in team_qs: if not team_sends_notification(team, 'block_new_video_message'): continue team_videos = TeamVideo.objects.filter( team=team, created__gt=team.last_notification_time) team.last_notification_time = datetime.now() team.save() members = team.users.filter( notify_by_email=True, is_active=True) \ .distinct() subject = fmt(_(u'New %(team)s videos ready for subtitling!'), team=team) for user in members: if not user.email: continue context = { 'domain': domain, 'user': user, 'team': team, 'team_videos': team_videos, "STATIC_URL": settings.STATIC_URL, } send_templated_email(user, subject, 'teams/email_new_videos.html', context, fail_silently=not settings.DEBUG)
def team_invitation_sent(invite_pk): from messages.models import Message from teams.models import Invite, Setting, TeamMember invite = Invite.objects.get(pk=invite_pk) if not team_sends_notification( invite.team, 'block_invitation_sent_message') or not invite.user.is_active: return False # does this team have a custom message for this? team_default_message = None messages = Setting.objects.messages().filter(team=invite.team) if messages.exists(): data = {} for m in messages: data[m.get_key_display()] = m.data mapping = { TeamMember.ROLE_ADMIN: data['messages_admin'], TeamMember.ROLE_MANAGER: data['messages_manager'], TeamMember.ROLE_CONTRIBUTOR: data['messages_invite'], } team_default_message = mapping.get(invite.role, None) context = { 'invite': invite, 'role': invite.role, "user": invite.user, "inviter": invite.author, "team": invite.team, "invite_pk": invite_pk, 'note': invite.note, 'custom_message': team_default_message, 'url_base': get_url_base(), } title = fmt(ugettext(u"You've been invited to team %(team)s on Amara"), team=invite.team.name) if invite.user.notify_by_message: body = render_to_string("messages/team-you-have-been-invited.txt", context) msg = Message() msg.message_type = 'S' msg.subject = title msg.user = invite.user msg.object = invite msg.author = invite.author msg.content = body msg.save() template_name = 'messages/email/team-you-have-been-invited.html' return send_templated_email(invite.user, title, template_name, context)
def send_templated_email_async(to, subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False, check_user_preference=True): return send_templated_email(to, subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False, check_user_preference=check_user_preference)
def team_task_assigned(task_pk): from teams.models import Task from messages.models import Message try: task = Task.objects.select_related("team_video__video", "team_video", "assignee").get( pk=task_pk, assignee__isnull=False) except Task.DoesNotExist: return False task_type = Task.TYPE_NAMES[task.type] subject = ugettext(u"You have a new task assignment on Amara!") user = task.assignee if not team_sends_notification( task.team, 'block_task_assigned_message') or not user.is_active: return False task_language = None if task.language: task_language = get_language_label(task.language) context = { "team": task.team, "user": user, "task_type": task_type, "task_language": task_language, "url_base": get_url_base(), "task": task, } msg = None if user.notify_by_message: template_name = "messages/team-task-assigned.txt" msg = Message() msg.message_type = 'S' msg.subject = subject msg.content = render_to_string(template_name, context) msg.user = user msg.object = task.team msg.save() template_name = "messages/email/team-task-assigned.html" email_res = send_templated_email(user, subject, template_name, context) return msg, email_res
def approved_notification(task_pk, published=False): """ On approval, it can be sent back (published=False) or approved and published """ from teams.models import Task from videos.models import Action from messages.models import Message try: task = Task.objects.select_related( "team_video__video", "team_video", "assignee", "subtitle_version").get( pk=task_pk) if not _team_sends_notification(task.team, 'block_approved_message'): return False except Task.DoesNotExist: return False # some tasks are being created without subtitles version, see # https://unisubs.sifterapp.com/projects/12298/issues/552092/comments if published: subject = ugettext(u"Your subtitles have been approved and published!") template_txt = "messages/team-task-approved-published.txt" template_html ="messages/email/team-task-approved-published.html" else: template_txt = "messages/team-task-approved-sentback.txt" template_html ="messages/email/team-task-approved-sentback.html" subject = ugettext(u"Your subtitles have been returned for further editing") version = task.get_subtitle_version() if task.new_review_base_version: user = task.new_review_base_version.author else: user = version.author task_language = get_language_label(task.language) reviewer = task.assignee video = task.team_video.video subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={ 'video_id': video.video_id, 'lang': task.language, 'lang_id': version.subtitle_language.pk, })) reviewer_message_url = "%s%s?user=%s" % ( get_url_base(), reverse("messages:new"), reviewer.username) context = { "team":task.team, "title": version.subtitle_language.get_title(), "user":user, "task_language": task_language, "url_base":get_url_base(), "task":task, "reviewer":reviewer, "note":task.body, "subs_url": subs_url, "reviewer_message_url": reviewer_message_url, } msg = None if user.notify_by_message: template_name = template_txt msg = Message() msg.subject = subject msg.content = render_to_string(template_name,context) msg.user = user msg.object = task.team msg.save() template_name = template_html Meter('templated-emails-sent-by-type.teams.approval-result').inc() email_res = send_templated_email(user, subject, template_name, context) Action.create_approved_video_handler(version, reviewer) return msg, email_res
def send_templated_email_async(to, subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False): return send_templated_email(to,subject, body_template, body_dict, from_email=None, ct="html", fail_silently=False)
def notify_for_version(version): domain = Site.objects.get_current().domain language = version.subtitle_language video = language.video qs = SubtitleVersion.objects.filter(subtitle_language=language).filter( version_number__lt=version.version_number).order_by('-version_number') if qs.count() == 0: return most_recent_version = qs[0] diff_data = diff_subtitles(version.get_subtitles(), most_recent_version.get_subtitles()) title = { 'new_title': version.title, 'old_title': most_recent_version.title, 'has_changed': version.title != most_recent_version.title } description = { 'new_description': version.description, 'old_description': most_recent_version.description, 'has_changed': version.description != most_recent_version.description } context = { 'title': title, 'description': description, 'version': version, 'domain': domain, 'translation': not language.is_primary_audio_language(), 'video': version.video, 'language': language, 'last_version': most_recent_version, 'diff_data': diff_data, 'video_url': video.get_absolute_url(), 'language_url': language.get_absolute_url(), 'user_url': version.author and version.author.get_absolute_url(), "STATIC_URL": settings.STATIC_URL, } subject = u'New edits to "%s" by %s on Amara' % (language.video, version.author) followers = set(video.notification_list(version.author)) followers.update(language.notification_list(version.author)) for item in qs: if item.author and item.author in followers: if item.author.notify_by_email: context['your_version'] = item context['user'] = item.author context['hash'] = item.author.hash_for_video( context['video'].video_id) context['user_is_rtl'] = item.author.guess_is_rtl() send_templated_email(item.author, subject, 'videos/email_notification.html', context, fail_silently=not settings.DEBUG) if item.author.notify_by_message: # TODO: Add body Message.objects.create(user=item.author, subject=subject, content='', message_type='S') followers.discard(item.author) for user in followers: context['user'] = user context['hash'] = user.hash_for_video(context['video'].video_id) context['user_is_rtl'] = user.guess_is_rtl() send_templated_email(user, subject, 'videos/email_notification_non_editors.html', context, fail_silently=not settings.DEBUG) return True
def _reviewed_notification(task_pk, status): from teams.models import Task from videos.models import Action from messages.models import Message try: task = Task.objects.select_related("team_video__video", "team_video", "assignee").get(pk=task_pk) except Task.DoesNotExist: return False subject = ugettext(u"Your subtitles have been reviewed") if status == REVIEWED_AND_PUBLISHED: subject += ugettext(" and published") if task.review_base_version: user = task.review_base_version.user else: user = task.subtitle_version.user task_language = get_language_label(task.language) reviewer = task.assignee video = task.team_video.video subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={ 'video_id': video.video_id, 'lang': task.language, 'lang_id': task.subtitle_version.language.pk, })) reviewer_message_url = "%s%s?user=%s" % ( get_url_base(), reverse("messages:new"), reviewer.username) reviewer_profile_url = "%s%s" % (get_url_base(), reverse("profiles:profile", kwargs={'user_id': reviewer.id})) perform_task_url = "%s%s" % (get_url_base(), reverse("teams:perform_task", kwargs={ 'slug': task.team.slug, 'task_pk': task_pk })) context = { "team": task.team, "title": task.subtitle_version.language.get_title(), "user": user, "task_language": task_language, "url_base": get_url_base(), "task": task, "reviewer": reviewer, "note": task.body, "reviewed_and_pending_approval": status == REVIEWED_AND_PENDING_APPROVAL, "sent_back": status == REVIEWED_AND_SENT_BACK, "reviewed_and_published": status == REVIEWED_AND_PUBLISHED, "subs_url": subs_url, "reviewer_message_url": reviewer_message_url, "reviewer_profile_url": reviewer_profile_url, "perform_task_url": perform_task_url, } msg = None if user.notify_by_message: template_name = "messages/team-task-reviewed.txt" msg = Message() msg.subject = subject msg.content = render_to_string(template_name, context) msg.user = user msg.object = task.team msg.save() template_name = "messages/email/team-task-reviewed.html" Meter('templated-emails-sent-by-type.teams.task-reviewed').inc() email_res = send_templated_email(user, subject, template_name, context) if status == REVIEWED_AND_SENT_BACK: if task.type == Task.TYPE_IDS['Review']: Action.create_declined_video_handler(task.subtitle_version, reviewer) else: Action.create_rejected_video_handler(task.subtitle_version, reviewer) elif status == REVIEWED_AND_PUBLISHED: Action.create_approved_video_handler(task.subtitle_version, reviewer) elif status == REVIEWED_AND_PENDING_APPROVAL: Action.create_accepted_video_handler(task.subtitle_version, reviewer) return msg, email_res
def team_member_new(member_pk): if getattr(settings, "MESSAGES_DISABLED", False): return from messages.models import Message from teams.models import TeamMember member = TeamMember.objects.get(pk=member_pk) if not _team_sends_notification(member.team,'block_team_member_new_message'): return False from videos.models import Action from teams.models import TeamMember # the feed item should appear on the timeline for all team members # as a team might have thousands of members, this one item has # to show up on all of them Action.create_new_member_handler(member) # notify admins and owners through messages notifiable = TeamMember.objects.filter( team=member.team, role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER]).exclude(pk=member.pk) for m in notifiable: context = { "new_member": member.user, "team":member.team, "user":m.user, "role":member.role, "url_base":get_url_base(), } body = render_to_string("messages/team-new-member.txt",context) subject = fmt( ugettext("%(team)s team has a new member"), team=member.team) if m.user.notify_by_message: msg = Message() msg.subject = subject msg.content = body msg.user = m.user msg.object = m.team msg.save() template_name = "messages/email/team-new-member.html" Meter('templated-emails-sent-by-type.teams.new-member').inc() send_templated_email(m.user, subject, template_name, context) # now send welcome mail to the new member template_name = "messages/team-welcome.txt" context = { "team":member.team, "url_base":get_url_base(), "role":member.role, "user":member.user, } body = render_to_string(template_name,context) msg = Message() msg.subject = fmt( ugettext("You've joined the %(team)s team!"), team=member.team) msg.content = body msg.user = member.user msg.object = member.team msg.save() template_name = "messages/email/team-welcome.html" Meter('templated-emails-sent-by-type.teams.welcome').inc() send_templated_email(msg.user, msg.subject, template_name, context)
def send_video_comment_notification(comment_pk_or_instance, version_pk=None): """ Comments can be attached to a video (appear in the videos:video (info)) page) OR sublanguage (appear in the videos:translation_history page) Approval / Reviews notes are also stored as comments. """ from comments.models import Comment from videos.models import Video from subtitles.models import SubtitleLanguage, SubtitleVersion if not isinstance(comment_pk_or_instance, Comment): try: comment = Comment.objects.get(pk=comment_pk_or_instance) except Comment.DoesNotExist: return else: comment = comment_pk_or_instance version = None if version_pk: try: version = SubtitleVersion.objects.get(pk=version_pk) except SubtitleVersion.DoesNotExist: pass ct = comment.content_object if isinstance(ct, Video): video = ct version = None language = None elif isinstance(ct, SubtitleLanguage): video = ct.video language = ct domain = settings.HOSTNAME protocol = getattr(settings, 'DEFAULT_PROTOCOL', 'https') if language: language_url = universal_url("videos:translation_history", kwargs={ "video_id": video.video_id, "lang": language.language_code, "lang_id": language.pk, }) else: language_url = None if version: version_url = universal_url( "videos:subtitleversion_detail", kwargs={ 'video_id': version.video.video_id, 'lang': version.subtitle_language.language_code, 'lang_id': version.subtitle_language.pk, 'version_id': version.pk, }) else: version_url = None subject = fmt(ugettext(u'%(user)s left a comment on the video %(title)s'), user=unicode(comment.user), title=video.title_display()) followers = set(video.notification_list(comment.user)) for user in followers: send_templated_email( user, subject, "messages/email/comment-notification.html", { "video": video, "user": user, "hash": user.hash_for_video(video.video_id), "commenter": unicode(comment.user), "commenter_url": comment.user.get_absolute_url(), "version_url": version_url, "language_url": language_url, "domain": domain, "version": version, "body": comment.content, "STATIC_URL": settings.STATIC_URL, }, fail_silently=not settings.DEBUG) if language: obj = language object_pk = language.pk content_type = ContentType.objects.get_for_model(language) exclude = [ u for u in language.followers.filter(notify_by_message=False) ] exclude.append(comment.user) message_followers = language.notification_list(exclude) else: obj = video object_pk = video.pk content_type = ContentType.objects.get_for_model(video) exclude = list(video.followers.filter(notify_by_message=False)) exclude.append(comment.user) message_followers = video.notification_list(exclude) for user in message_followers: Message.objects.create( user=user, subject=subject, object_pk=object_pk, content_type=content_type, object=obj, message_type="S", content=render_to_string( 'messages/new-comment.html', { "video": video, "language": language, "commenter": unicode(comment.user), "commenter_url": comment.user.get_absolute_url(), "version_url": version_url, "language_url": language_url, "domain": domain, "protocol": protocol, "version": version, "body": comment.content }))
def approved_notification(task_pk, published=False): """ On approval, it can be sent back (published=False) or approved and published """ from teams.models import Task from activity.models import ActivityRecord from messages.models import Message from teams.models import TeamNotificationSetting try: task = Task.objects.select_related("team_video__video", "team_video", "assignee", "subtitle_version").get(pk=task_pk) if not team_sends_notification(task.team, 'block_approved_message'): return False except Task.DoesNotExist: return False # some tasks are being created without subtitles version, see # https://unisubs.sifterapp.com/projects/12298/issues/552092/comments users_to_notify = set() version = task.get_subtitle_version() if task.new_review_base_version: user = task.new_review_base_version.author else: user = version.author if user.is_active: users_to_notify.add(user) if published: subject = ugettext(u"Your subtitles have been approved and published!") template_txt = "messages/team-task-approved-published.txt" template_html = "messages/email/team-task-approved-published.html" # Not sure whether it is the right place to send notification # but should work around the approval when there is no new sub version TeamNotificationSetting.objects.notify_team( task.team.pk, TeamNotificationSetting.EVENT_SUBTITLE_APPROVED, video_id=version.video.video_id, language_pk=version.subtitle_language.pk, version_pk=version.pk) subtitler = task.get_subtitler() if subtitler is not None and \ subtitler.is_active: users_to_notify.add(subtitler) else: template_txt = "messages/team-task-approved-sentback.txt" template_html = "messages/email/team-task-approved-sentback.html" subject = ugettext( u"Your subtitles have been returned for further editing") reviewer = task.assignee ActivityRecord.objects.create_for_version_approved(version, reviewer) if len(users_to_notify) > 0: task_language = get_language_label(task.language) video = task.team_video.video subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={ 'video_id': video.video_id, 'lang': task.language, 'lang_id': version.subtitle_language.pk, })) reviewer_message_url = "%s%s?user=%s" % ( get_url_base(), reverse("messages:new"), reviewer.username) context = { "team": task.team, "title": version.subtitle_language.get_title(), "task_language": task_language, "url_base": get_url_base(), "task": task, "reviewer": reviewer, "note": task.body, "subs_url": subs_url, "reviewer_message_url": reviewer_message_url, } for user in users_to_notify: context['user'] = user msg = None if user.notify_by_message: msg = Message() msg.message_type = 'S' msg.subject = subject msg.content = render_to_string(template_txt, context) msg.user = user msg.object = task.team msg.save() email_res = send_templated_email(user, subject, template_html, context)
def _reviewed_notification(task_pk, status): from teams.models import Task from activity.models import ActivityRecord from messages.models import Message try: task = Task.objects.select_related("team_video__video", "team_video", "assignee").get(pk=task_pk) except Task.DoesNotExist: return False notification_setting_name = { REVIEWED_AND_PUBLISHED: 'block_reviewed_and_published_message', REVIEWED_AND_PENDING_APPROVAL: 'block_reviewed_and_pending_approval_message', REVIEWED_AND_SENT_BACK: 'block_reviewed_and_sent_back_message', }[status] version = task.get_subtitle_version() if task.new_review_base_version: user = task.new_review_base_version.author else: user = version.author if not team_sends_notification( task.team, notification_setting_name) or not user.is_active: return False subject = ugettext(u"Your subtitles have been reviewed") if status == REVIEWED_AND_PUBLISHED: subject += ugettext(" and published") if status == REVIEWED_AND_SENT_BACK: subject = ugettext(u"Needed: additional changes on your subtitles!") task_language = get_language_label(task.language) reviewer = task.assignee video = task.team_video.video subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={ 'video_id': video.video_id, 'lang': task.language, 'lang_id': version.subtitle_language.pk, })) reviewer_message_url = "%s%s?user=%s" % ( get_url_base(), reverse("messages:new"), reviewer.username) reviewer_profile_url = "%s%s" % (get_url_base(), reverse("profiles:profile", kwargs={'user_id': reviewer.id})) perform_task_url = "%s%s" % (get_url_base(), reverse("teams:perform_task", kwargs={ 'slug': task.team.slug, 'task_pk': task_pk })) context = { "team": task.team, "title": version.subtitle_language.get_title(), "user": user, "task_language": task_language, "url_base": get_url_base(), "task": task, "reviewer": reviewer, "note": task.body, "reviewed_and_pending_approval": status == REVIEWED_AND_PENDING_APPROVAL, "sent_back": status == REVIEWED_AND_SENT_BACK, "reviewed_and_published": status == REVIEWED_AND_PUBLISHED, "subs_url": subs_url, "reviewer_message_url": reviewer_message_url, "reviewer_profile_url": reviewer_profile_url, "perform_task_url": perform_task_url, } msg = None if user.notify_by_message: template_name = "messages/team-task-reviewed.txt" msg = Message() msg.message_type = 'S' msg.subject = subject msg.content = render_to_string(template_name, context) msg.user = user msg.object = task.team msg.save() template_name = "messages/email/team-task-reviewed.html" email_res = send_templated_email(user, subject, template_name, context) if status == REVIEWED_AND_SENT_BACK: if task.type == Task.TYPE_IDS['Review']: ActivityRecord.objects.create_for_version_declined( version, reviewer) else: ActivityRecord.objects.create_for_version_rejected( version, reviewer) elif status == REVIEWED_AND_PUBLISHED: ActivityRecord.objects.create_for_version_approved(version, reviewer) elif status == REVIEWED_AND_PENDING_APPROVAL: ActivityRecord.objects.create_for_version_accepted(version, reviewer) return msg, email_res
def team_member_new(member_pk): if getattr(settings, "MESSAGES_DISABLED", False): return from messages.models import Message from teams.models import TeamMember, Setting member = TeamMember.objects.get(pk=member_pk) if not team_sends_notification(member.team, 'block_team_member_new_message'): return False from teams.models import TeamMember # the feed item should appear on the timeline for all team members # as a team might have thousands of members, this one item has # to show up on all of them # notify admins and owners through messages notifiable = TeamMember.objects.filter( team=member.team, user__is_active=True, role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER]).exclude(pk=member.pk) for m in notifiable: context = { "new_member": member.user, "team": member.team, "user": m.user, "role": member.role, "url_base": get_url_base(), } body = render_to_string("messages/team-new-member.txt", context) subject = fmt(ugettext("%(team)s team has a new member"), team=member.team) if m.user.notify_by_message: msg = Message() msg.message_type = 'S' msg.subject = subject msg.content = body msg.user = m.user msg.object = m.team msg.save() template_name = "messages/email/team-new-member.html" send_templated_email(m.user, subject, template_name, context) # does this team have a custom message for this? team_default_message = None messages = Setting.objects.messages().filter(team=member.team) if messages.exists(): for m in messages: if m.get_key_display() == 'messages_joins': team_default_message = m.data break for ul in UserLanguage.objects.filter( user=member.user).order_by("priority"): localized_message = Setting.objects.messages().filter( team=member.team, language_code=ul.language) if len(localized_message) == 1: if team_default_message: team_default_message += u'\n\n----------------\n\n' + localized_message[ 0].data else: team_default_message = localized_message[0].data break # now send welcome mail to the new member template_name = "messages/team-welcome.txt" context = { "team": member.team, "url_base": get_url_base(), "role": member.role, "user": member.user, "custom_message": team_default_message, } body = render_to_string(template_name, context) msg = Message() msg.message_type = 'S' msg.subject = fmt(ugettext("You've joined the %(team)s team!"), team=member.team) msg.content = body msg.user = member.user msg.object = member.team msg.save() template_name = "messages/email/team-welcome.html" send_templated_email(msg.user, msg.subject, template_name, context)
if 'project' in video_item and len(video_item['project']) > 0: project, created = Project.objects.get_or_create( team=team, slug=pan_slugify(video_item['project']), defaults={'name': video_item['project']}) project_id = project.id else: project_id = None team_video = TeamVideo.objects.create(video=video, team=team, project_id=project_id) else: messages.append( fmt(_(u'You are not authorized to perform such action\n'))) messages.append( fmt(_(u"Number of videos added to team: %(num)i\n"), num=num_successful_videos)) domain = Site.objects.get_current().domain context = { 'domain': domain, 'user': user, 'team': team, 'messages': messages, "STATIC_URL": settings.STATIC_URL, } send_templated_email(user, "Summary of videos added to team on Amara", 'teams/email_videos_added.html', context, fail_silently=not settings.DEBUG)
def _send_email(self, to_user): send_templated_email(to_user, "test email", "messages/email/email-confirmed.html", {})