Test + small changes

This commit is contained in:
Ludovic Stephan 2017-07-18 17:18:54 +02:00
parent 270e31f12b
commit da75dc7d9c
6 changed files with 96 additions and 20 deletions

View file

@ -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')]),
),
]

View file

@ -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
View 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})

View file

@ -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.db import migrations, models

View file

@ -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
@ -26,8 +26,8 @@ class Migration(migrations.Migration):
('color', models.CharField(help_text='Rentrer une couleur en hexadécimal', max_length=7, validators=[django.core.validators.RegexValidator(message="La chaîne de caractère rentrée n'est pas une couleur en hexadécimal.", regex='^#(?:[0-9a-fA-F]{3}){1,2}$')], verbose_name='couleur')),
],
options={
'verbose_name_plural': 'tags',
'verbose_name': 'tag',
'verbose_name_plural': 'tags',
},
),
migrations.CreateModel(
@ -43,8 +43,8 @@ class Migration(migrations.Migration):
('remarks', models.TextField(blank=True, help_text='Visible uniquement par les organisateurs', null=True, verbose_name='remarques')),
],
options={
'verbose_name_plural': 'templates activité',
'verbose_name': 'template activité',
'verbose_name_plural': 'templates activité',
},
),
migrations.CreateModel(
@ -60,8 +60,8 @@ class Migration(migrations.Migration):
('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='created_events', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name_plural': 'évènements',
'verbose_name': 'évènement',
'verbose_name_plural': 'évènements',
},
),
migrations.CreateModel(
@ -72,8 +72,8 @@ class Migration(migrations.Migration):
('description', models.TextField(blank=True)),
],
options={
'verbose_name_plural': 'lieux',
'verbose_name': 'lieu',
'verbose_name_plural': 'lieux',
},
),
migrations.CreateModel(
@ -84,8 +84,8 @@ class Migration(migrations.Migration):
('end', models.DateTimeField(verbose_name='heure de fin')),
],
options={
'verbose_name_plural': 'activités',
'verbose_name': 'activité',
'verbose_name_plural': 'activités',
},
bases=('event.activitytemplate',),
),

View file

@ -1,4 +1,4 @@
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.test import TestCase
from datetime import timedelta
@ -6,6 +6,8 @@ from django.utils import timezone
from .models import Event, ActivityTemplate, Activity, Place, \
ActivityTag
User = get_user_model()
class ActivityInheritanceTest(TestCase):
@classmethod