Small fixes

This commit is contained in:
Ludovic Stephan 2017-07-18 13:54:11 +02:00
parent af75c90b84
commit 19093d01f3
2 changed files with 70 additions and 5 deletions

View file

@ -1,5 +1,4 @@
from django.db import models from django.db import models
from django.conf import settings
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import ( from django.contrib.contenttypes.fields import (
@ -16,31 +15,41 @@ class Subscription(models.Model):
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id') content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
abstract = True
class UserSubscription(Subscription): class UserSubscription(Subscription):
user = models.ForeignKey(settings.AUTH_USER_MODEL) user = models.ForeignKey(User)
is_unsub = models.BooleanField( is_unsub = models.BooleanField(
_("désinscription"), _("désinscription"),
default=False default=False
) )
class Meta:
verbose_name = _("souscription utilisateur")
class GroupSubscription(Subscription): class GroupSubscription(Subscription):
group = models.ForeignKey(Group) group = models.ForeignKey(Group)
class Meta:
verbose_name = _("souscription en groupe")
class SubscriptionMixin(models.Model): class SubscriptionMixin(models.Model):
subscribed_users = GenericRelation(UserSubscription) subscribed_users = GenericRelation(UserSubscription)
subscribed_groups = GenericRelation(GroupSubscription) subscribed_groups = GenericRelation(GroupSubscription)
def get_unique_users(self): def get_manual_subscribers(self):
return self.subscribed_users.filter(is_unsub=False) return self.subscribed_users.filter(is_unsub=False)
def get_group_users(self): def get_subscribers_from_groups(self):
return User.objects.filter(group__in=self.subscribed_groups.all()) return User.objects.filter(group__in=self.subscribed_groups.all())
def get_all_subscribers(self): def get_all_subscribers(self):
return (self.get_unique_users().union(self.get_group_users()) return (self.get_manual_subscribers()
.union(self.get_subscribers_from_groups())
.exclude(is_unsub=True)) .exclude(is_unsub=True))
class Meta: class Meta:

56
vim Normal file
View file

@ -0,0 +1,56 @@
diff --git a/communication/models.py b/communication/models.py
index 220d29d..8a89504 100644
--- a/communication/models.py
+++ b/communication/models.py
@@ -1,5 +1,4 @@
from django.db import models
-from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import (
@@ -16,31 +15,41 @@ class Subscription(models.Model):
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
+ class Meta:
+ abstract = True
+
class UserSubscription(Subscription):
- user = models.ForeignKey(settings.AUTH_USER_MODEL)
+ user = models.ForeignKey(User)
is_unsub = models.BooleanField(
_("désinscription"),
default=False
)
+ class Meta:
+ verbose_name = _("souscription utilisateur")
+
class GroupSubscription(Subscription):
group = models.ForeignKey(Group)
+ class Meta:
+ verbose_name = _("souscription en groupe")
+
class SubscriptionMixin(models.Model):
subscribed_users = GenericRelation(UserSubscription)
subscribed_groups = GenericRelation(GroupSubscription)
- def get_unique_users(self):
+ def get_manual_subscribers(self):
return self.subscribed_users.filter(is_unsub=False)
- def get_group_users(self):
+ def get_subscribers_from_groups(self):
return User.objects.filter(group__in=self.subscribed_groups.all())
def get_all_subscribers(self):
- return (self.get_unique_users().union(self.get_group_users())
+ return (self.get_manual_subscribers()
+ .union(self.get_subscribers_from_groups())
.exclude(is_unsub=True))
class Meta: