Adding live notifications configuration

remotes/origin/3.4.0rc
Jesús Espino 2017-03-31 15:11:48 +02:00 committed by Alex Hermida
parent d74094295c
commit b6b7ff6d9b
5 changed files with 47 additions and 13 deletions

View File

@ -32,5 +32,5 @@ class WatchedInline(GenericTabularInline):
class NotifyPolicyInline(TabularInline): class NotifyPolicyInline(TabularInline):
model = models.NotifyPolicy model = models.NotifyPolicy
extra = 0 extra = 0
readonly_fields = ("notify_level",) readonly_fields = ("notify_level", "live_notify_level")
raw_id_fields = ["user"] raw_id_fields = ["user"]

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-03-31 13:03
from __future__ import unicode_literals
from django.db import migrations, models
import taiga.projects.notifications.choices
class Migration(migrations.Migration):
dependencies = [
('notifications', '0006_auto_20151103_0954'),
]
operations = [
migrations.AddField(
model_name='notifypolicy',
name='live_notify_level',
field=models.SmallIntegerField(choices=[(taiga.projects.notifications.choices.NotifyLevel(1), 'Involved'), (taiga.projects.notifications.choices.NotifyLevel(2), 'All'), (taiga.projects.notifications.choices.NotifyLevel(3), 'None')], default=taiga.projects.notifications.choices.NotifyLevel(1)),
),
]

View File

@ -36,6 +36,7 @@ class NotifyPolicy(models.Model):
project = models.ForeignKey("projects.Project", related_name="notify_policies") project = models.ForeignKey("projects.Project", related_name="notify_policies")
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="notify_policies") user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="notify_policies")
notify_level = models.SmallIntegerField(choices=NOTIFY_LEVEL_CHOICES) notify_level = models.SmallIntegerField(choices=NOTIFY_LEVEL_CHOICES)
live_notify_level = models.SmallIntegerField(choices=NOTIFY_LEVEL_CHOICES, default=NotifyLevel.involved)
created_at = models.DateTimeField(default=timezone.now) created_at = models.DateTimeField(default=timezone.now)
modified_at = models.DateTimeField() modified_at = models.DateTimeField()

View File

@ -27,7 +27,7 @@ class NotifyPolicySerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = models.NotifyPolicy model = models.NotifyPolicy
fields = ('id', 'project', 'project_name', 'notify_level') fields = ('id', 'project', 'project_name', 'notify_level', "live_notify_level")
def get_project_name(self, obj): def get_project_name(self, obj):
return obj.project.name return obj.project.name

View File

@ -55,7 +55,7 @@ def notify_policy_exists(project, user) -> bool:
return qs.exists() return qs.exists()
def create_notify_policy(project, user, level=NotifyLevel.involved): def create_notify_policy(project, user, level=NotifyLevel.involved, live_level=NotifyLevel.all):
""" """
Given a project and user, create notification policy for it. Given a project and user, create notification policy for it.
""" """
@ -63,12 +63,13 @@ def create_notify_policy(project, user, level=NotifyLevel.involved):
try: try:
return model_cls.objects.create(project=project, return model_cls.objects.create(project=project,
user=user, user=user,
notify_level=level) notify_level=level,
live_notify_level=live_level)
except IntegrityError as e: except IntegrityError as e:
raise exc.IntegrityError(_("Notify exists for specified user and project")) from e raise exc.IntegrityError(_("Notify exists for specified user and project")) from e
def create_notify_policy_if_not_exists(project, user, level=NotifyLevel.involved): def create_notify_policy_if_not_exists(project, user, level=NotifyLevel.involved, live_level=NotifyLevel.all):
""" """
Given a project and user, create notification policy for it. Given a project and user, create notification policy for it.
""" """
@ -76,7 +77,8 @@ def create_notify_policy_if_not_exists(project, user, level=NotifyLevel.involved
try: try:
result = model_cls.objects.get_or_create(project=project, result = model_cls.objects.get_or_create(project=project,
user=user, user=user,
defaults={"notify_level": level}) defaults={"notify_level": level,
"live_notify_level": live_level})
return result[0] return result[0]
except IntegrityError as e: except IntegrityError as e:
raise exc.IntegrityError(_("Notify exists for specified user and project")) from e raise exc.IntegrityError(_("Notify exists for specified user and project")) from e
@ -134,7 +136,7 @@ def _filter_notificable(user):
return user.is_active and not user.is_system return user.is_active and not user.is_system
def get_users_to_notify(obj, *, history=None, discard_users=None) -> list: def get_users_to_notify(obj, *, history=None, discard_users=None, live=False) -> list:
""" """
Get filtered set of users to notify for specified Get filtered set of users to notify for specified
model instance and changer. model instance and changer.
@ -146,6 +148,8 @@ def get_users_to_notify(obj, *, history=None, discard_users=None) -> list:
def _check_level(project: object, user: object, levels: tuple) -> bool: def _check_level(project: object, user: object, levels: tuple) -> bool:
policy = project.cached_notify_policy_for_user(user) policy = project.cached_notify_policy_for_user(user)
if live:
return policy.live_notify_level in levels
return policy.notify_level in levels return policy.notify_level in levels
_can_notify_hard = partial(_check_level, project, _can_notify_hard = partial(_check_level, project,
@ -235,7 +239,8 @@ def send_notifications(obj, *, history):
if settings.CHANGE_NOTIFICATIONS_MIN_INTERVAL == 0: if settings.CHANGE_NOTIFICATIONS_MIN_INTERVAL == 0:
send_sync_notifications(notification.id) send_sync_notifications(notification.id)
for user in notify_users: live_notify_users = get_users_to_notify(obj, history=history, discard_users=[notification.owner], live=True)
for user in live_notify_users:
events.emit_live_notification_for_model(obj, user, history) events.emit_live_notification_for_model(obj, user, history)
@ -420,7 +425,11 @@ def add_watcher(obj, user):
project=obj.project) project=obj.project)
notify_policy, _ = apps.get_model("notifications", "NotifyPolicy").objects.get_or_create( notify_policy, _ = apps.get_model("notifications", "NotifyPolicy").objects.get_or_create(
project=obj.project, user=user, defaults={"notify_level": NotifyLevel.involved}) project=obj.project,
user=user,
defaults={"notify_level": NotifyLevel.involved,
"live_notify_level": NotifyLevel.involved}
)
return watched return watched
@ -442,22 +451,25 @@ def remove_watcher(obj, user):
qs.delete() qs.delete()
def set_notify_policy_level(notify_policy, notify_level): def set_notify_policy_level(notify_policy, notify_level, live=False):
""" """
Set notification level for specified policy. Set notification level for specified policy.
""" """
if notify_level not in [e.value for e in NotifyLevel]: if notify_level not in [e.value for e in NotifyLevel]:
raise exc.IntegrityError(_("Invalid value for notify level")) raise exc.IntegrityError(_("Invalid value for notify level"))
notify_policy.notify_level = notify_level if live:
notify_policy.live_notify_level = notify_level
else:
notify_policy.notify_level = notify_level
notify_policy.save() notify_policy.save()
def set_notify_policy_level_to_ignore(notify_policy): def set_notify_policy_level_to_ignore(notify_policy, live=False):
""" """
Set notification level for specified policy. Set notification level for specified policy.
""" """
set_notify_policy_level(notify_policy, NotifyLevel.none) set_notify_policy_level(notify_policy, NotifyLevel.none, live=live)
def make_ms_thread_index(msg_id, dt): def make_ms_thread_index(msg_id, dt):