We now use `django-notifications` and `django-contrib-comments` to manage notifications and comments. Subscriptions are managed through the `SubscriptionMixin` model, and can correspond to unique users and to group-like subscriptions.
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
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 (
|
|
GenericForeignKey, GenericRelation
|
|
)
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class Subscription(models.Model):
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
object_id = models.PositiveIntegerField()
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
|
|
|
|
|
class UserSubscription(Subscription):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL)
|
|
is_unsub = models.BooleanField(
|
|
_("désinscription"),
|
|
default=False
|
|
)
|
|
|
|
|
|
class GroupSubscription(Subscription):
|
|
group = models.ForeignKey(Group)
|
|
|
|
|
|
class SubscriptionMixin(models.Model):
|
|
subscribed_users = GenericRelation(UserSubscription)
|
|
subscribed_groups = GenericRelation(GroupSubscription)
|
|
|
|
def get_unique_users(self):
|
|
return self.subscribed_users.filter(is_unsub=False)
|
|
|
|
def get_group_users(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())
|
|
.exclude(is_unsub=True))
|
|
|
|
class Meta:
|
|
abstract = True
|