Test + small changes
This commit is contained in:
parent
270e31f12b
commit
da75dc7d9c
6 changed files with 96 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.3 on 2017-07-18 13:17
|
||||
# Generated by Django 1.11.3 on 2017-07-18 15:12
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
|
@ -43,4 +43,12 @@ class Migration(migrations.Migration):
|
|||
'verbose_name': 'souscription utilisateur',
|
||||
},
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='usersubscription',
|
||||
unique_together=set([('user', 'content_type', 'object_id')]),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='groupsubscription',
|
||||
unique_together=set([('group', 'content_type', 'object_id')]),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -28,6 +28,7 @@ class UserSubscription(Subscription):
|
|||
|
||||
class Meta:
|
||||
verbose_name = _("souscription utilisateur")
|
||||
unique_together = ("user", "content_type", "object_id")
|
||||
|
||||
|
||||
class GroupSubscription(Subscription):
|
||||
|
@ -35,22 +36,29 @@ class GroupSubscription(Subscription):
|
|||
|
||||
class Meta:
|
||||
verbose_name = _("souscription en groupe")
|
||||
unique_together = ("group", "content_type", "object_id")
|
||||
|
||||
|
||||
class SubscriptionMixin(models.Model):
|
||||
subscribed_users = GenericRelation(UserSubscription)
|
||||
subscribed_groups = GenericRelation(GroupSubscription)
|
||||
|
||||
def get_manual_subscribers(self):
|
||||
return self.subscribed_users.filter(is_unsub=False)
|
||||
|
||||
def get_subscribers_from_groups(self):
|
||||
return User.objects.filter(group__in=self.subscribed_groups.all())
|
||||
user_subscriptions = GenericRelation(UserSubscription)
|
||||
group_subscriptions = GenericRelation(GroupSubscription)
|
||||
|
||||
def get_all_subscribers(self):
|
||||
return (self.get_manual_subscribers()
|
||||
.union(self.get_subscribers_from_groups())
|
||||
.exclude(is_unsub=True))
|
||||
subscribed_users = User.objects.filter(
|
||||
usersubscription__in=self.user_subscriptions.filter(is_unsub=False)
|
||||
)
|
||||
subscribed_groups = Group.objects.filter(
|
||||
groupsubscription__in=self.group_subscriptions.all()
|
||||
)
|
||||
subscribers_from_groups = User.objects.filter(
|
||||
groups__in=subscribed_groups,
|
||||
).exclude(
|
||||
usersubscription__in=self.user_subscriptions.filter(
|
||||
is_unsub=True
|
||||
)
|
||||
)
|
||||
|
||||
return subscribed_users.union(subscribers_from_groups)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
|
58
communication/tests.py
Normal file
58
communication/tests.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
from django.contrib.auth.models import Group
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from .models import (UserSubscription, GroupSubscription)
|
||||
from event.models import Event
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class SubscriptionTest(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.root = User.objects.create(username='root')
|
||||
cls.user_true = User.objects.create(username='usertrue')
|
||||
cls.user_false = User.objects.create(username='userfalse')
|
||||
cls.user_group_true = User.objects.create(username='usergrouptrue')
|
||||
cls.user_group_false = User.objects.create(username='usergroupfalse')
|
||||
|
||||
cls.group = Group.objects.create(name="TestGroup")
|
||||
cls.user_group_true.groups.add(cls.group)
|
||||
cls.user_group_false.groups.add(cls.group)
|
||||
|
||||
cls.event = Event.objects.create(
|
||||
title='TestEvent',
|
||||
slug='test',
|
||||
created_by=cls.root,
|
||||
creation_date=timezone.now(),
|
||||
description="Ceci est un test",
|
||||
beginning_date=timezone.now()
|
||||
+ timedelta(days=30),
|
||||
ending_date=timezone.now()
|
||||
+ timedelta(days=31),
|
||||
)
|
||||
cls.groupsub = GroupSubscription.objects.create(
|
||||
content_object=cls.event,
|
||||
group=cls.group
|
||||
)
|
||||
cls.groupunsub = UserSubscription.objects.create(
|
||||
content_object=cls.event,
|
||||
user=cls.user_group_false,
|
||||
is_unsub=True
|
||||
)
|
||||
cls.userunsub = UserSubscription.objects.create(
|
||||
content_object=cls.event,
|
||||
user=cls.user_false,
|
||||
is_unsub=True
|
||||
)
|
||||
cls.usersub = UserSubscription.objects.create(
|
||||
content_object=cls.event,
|
||||
user=cls.user_true,
|
||||
is_unsub=False
|
||||
)
|
||||
|
||||
def test_all_subs(self):
|
||||
self.assertSetEqual(set(self.event.get_all_subscribers()),
|
||||
{self.user_true, self.user_group_true})
|
Loading…
Add table
Add a link
Reference in a new issue