Refactor communication models
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.
This commit is contained in:
parent
6f158638bf
commit
af75c90b84
3 changed files with 42 additions and 58 deletions
|
@ -1,60 +1,47 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
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 Notifying(models.Model):
|
||||
subscribed = models.ManyToManyField(
|
||||
User,
|
||||
verbose_name=_("abonnés"),
|
||||
related_name="subscribed_to"
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
def get_url(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
class GroupSubscription(Subscription):
|
||||
group = models.ForeignKey(Group)
|
||||
|
||||
|
||||
class Notification(models.Model):
|
||||
sent_by = models.ForeignKey(
|
||||
Notifying,
|
||||
related_name="notifs_sent"
|
||||
)
|
||||
about = models.ForeignKey(
|
||||
Notifying,
|
||||
related_name="notifs",
|
||||
)
|
||||
to = models.ForeignKey(
|
||||
User,
|
||||
verbose_name=_("envoyée à")
|
||||
)
|
||||
viewed_at = models.DateTimeField(
|
||||
_("Vue à"),
|
||||
blank=True
|
||||
)
|
||||
text = models.TextField(_("corps de la notification"))
|
||||
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)
|
||||
|
||||
class Commentable(Notifying):
|
||||
pass
|
||||
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 Thread(models.Model):
|
||||
topic = models.OneToOneField(Commentable)
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
thread = models.ForeignKey(Thread)
|
||||
parent = models.ForeignKey(
|
||||
'Comment',
|
||||
related_name="replies",
|
||||
)
|
||||
author = models.ForeignKey(
|
||||
User,
|
||||
verbose_name=_("auteur"),
|
||||
editable=False
|
||||
)
|
||||
at = models.DateTimeField(
|
||||
_("date d'écriture"),
|
||||
auto_now_add=True
|
||||
)
|
||||
text = models.TextField(_("corps du message"))
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue