def test_send_notifications_top_level_with_mention_and_notification( self, add_story, push_notification_from_signals, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() NoticeSetting.for_user( image.user, NoticeType.objects.get(label='new_comment_mention'), 1) comment = NestedCommentsGenerators.comment( author=commenter, target=image, text='[quote="%s"]Foo[/quote]' % image.user.username) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment', mock.ANY) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment_reply', mock.ANY) push_notification_from_signals.assert_called_with( [image.user], commenter, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_setting_default_notice_settings_for_a_user(self): """ Test class method for saving default settings for a user. """ NoticeSetting.set_user_default_notice_settings(self.user,0) obj_from_db_for_user = NoticeSetting.objects.filter(user = self.user) self.assertEqual(len(obj_from_db_for_user),2)
def get_queryset(self): notice_types = NoticeType.objects.all() for notice_type in notice_types: for medium_id, medium_display in NOTICE_MEDIA: # This will create it with default values if it doesn't exist. NoticeSetting.for_user(self.request.user, notice_type, medium_id) return NoticeSetting.objects.filter(user=self.request.user)
def post_save_profile(instance, sender, created, **kwargs): """ On new profile, remove all notifications and set only the needed, on profile update check if the password is changed and notify the user """ if created: for notice in NoticeType.objects.filter(label__in=PROFILE_NOTICE_SETTINGS): NoticeSetting.for_user(instance, notice, NOTICE_MEDIA[0][0]) NoticeSetting.objects.filter(user=instance).exclude(notice_type__label__in=[ 'password_updated', 'message_received']).update(send=False)
def test_get_mentioned_users_with_notification_enabled_users_with_notifications_enabled(self): user = Generators.user() notice_type = NoticeType.objects.create( label='new_forum_post_mention', display='', description='', default=2) NoticeSetting.for_user(user, notice_type, 1) self.assertEqual( [user], MentionsService.get_mentioned_users_with_notification_enabled([user.username], 'new_forum_post_mention'))
def test_setting_default_notice_settings_correct_notices(self): """ Test class method for saving default settings for a user. Make sure right notices have been saved. """ NoticeSetting.set_user_default_notice_settings(self.user,0) obj_from_db_for_user = NoticeSetting.objects.filter(user = self.user).order_by('id') for notice_setting in obj_from_db_for_user: self.assertEqual(notice_setting.user,self.user) self.assertEqual(notice_setting.send,True)
def can_send(self, user, notice_type): """ Determines whether this backend is allowed to send a notification to the given user and notice_type. """ from notification.models import NoticeSetting return NoticeSetting.for_user(user, notice_type, self.medium_id).send
def get_context_data(self, **kwargs): ctx = super(ProfileDetail, self).get_context_data(**kwargs) ctx['journal_entries'] = JournalEntry.objects.filter( author=self.object).order_by('-date') ctx['journal_entries_total'] = JournalEntry.objects.filter( author=self.object).count() ctx['journal_entries_published'] = JournalEntry.objects.filter( author=self.object, publish=True).count() ctx['favorites'] = Favorite.objects.filter( user=self.object).order_by('-created_on') ctx['icons'] = Icon.objects.filter(owner=self.object) ctx['threads_all'] = Thread.ordered(Thread.objects.inbox(self.object)) ctx['threads_unread'] = Thread.ordered( Thread.objects.unread(self.object)) ctx['action_list'] = actor_stream(ctx['profile']) # need to render the form ctx['form'] = UploadFileForm() notice_settings = [] for notice in NoticeType.objects.filter( label__in=PROFILE_NOTICE_SETTINGS): notice_settings.append( NoticeSetting.for_user(self.object, notice, NOTICE_MEDIA[0][0])) ctx['notice_settings'] = notice_settings return ctx
def notice_settings(request): """ The notice settings view. Template: :template:`notification/notice_settings.html` Context: notice_types A list of all :model:`notification.NoticeType` objects. notice_settings A dictionary containing ``column_headers`` for each ``NOTICE_MEDIA`` and ``rows`` containing a list of dictionaries: ``notice_type``, a :model:`notification.NoticeType` object and ``cells``, a list of tuples whose first value is suitable for use in forms and the second value is ``True`` or ``False`` depending on a ``request.POST`` variable called ``form_label``, whose valid value is ``on``. """ notice_types = NoticeType.objects.all() settings_table = [] notice_media = [] for medium_id, medium_display in NOTICE_MEDIA: if medium_display in NOTIFICATION_UNCONFIGURABLE_MEDIA: continue else: notice_media.append((medium_id, medium_display)) for notice_type in notice_types: settings_row = [] for medium_id, medium_display in notice_media: form_label = "%s_%s" % (notice_type.label, medium_id) setting = NoticeSetting.for_user(request.user, notice_type, medium_id) if request.method == "POST": if request.POST.get(form_label) == "on": if not setting.send: setting.send = True setting.save() else: if setting.send: setting.send = False setting.save() settings_row.append((form_label, setting.send)) settings_table.append({"notice_type": notice_type, "cells": settings_row}) if request.method == "POST": next_page = request.POST.get("next_page", ".") return HttpResponseRedirect(next_page) notice_settings = { "column_headers": [medium_display for medium_id, medium_display in notice_media], "rows": settings_table, } return render_to_response("notification/notice_settings.html", { "notice_types": notice_types, "notice_settings": notice_settings, }, context_instance=RequestContext(request))
def notice_settings(request): """ The notice settings view. Template: :template:`notification/notice_settings.html` Context: notice_types A list of all :model:`notification.NoticeType` objects. notice_settings A dictionary containing ``column_headers`` for each ``NOTICE_MEDIA`` and ``rows`` containing a list of dictionaries: ``notice_type``, a :model:`notification.NoticeType` object and ``cells``, a list of tuples whose first value is suitable for use in forms and the second value is ``True`` or ``False`` depending on a ``request.POST`` variable called ``form_label``, whose valid value is ``on``. """ notice_types = NoticeType.objects.all() settings_table = [] for notice_type in notice_types: settings_row = [] for medium_id, medium_display in NOTICE_MEDIA: form_label = "%s_%s" % (notice_type.label, medium_id) setting = NoticeSetting.for_user(request.user, notice_type, medium_id) if request.method == "POST": if request.POST.get(form_label) == "on": if not setting.send: setting.send = True setting.save() else: if setting.send: setting.send = False setting.save() settings_row.append((form_label, setting.send)) settings_table.append({ "notice_type": notice_type, "cells": settings_row }) if request.method == "POST": next_page = request.POST.get("next_page", ".") return HttpResponseRedirect(next_page) settings = { "column_headers": [medium_display for medium_id, medium_display in NOTICE_MEDIA], "rows": settings_table, } return render_to_response("notification/notice_settings.html", { "notice_types": notice_types, "notice_settings": settings, }, context_instance=RequestContext(request))
def test_send_notifications_reply_with_mention_and_notification( self, add_story, push_notification_from_signals, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() replier = Generators.user() NoticeSetting.for_user( commenter, NoticeType.objects.get(label='new_comment_mention'), 1) comment = NestedCommentsGenerators.comment(author=commenter, target=image) push_notification.assert_called_with([image.user], commenter, 'new_comment', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object) push_notification.reset_mock() push_notification_from_signals.reset_mock() add_story.resetMock() comment = NestedCommentsGenerators.comment( author=replier, target=image, parent=comment, text='[quote="%s"]Foo[/quote]' % commenter.username) push_notification.assert_has_calls([ mock.call([image.user], replier, 'new_comment', mock.ANY), ], any_order=True) push_notification_from_signals.assert_called_with( [commenter], replier, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def set_profile_notification(request, username): if username == request.user.username: notice_type = request.POST.get('notice_type', None) if notice_type: notice = NoticeType.objects.get(label=notice_type) setting = NoticeSetting.for_user(request.user, notice, NOTICE_MEDIA[0][0]) setting.send = json.loads(request.POST.get('send', True)) setting.save() return HttpResponse('Ok') else: return HttpResponseForbidden( 'You are not allowed to edit other users profile')
def get_context_data(self, **kwargs): ctx = super(ProfileDetail, self).get_context_data(**kwargs) ctx['diary_entries'] = DiaryEntry.objects.filter(author=self.object).order_by('-date') ctx['favorites'] = Favorite.objects.filter(user=self.object).order_by('-created_on') ctx['threads_all'] = Thread.ordered(Thread.objects.inbox(self.object)) ctx['threads_unread'] = Thread.ordered(Thread.objects.unread(self.object)) ctx['action_list'] = actor_stream(ctx['profile']) # need to render the form ctx['form'] = UploadFileForm() notice_settings = [] for notice in NoticeType.objects.filter(label__in=PROFILE_NOTICE_SETTINGS): notice_settings.append(NoticeSetting.for_user(self.object, notice, NOTICE_MEDIA[0][0])) ctx['notice_settings'] = notice_settings return ctx
def notification_settings(context): ''' Provide users notification settings for custom template rendering. ''' user = context['request'].user user_settings = [] # Get list of user's settings if user.is_authenticated(): notice_types = NoticeType.objects.all() for notice in notice_types: for media in NOTICE_MEDIA: setting = get_notification_setting(user, notice, media[0]) #setting.send = 'yes' if setting.send else '' user_settings.append(setting) # Get list of available settings else: notice_types = NoticeType.objects.all() for notice in notice_types: for media in NOTICE_MEDIA: setting = NoticeSetting(user=User(), notice_type=notice, medium=media[0]) setting.send = True user_settings.append(setting) return user_settings