diff --git a/circle/dashboard/forms.py b/circle/dashboard/forms.py index 4fb8df1..51cd862 100644 --- a/circle/dashboard/forms.py +++ b/circle/dashboard/forms.py @@ -1223,7 +1223,7 @@ class MyProfileForm(forms.ModelForm): class Meta: fields = ('preferred_language', 'email_notifications', - 'use_gravatar', ) + 'desktop_notifications', 'use_gravatar', ) model = Profile @property diff --git a/circle/dashboard/migrations/0004_profile_desktop_notifications.py b/circle/dashboard/migrations/0004_profile_desktop_notifications.py new file mode 100644 index 0000000..15e68d4 --- /dev/null +++ b/circle/dashboard/migrations/0004_profile_desktop_notifications.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dashboard', '0003_message'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='desktop_notifications', + field=models.BooleanField(default=False, help_text='Whether user wants to get desktop notification when an activity has finished and the window not in focus.', verbose_name='Desktop notifications'), + ), + ] diff --git a/circle/dashboard/models.py b/circle/dashboard/models.py index 75a35f7..2c87669 100644 --- a/circle/dashboard/models.py +++ b/circle/dashboard/models.py @@ -184,6 +184,10 @@ class Profile(Model): email_notifications = BooleanField( verbose_name=_("Email notifications"), default=True, help_text=_('Whether user wants to get digested email notifications.')) + desktop_notifications = BooleanField( + verbose_name=_("Desktop notifications"), default=False, + help_text=_('Whether user wants to get desktop notification when ' + 'an activity has finished and the window not in focus.')) smb_password = CharField( max_length=20, verbose_name=_('Samba password'), diff --git a/circle/dashboard/static/dashboard/activity.js b/circle/dashboard/static/dashboard/activity.js index c3d10dd..463d542 100644 --- a/circle/dashboard/static/dashboard/activity.js +++ b/circle/dashboard/static/dashboard/activity.js @@ -169,7 +169,7 @@ $(function() { ); } else { in_progress = false; - if(windowHasFocus === false){ + if(windowHasFocus === false && userWantNotifications()){ sendNotification(generateMessageFromLastActivity()); } if(reload_vm_detail) location.reload(); @@ -186,7 +186,8 @@ $(function() { // Notification init $(function(){ - Notification.requestPermission(); + if(userWantNotifications()) + Notification.requestPermission(); }); // Detect window has focus @@ -200,10 +201,10 @@ $(window).focus(function(){ function generateMessageFromLastActivity(){ var ac = $('div.activity').first(); - if(ac.length === 0) return ""; - var error = $(ac[0]).children(".timeline-icon-failed").length; + var error = ac.children(".timeline-icon-failed").length; var sign = (error === 1) ? "❌ " : "✓ "; - return sign + ac[0].innerText.split(",")[0]; + var msg = ac.children("strong").text().trim(); + return sign + msg; } function sendNotification(message) { @@ -219,6 +220,11 @@ function sendNotification(message) { } } +function userWantNotifications(){ + var dn = $("#user-options").data("desktop_notifications"); + return dn === "True"; +} + function addConnectText() { var activities = $(".timeline .activity"); if(activities.length > 1) { @@ -229,7 +235,6 @@ function addConnectText() { } } - String.prototype.hashCode = function() { var hash = 0, i, chr, len; if (this.length === 0) return hash; diff --git a/circle/dashboard/templates/dashboard/_display-name.html b/circle/dashboard/templates/dashboard/_display-name.html index c315f81..0f58c31 100644 --- a/circle/dashboard/templates/dashboard/_display-name.html +++ b/circle/dashboard/templates/dashboard/_display-name.html @@ -15,4 +15,6 @@ {% endif %} {% endif %} + <span id="user-options" data-desktop_notifications="{{ user.profile.desktop_notifications }}"><span> + {% endif %}