def set_default_notice_settings(user): # Add openaction backend for sending notices to the user. # when a User is activated # Note: we need to get the index of the backands list because # 'notification' needs the index, not the name medium = settings.NOTIFICATION_BACKENDS.index(("openaction", "oa_notification.backends.openaction.OpenActionDefaultBackend")) for notice_type in notification.NoticeType.objects.all(): notification.get_notification_setting(user=user, notice_type=notice_type, medium=medium )
def set_default_notice_settings(user): # Add openaction backend for sending notices to the user. # when a User is activated # Note: we need to get the index of the backands list because # 'notification' needs the index, not the name medium = settings.NOTIFICATION_BACKENDS.index( ("openaction", "oa_notification.backends.openaction.OpenActionDefaultBackend")) for notice_type in notification.NoticeType.objects.all(): notification.get_notification_setting(user=user, notice_type=notice_type, medium=medium)
def test_bulk_public_admin(self): """ A POST to the index with the formset data and some playlists marked with the bulk option and the 'public' action should mark those videos as public if the user is an admin. """ notice_type = notification.NoticeType.objects.get( label='admin_new_playlist') setting = notification.get_notification_setting( User.objects.get(username='******'), notice_type, "1") setting.send = True setting.save() url = reverse('localtv_playlist_index') + '?show=all' c = Client() c.login(username='******', password='******') response = c.post(url, { 'form-INITIAL_FORMS': 1, 'form-TOTAL_FORMS': 1, 'form-0-id': self.list.pk, 'form-0-BULK': 'yes', 'bulk_action': 'public'}) self.assertStatusCodeEquals(response, 302) self.assertEquals(response['Location'], 'http://%s%s' % ( self.site_location.site.domain, reverse('localtv_playlist_index'))) playlist = Playlist.objects.get(pk=self.list.pk) self.assertEquals(playlist.status, PLAYLIST_STATUS_PUBLIC) self.assertEquals(len(mail.outbox), 0)
def test_POST_succeed_email(self): """ If the POST to the view succeeds and admins are subscribed to the 'admin_new_submission' notice, an e-mail should be sent to the site admins. """ notice_type = notification.NoticeType.objects.get( label='admin_new_submission') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = True setting.save() c = Client() c.post(self.url, self.POST_data) video = models.Video.objects.all()[0] self.assertEquals(len(mail.outbox), 1) message = mail.outbox[0] for recipient in message.to: u = User.objects.get(email=recipient) self.assertTrue(self.site_location.user_is_admin(u)) self.assertEquals(message.subject, '[%s] New Video in Review Queue: %s' % ( video.site.name, video)) t = loader.get_template('localtv/submit_video/new_video_email.txt') c = Context({'video': video}) self.assertEquals(message.body, t.render(c))
def test_public_admin(self): """ The localtv_playlist_public view should set the playlist's status to Playlist.WAITING_FOR_MODERATION and send a notification e-mail. """ notice_type = notification.NoticeType.objects.get( label='admin_new_playlist') setting = notification.get_notification_setting( User.objects.get(username='******'), notice_type, "1") setting.send = True setting.save() url = reverse('localtv_playlist_public', args=(self.list.pk,)) c = Client() c.login(username='******', password='******') response = c.post(url, {}) self.assertStatusCodeEquals(response, 302) self.assertEqual(response['Location'], 'http://%s%s?show=all' % ( self.site_location.site.domain, reverse('localtv_playlist_index'))) playlist = Playlist.objects.get(pk=self.list.pk) self.assertEqual(playlist.status, Playlist.PUBLIC) self.assertEqual(len(mail.outbox), 0)
def test_bulk_public_admin(self): """ A POST to the index with the formset data and some playlists marked with the bulk option and the 'public' action should mark those videos as public if the user is an admin. """ notice_type = notification.NoticeType.objects.get( label='admin_new_playlist') setting = notification.get_notification_setting( User.objects.get(username='******'), notice_type, "1") setting.send = True setting.save() url = reverse('localtv_playlist_index') + '?show=all' c = Client() c.login(username='******', password='******') response = c.post(url, { 'form-INITIAL_FORMS': 1, 'form-TOTAL_FORMS': 1, 'form-0-id': self.list.pk, 'form-0-BULK': 'yes', 'bulk_action': 'public'}) self.assertStatusCodeEquals(response, 302) self.assertEqual(response['Location'], 'http://%s%s' % ( self.site_location.site.domain, reverse('localtv_playlist_index'))) playlist = Playlist.objects.get(pk=self.list.pk) self.assertEqual(playlist.status, Playlist.PUBLIC) self.assertEqual(len(mail.outbox), 0)
def test_POST_succeed_email(self): """ If the POST to the view succeeds and admins are subscribed to the 'admin_new_submission' notice, an e-mail should be sent to the site admins. """ notice_type = notification.NoticeType.objects.get( label='admin_new_submission') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting( user, notice_type, "1") setting.send = True setting.save() c = Client() c.post(self.url, self.POST_data) video = models.Video.objects.all()[0] self.assertEquals(len(mail.outbox), 1) message = mail.outbox[0] for recipient in message.to: u = User.objects.get(email=recipient) self.assertTrue(self.site_location.user_is_admin(u)) self.assertEquals( message.subject, '[%s] New Video in Review Queue: %s' % (video.site.name, video)) t = loader.get_template('localtv/submit_video/new_video_email.txt') c = Context({'video': video}) self.assertEquals(message.body, t.render(c))
def test_public_admin(self): """ The localtv_playlist_public view should set the playlist's status to PLAYLIST_STATUS_WAITING_FOR_MODERATION and send a notification e-mail. """ notice_type = notification.NoticeType.objects.get( label='admin_new_playlist') setting = notification.get_notification_setting( User.objects.get(username='******'), notice_type, "1") setting.send = True setting.save() url = reverse('localtv_playlist_public', args=(self.list.pk,)) c = Client() c.login(username='******', password='******') response = c.post(url, {}) self.assertStatusCodeEquals(response, 302) self.assertEquals(response['Location'], 'http://%s%s?show=all' % ( self.site_location.site.domain, reverse('localtv_playlist_index'))) playlist = Playlist.objects.get(pk=self.list.pk) self.assertEquals(playlist.status, PLAYLIST_STATUS_PUBLIC) self.assertEquals(len(mail.outbox), 0)
def add_notice_for_admins(sitelocation, label): print 'adding notices for admins', label notice_type = notification.NoticeType.objects.get(label=label) for admin in sitelocation.admins.all(): setting = notification.get_notification_setting( admin, notice_type, "1") setting.send = True setting.save()
def add_notice_for_admins(sitelocation, label): print 'adding notices for admins', label notice_type = notification.NoticeType.objects.get( label=label) for admin in sitelocation.admins.all(): setting = notification.get_notification_setting( admin, notice_type, "1") setting.send = True setting.save()
def test_submit__succeed__notification(self): """ If the POST to the view succeeds, any admins who are subscribed to the 'admin_new_submission' notice should be sent an e-mail, unless the user submitting the video was an admin. """ notice_type = notification.NoticeType.objects.get( label='admin_new_submission') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = True setting.save() session = self.client.session session[SubmitURLView.session_key] = { 'video': self.session_video, 'url': self.session_url } session.save() # Case one: Non-admin. self.client.post(self.url, self.POST_data) self.assertEqual(len(Video.objects.all()), 1) video = Video.objects.all()[0] self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] for recipient in message.to: u = User.objects.get(email=recipient) self.assertTrue(self.site_settings.user_is_admin(u)) self.assertEqual(message.subject, '[%s] New Video in Review Queue: %s' % ( video.site.name, video)) t = loader.get_template('localtv/submit_video/new_video_email.txt') c = Context({'video': video}) self.assertEqual(message.body, t.render(c)) # Reset the mail outbox and session data. mail.outbox = [] session = self.client.session session[SubmitURLView.session_key] = { 'video': self.session_video, 'url': self.session_url } session.save() # Case two: admin. self.client.login(username='******', password='******') self.client.post(self.url, self.POST_data) self.assertEqual(len(mail.outbox), 0)
def setUp(self): BaseTestCase.setUp(self) notice_type = notification.NoticeType.objects.get( label='admin_queue_daily') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = True setting.save()
def save(self): if not self.instance: raise RuntimeError('Cannot save the notifications without a user.') for choice, label in self.fields['notifications'].choices: notice_type = notification.NoticeType.objects.get(label=choice) setting = notification.get_notification_setting( self.instance, notice_type, "1") setting.send = choice in self.cleaned_data['notifications'] setting.save() return self.instance
def save(self): if not self.instance: raise RuntimeError('Cannot save the notifications without a user.') for choice, label in self.fields['notifications'].choices: notice_type = notification.NoticeType.objects.get(label=choice) setting = notification.get_notification_setting(self.instance, notice_type, "1") setting.send = choice in self.cleaned_data['notifications'] setting.save() return self.instance
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 = get_notification_setting(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 }) 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 = "{0}_{1}".format(notice_type.label, medium_id) setting = get_notification_setting(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 test_POST_succeed_email_admin(self): """ If the submitting user is an admin, no e-mail should be sent. """ notice_type = notification.NoticeType.objects.get( label='admin_new_submission') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = True setting.save() c = Client() c.login(username='******', password='******') c.post(self.url, self.POST_data) self.assertEquals(len(mail.outbox), 0)
def test_POST_succeed_email_admin(self): """ If the submitting user is an admin, no e-mail should be sent. """ notice_type = notification.NoticeType.objects.get( label='admin_new_submission') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting( user, notice_type, "1") setting.send = True setting.save() c = Client() c.login(username='******', password='******') c.post(self.url, self.POST_data) self.assertEquals(len(mail.outbox), 0)
def setUp(self): admin = self.create_user(username='******', email='*****@*****.**', is_superuser=True, password='******') self.user = self.create_user(username='******', password='******') notice_type = notification.NoticeType.objects.get( label='admin_new_playlist') setting = notification.get_notification_setting(admin, notice_type, "1") setting.send = True setting.save() self.list = Playlist.objects.create( site_id=settings.SITE_ID, user=self.user, name='Test List', slug='test-list', description="This is a list for testing")
def _test_submit__succeed__notification__user(self, data): """ If the POST to the view succeeds, any admins who are subscribed to the 'admin_new_submission' notice should be sent an e-mail, unless the user submitting the video was an admin. """ self.create_user('user', 'password') site_settings = SiteSettings.objects.get_current() admin = self.create_user('admin', 'admin', email='*****@*****.**') site_settings.admins.add(admin) self._set_session(data) self.client.login(username='******', password='******') notice_type = notification.NoticeType.objects.get( label='admin_new_submission') setting = notification.get_notification_setting( admin, notice_type, "1") setting.send = True setting.save() mail.outbox = [] self.client.post(data['url'], data['POST']) self.assertEqual(len(Video.objects.all()), 1) video = Video.objects.all()[0] self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] for recipient in message.to: u = User.objects.get(email=recipient) self.assertTrue(site_settings.user_is_admin(u)) self.assertEqual( message.subject, '[%s] New Video in Review Queue: %s' % (video.site.name, video)) t = loader.get_template('localtv/submit_video/new_video_email.txt') c = Context({'video': video}) self.assertEqual(message.body, t.render(c))
def _test_submit__succeed__notification__user(self, data): """ If the POST to the view succeeds, any admins who are subscribed to the 'admin_new_submission' notice should be sent an e-mail, unless the user submitting the video was an admin. """ self.create_user('user', 'password') site_settings = SiteSettings.objects.get_current() admin = self.create_user('admin', 'admin', email='*****@*****.**') site_settings.admins.add(admin) self._set_session(data) self.client.login(username='******', password='******') notice_type = notification.NoticeType.objects.get( label='admin_new_submission') setting = notification.get_notification_setting(admin, notice_type, "1") setting.send = True setting.save() mail.outbox = [] self.client.post(data['url'], data['POST']) self.assertEqual(len(Video.objects.all()), 1) video = Video.objects.all()[0] self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] for recipient in message.to: u = User.objects.get(email=recipient) self.assertTrue(site_settings.user_is_admin(u)) self.assertEqual(message.subject, '[%s] New Video in Review Queue: %s' % ( video.site.name, video)) t = loader.get_template('localtv/submit_video/new_video_email.txt') c = Context({'video': video}) self.assertEqual(message.body, t.render(c))
def test_no_email_without_setting(self): """ If no admins are subscribed, no e-mail should be sent. """ notice_type = notification.NoticeType.objects.get( label='admin_queue_daily') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = False setting.save() queue_videos = Video.objects.filter( status=Video.UNAPPROVED) new_video = queue_videos[0] new_video.when_submitted = datetime.datetime.now() new_video.save() review_status_email.Command().handle_noargs() self.assertEqual(len(mail.outbox), 0)
def test_no_email_without_setting(self): """ If no admins are subscribed, no e-mail should be sent. """ notice_type = notification.NoticeType.objects.get( label='admin_queue_daily') for username in 'admin', 'superuser': user = User.objects.get(username=username) setting = notification.get_notification_setting(user, notice_type, "1") setting.send = False setting.save() queue_videos = models.Video.objects.filter( status=models.VIDEO_STATUS_UNAPPROVED) new_video = queue_videos[0] new_video.when_submitted = datetime.datetime.now() new_video.save() review_status_email.Command().handle_noargs() self.assertEquals(len(mail.outbox), 0)
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
def _set_notification(self, user, send): notice_type = notification.NoticeType.objects.get( label='admin_queue_daily') setting = notification.get_notification_setting(user, notice_type, "1") setting.send = send setting.save()
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 = [] changed = False settings_data = [] 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 = get_notification_setting(request.user, notice_type, medium_id) if request.method == "POST": print form_label, request.POST.get(form_label, "not_found") if request.POST.get(form_label) == "on": if not setting.send: setting.send = True setting.save() changed = True else: if setting.send: setting.send = False setting.save() changed = True settings_row.append((form_label, setting.send)) #use to determin if a notice_type is from the system or a system user notice_type.is_system = notice_type.label.find('system')+1 settings_table.append({"notice_type": notice_type, "cells": settings_row}) settings_data.append({"notice_type": notice_type.label, "cells": settings_row}) if changed: messages.add_message(request, messages.INFO, "Notification settings updated.") 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, } if request.is_ajax(): return HttpResponse(json.dumps(settings_data), content_type='application/json') return render_to_response("notification/notice_settings.html", { "notice_types": notice_types, "notice_settings": notice_settings, }, context_instance=RequestContext(request))
def _set_notification(self, user, send): notice_type = notification.NoticeType.objects.get(label='admin_queue_daily') setting = notification.get_notification_setting(user, notice_type, "1") setting.send = send setting.save()
def notice_settings(request): """ This is copied from notification.views.notice_settings in order to add code for managing subscriptions as well as notice settings. 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 = get_notification_setting(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}) notice_settings = { "column_headers": [medium_display for medium_id, medium_display in NOTICE_MEDIA], "rows": settings_table, } subscriptions = [] for sub in Subscription.objects.filter(subscriber=request.user): label = 'sub_%s' % sub.pk if request.method == "POST" and not request.POST.get(label) == "on": sub.delete() else: subscriptions.append([label, sub]) if request.method == "POST": messages.info(request, "Settings saved.") return redirect("subscriptions.settings") from_email = settings.DEFAULT_FROM_EMAIL return render(request, "notification/notice_settings.html", { "notice_types": notice_types, "notice_settings": notice_settings, "subscription_settings": subscriptions, "from_email": from_email, })
def notice_settings(request): """ This is copied from notification.views.notice_settings in order to add code for managing subscriptions as well as notice settings. 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 = get_notification_setting(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 }) notice_settings = { "column_headers": [medium_display for medium_id, medium_display in NOTICE_MEDIA], "rows": settings_table, } subscriptions = [] for sub in Subscription.objects.filter(subscriber=request.user): label = 'sub_%s' % sub.pk if request.method == "POST" and not request.POST.get(label) == "on": sub.delete() else: subscriptions.append([label, sub]) if request.method == "POST": messages.info(request, "Settings saved.") return redirect("subscriptions.settings") from_email = settings.DEFAULT_FROM_EMAIL return render( request, "notification/notice_settings.html", { "notice_types": notice_types, "notice_settings": notice_settings, "subscription_settings": subscriptions, "from_email": from_email, })