users can disable notifications
This commit is contained in:
parent
4a61d463bd
commit
f420dd98ac
9 changed files with 82 additions and 22 deletions
|
@ -7,7 +7,7 @@ from accounts.models import *
|
||||||
__all__ = ['UserForm', 'UserFormWithoutUsername', 'ProfileForm', 'GroupForm', 'TeamForm']
|
__all__ = ['UserForm', 'UserFormWithoutUsername', 'ProfileForm', 'GroupForm', 'TeamForm']
|
||||||
|
|
||||||
|
|
||||||
user_fields=['first_name', 'last_name', 'email']
|
user_fields=['first_name', 'last_name', 'email', 'notifications']
|
||||||
|
|
||||||
UserForm = modelform_factory(User,
|
UserForm = modelform_factory(User,
|
||||||
fields=['username']+user_fields+['is_superuser'])
|
fields=['username']+user_fields+['is_superuser'])
|
||||||
|
|
20
accounts/migrations/0003_user_notifications.py
Normal file
20
accounts/migrations/0003_user_notifications.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('accounts', '0002_auto_20140905_0229'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='notifications',
|
||||||
|
field=models.IntegerField(choices=[(0, 'Never'), (1, 'Ignore my actions'), (2, 'Always')], default=1),
|
||||||
|
preserve_default=True,
|
||||||
|
),
|
||||||
|
]
|
|
@ -14,6 +14,18 @@ class User(AbstractUser):
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['username']
|
ordering = ['username']
|
||||||
|
|
||||||
|
NOTIFICATIONS_NEVER = 0 # do not change: used as boolean value
|
||||||
|
NOTIFICATIONS_OTHERS = 1
|
||||||
|
NOTIFICATIONS_ALWAYS = 2
|
||||||
|
NOTIFICATIONS_CHOICES = (
|
||||||
|
(NOTIFICATIONS_NEVER, 'Never'),
|
||||||
|
(NOTIFICATIONS_OTHERS, 'Ignore my actions'),
|
||||||
|
(NOTIFICATIONS_ALWAYS, 'Always'),
|
||||||
|
)
|
||||||
|
|
||||||
|
notifications = models.IntegerField(choices=NOTIFICATIONS_CHOICES,
|
||||||
|
default=NOTIFICATIONS_OTHERS)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def teams(self):
|
def teams(self):
|
||||||
query = Q(groups__in=self.groups.all()) | Q(users=self)
|
query = Q(groups__in=self.groups.all()) | Q(users=self)
|
||||||
|
|
|
@ -20,6 +20,7 @@ class TestViews(TestCase):
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
response = self.client.post(reverse('profile'), {
|
response = self.client.post(reverse('profile'), {
|
||||||
'first_name': 'newfirstname',
|
'first_name': 'newfirstname',
|
||||||
|
'notifications': User.NOTIFICATIONS_OTHERS,
|
||||||
}, follow=True)
|
}, follow=True)
|
||||||
self.assertRedirects(response, reverse('profile'))
|
self.assertRedirects(response, reverse('profile'))
|
||||||
self.assertContains(response, 'successfully')
|
self.assertContains(response, 'successfully')
|
||||||
|
@ -46,6 +47,7 @@ class TestViews(TestCase):
|
||||||
user_count = User.objects.count()
|
user_count = User.objects.count()
|
||||||
response = self.client.post(reverse('add-user'), {
|
response = self.client.post(reverse('add-user'), {
|
||||||
'username': 'newuser',
|
'username': 'newuser',
|
||||||
|
'notifications': User.NOTIFICATIONS_OTHERS,
|
||||||
})
|
})
|
||||||
self.assertEqual(User.objects.count(), user_count + 1)
|
self.assertEqual(User.objects.count(), user_count + 1)
|
||||||
user = User.objects.get(username='newuser')
|
user = User.objects.get(username='newuser')
|
||||||
|
@ -63,6 +65,7 @@ class TestViews(TestCase):
|
||||||
response = self.client.post(reverse('edit-user', args=[user.id]), {
|
response = self.client.post(reverse('edit-user', args=[user.id]), {
|
||||||
'username': user.username,
|
'username': user.username,
|
||||||
'first_name': 'newfirstname',
|
'first_name': 'newfirstname',
|
||||||
|
'notifications': User.NOTIFICATIONS_OTHERS,
|
||||||
})
|
})
|
||||||
self.assertRedirects(response, reverse('show-user', args=[user.id]))
|
self.assertRedirects(response, reverse('show-user', args=[user.id]))
|
||||||
user = User.objects.get(pk=user.pk)
|
user = User.objects.get(pk=user.pk)
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
{% endcomment %}
|
{% endcomment %}
|
||||||
|
|
||||||
{% if request.user.is_authenticated %}
|
{% if request.user.is_authenticated %}
|
||||||
{% if request.user.email and request.user in project.subscribers.all %}
|
{% if request.user.email and request.user.notifications and request.user in project.subscribers.all %}
|
||||||
<li><a href="{% url 'unsubscribe-project' project.name %}?next={{ request.get_full_path }}"><span class="glyphicon glyphicon-eye-close"></span> Unwatch</a></li>
|
<li><a href="{% url 'unsubscribe-project' project.name %}?next={{ request.get_full_path }}"><span class="glyphicon glyphicon-eye-close"></span> Unwatch</a></li>
|
||||||
{% else %}
|
{% else %}
|
||||||
<li><a href="{% url 'subscribe-project' project.name %}?next={{ request.get_full_path }}"><span class="glyphicon glyphicon-eye-open"></span> Watch</a></li>
|
<li><a href="{% url 'subscribe-project' project.name %}?next={{ request.get_full_path }}"><span class="glyphicon glyphicon-eye-open"></span> Watch</a></li>
|
||||||
|
|
|
@ -193,15 +193,13 @@
|
||||||
</h5>
|
</h5>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<span style="display: inline-block; margin-left: 14px;"></span>
|
<span style="display: inline-block; margin-left: 14px;"></span>
|
||||||
{% if request.user.email and request.user in project.subscribers.all %}
|
{% if request.user.email and request.user.notifications and request.user in project.subscribers.all %}
|
||||||
Subscribed to the project
|
Subscribed to the project
|
||||||
{% else %}
|
{% elif request.user.notifications and request.user in issue.subscribers.all %}
|
||||||
{% if request.user in issue.subscribers.all %}
|
|
||||||
<a href="{% url 'unsubscribe-issue' project.name issue.id %}" class="btn btn-default"><span class="glyphicon glyphicon-eye-close"></span> Unsubscribe</a>
|
<a href="{% url 'unsubscribe-issue' project.name issue.id %}" class="btn btn-default"><span class="glyphicon glyphicon-eye-close"></span> Unsubscribe</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="{% url 'subscribe-issue' project.name issue.id %}" class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> Subscribe</a>
|
<a href="{% url 'subscribe-issue' project.name issue.id %}" class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> Subscribe</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -9,6 +9,8 @@ if 'djcelery' in settings.INSTALLED_APPS:
|
||||||
else:
|
else:
|
||||||
from django.core.mail import send_mass_mail
|
from django.core.mail import send_mass_mail
|
||||||
|
|
||||||
|
from accounts.models import User
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'notify_new_issue', 'notify_new_comment',
|
'notify_new_issue', 'notify_new_comment',
|
||||||
|
@ -31,9 +33,11 @@ def notify_new_issue(issue):
|
||||||
data = []
|
data = []
|
||||||
for dest in dests:
|
for dest in dests:
|
||||||
|
|
||||||
if dest == issue.author:
|
if dest.notifications == User.NOTIFICATIONS_NEVER:
|
||||||
|
continue
|
||||||
|
if dest == issue.author \
|
||||||
|
and dest.notifications == User.NOTIFICATIONS_OTHERS:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
dest_addr = dest.email
|
dest_addr = dest.email
|
||||||
if not dest_addr:
|
if not dest_addr:
|
||||||
continue
|
continue
|
||||||
|
@ -87,9 +91,11 @@ def notify_event(event, template):
|
||||||
|
|
||||||
for dest in dests:
|
for dest in dests:
|
||||||
|
|
||||||
if dest == event.author:
|
if dest.notifications == User.NOTIFICATIONS_NEVER:
|
||||||
|
continue
|
||||||
|
if dest == issue.author \
|
||||||
|
and dest.notifications == User.NOTIFICATIONS_OTHERS:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
dest_addr = dest.email
|
dest_addr = dest.email
|
||||||
if not dest_addr:
|
if not dest_addr:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -127,9 +127,16 @@ class TestViews(TestCase):
|
||||||
response = self.client.get(reverse('unsubscribe-project', args=[project.name]), follow=True)
|
response = self.client.get(reverse('unsubscribe-project', args=[project.name]), follow=True)
|
||||||
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
||||||
self.assertContains(response, 'not subscribed to this project')
|
self.assertContains(response, 'not subscribed to this project')
|
||||||
response = self.client.get(reverse('subscribe-project', args=[project.name]))
|
response = self.client.get(reverse('subscribe-project', args=[project.name]), follow=True)
|
||||||
self.assertRedirects(response, reverse('profile'))
|
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
||||||
|
self.assertContains(response, 'must set an email')
|
||||||
user.email = 'user@example.com'
|
user.email = 'user@example.com'
|
||||||
|
user.notifications = User.NOTIFICATIONS_NEVER
|
||||||
|
user.save()
|
||||||
|
response = self.client.get(reverse('subscribe-project', args=[project.name]), follow=True)
|
||||||
|
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
||||||
|
self.assertContains(response, 'must enable notifications')
|
||||||
|
user.notifications = User.NOTIFICATIONS_OTHERS
|
||||||
user.save()
|
user.save()
|
||||||
response = self.client.get(reverse('subscribe-project', args=[project.name]))
|
response = self.client.get(reverse('subscribe-project', args=[project.name]))
|
||||||
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
self.assertRedirects(response, reverse('list-issue', args=[project.name]))
|
||||||
|
@ -343,9 +350,16 @@ class TestViews(TestCase):
|
||||||
response = self.client.get(reverse('unsubscribe-issue', args=[project.name, issue.id]), follow=True)
|
response = self.client.get(reverse('unsubscribe-issue', args=[project.name, issue.id]), follow=True)
|
||||||
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
||||||
self.assertContains(response, 'not subscribed to this issue')
|
self.assertContains(response, 'not subscribed to this issue')
|
||||||
response = self.client.get(reverse('subscribe-issue', args=[project.name, issue.id]))
|
response = self.client.get(reverse('subscribe-issue', args=[project.name, issue.id]), follow=True)
|
||||||
self.assertRedirects(response, reverse('profile'))
|
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
||||||
|
self.assertContains(response, 'must set an email')
|
||||||
user.email = 'user@example.com'
|
user.email = 'user@example.com'
|
||||||
|
user.notifications = User.NOTIFICATIONS_NEVER
|
||||||
|
user.save()
|
||||||
|
response = self.client.get(reverse('subscribe-issue', args=[project.name, issue.id]), follow=True)
|
||||||
|
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
||||||
|
self.assertContains(response, 'must enable notifications')
|
||||||
|
user.notifications = User.NOTIFICATIONS_OTHERS
|
||||||
user.save()
|
user.save()
|
||||||
response = self.client.get(reverse('subscribe-issue', args=[project.name, issue.id]))
|
response = self.client.get(reverse('subscribe-issue', args=[project.name, issue.id]))
|
||||||
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
self.assertRedirects(response, reverse('show-issue', args=[project.name, issue.id]))
|
||||||
|
|
|
@ -4,6 +4,7 @@ from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
from tracker.forms import *
|
from tracker.forms import *
|
||||||
from tracker.models import *
|
from tracker.models import *
|
||||||
|
@ -130,10 +131,14 @@ def project_delete(request, project):
|
||||||
def project_subscribe(request, project):
|
def project_subscribe(request, project):
|
||||||
|
|
||||||
if not request.user.email:
|
if not request.user.email:
|
||||||
messages.error(request, 'You must set an email address in order to receive notifications.')
|
messages.error(request, 'You must set an email address in your '
|
||||||
return redirect('profile')
|
'<a href="' + reverse('profile') + '">profile</a> in order '
|
||||||
|
'to watch this project.')
|
||||||
if project.subscribers.filter(pk=request.user.pk).exists():
|
elif request.user.notifications == User.NOTIFICATIONS_NEVER:
|
||||||
|
messages.error(request, 'You must enable notifications in your '
|
||||||
|
'<a href="' + reverse('profile') + '">profile</a> in order '
|
||||||
|
'to watch this project.')
|
||||||
|
elif project.subscribers.filter(pk=request.user.pk).exists():
|
||||||
messages.warning(request,
|
messages.warning(request,
|
||||||
'You are already subscribed to this project.')
|
'You are already subscribed to this project.')
|
||||||
else:
|
else:
|
||||||
|
@ -544,10 +549,12 @@ def issue_subscribe(request, project, issue):
|
||||||
issue = get_object_or_404(Issue, project=project, id=issue)
|
issue = get_object_or_404(Issue, project=project, id=issue)
|
||||||
|
|
||||||
if not request.user.email:
|
if not request.user.email:
|
||||||
messages.error(request, 'You must set an email address in order to receive notifications.')
|
messages.error(request, 'You must set an email address in your '
|
||||||
return redirect('profile')
|
'<a href="' + reverse('profile') + '">profile</a> to subscribe.')
|
||||||
|
elif request.user.notifications == User.NOTIFICATIONS_NEVER:
|
||||||
if issue.subscribers.filter(pk=request.user.pk).exists():
|
messages.error(request, 'You must enable notifications in your '
|
||||||
|
'<a href="' + reverse('profile') + '">profile</a> to subscribe.')
|
||||||
|
elif issue.subscribers.filter(pk=request.user.pk).exists():
|
||||||
messages.warning(request, 'You are already subscribed to this issue.')
|
messages.warning(request, 'You are already subscribed to this issue.')
|
||||||
else:
|
else:
|
||||||
issue.subscribers.add(request.user)
|
issue.subscribers.add(request.user)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue