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.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 _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
class Notifying(models.Model):
|
|
||||||
subscribed = models.ManyToManyField(
|
class Subscription(models.Model):
|
||||||
User,
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
||||||
verbose_name=_("abonnés"),
|
object_id = models.PositiveIntegerField()
|
||||||
related_name="subscribed_to"
|
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):
|
class SubscriptionMixin(models.Model):
|
||||||
sent_by = models.ForeignKey(
|
subscribed_users = GenericRelation(UserSubscription)
|
||||||
Notifying,
|
subscribed_groups = GenericRelation(GroupSubscription)
|
||||||
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"))
|
|
||||||
|
|
||||||
|
def get_unique_users(self):
|
||||||
|
return self.subscribed_users.filter(is_unsub=False)
|
||||||
|
|
||||||
class Commentable(Notifying):
|
def get_group_users(self):
|
||||||
pass
|
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):
|
class Meta:
|
||||||
topic = models.OneToOneField(Commentable)
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
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"))
|
|
||||||
|
|
|
@ -3,15 +3,10 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from django.core.validators import RegexValidator
|
from django.core.validators import RegexValidator
|
||||||
from django.core.exceptions import FieldError
|
from django.core.exceptions import FieldError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from communication.models import Commentable, Notifying
|
from communication.models import SubscriptionMixin
|
||||||
|
|
||||||
|
|
||||||
class Event(Commentable):
|
class Event(SubscriptionMixin, models.Model):
|
||||||
commentable_model = models.OneToOneField(
|
|
||||||
Commentable,
|
|
||||||
related_name="event_model",
|
|
||||||
parent_link=True
|
|
||||||
)
|
|
||||||
title = models.CharField(
|
title = models.CharField(
|
||||||
_("nom de l'évènement"),
|
_("nom de l'évènement"),
|
||||||
max_length=200,
|
max_length=200,
|
||||||
|
@ -19,7 +14,6 @@ class Event(Commentable):
|
||||||
slug = models.SlugField(
|
slug = models.SlugField(
|
||||||
_('identificateur'),
|
_('identificateur'),
|
||||||
unique=True,
|
unique=True,
|
||||||
primary_key=True,
|
|
||||||
help_text=_("Seulement des lettres, des chiffres ou"
|
help_text=_("Seulement des lettres, des chiffres ou"
|
||||||
"les caractères '_' ou '-'."),
|
"les caractères '_' ou '-'."),
|
||||||
)
|
)
|
||||||
|
@ -36,7 +30,8 @@ class Event(Commentable):
|
||||||
beginning_date = models.DateTimeField(_('date de début'))
|
beginning_date = models.DateTimeField(_('date de début'))
|
||||||
ending_date = models.DateTimeField(
|
ending_date = models.DateTimeField(
|
||||||
_('date de fin'),
|
_('date de fin'),
|
||||||
blank=True
|
blank=True,
|
||||||
|
null=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -93,7 +88,7 @@ class ActivityTag(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class ActivityTemplate(Commentable):
|
class ActivityTemplate(SubscriptionMixin, models.Model):
|
||||||
title = models.CharField(
|
title = models.CharField(
|
||||||
_("nom de l'activité"),
|
_("nom de l'activité"),
|
||||||
max_length=200,
|
max_length=200,
|
||||||
|
|
|
@ -5,6 +5,8 @@ Pillow
|
||||||
channels
|
channels
|
||||||
django-bootstrap-form==3.2.1
|
django-bootstrap-form==3.2.1
|
||||||
django-widget-tweaks
|
django-widget-tweaks
|
||||||
|
django-notifications
|
||||||
|
django-contrib-comments
|
||||||
|
|
||||||
# Production specific
|
# Production specific
|
||||||
daphne
|
daphne
|
||||||
|
|
Loading…
Reference in a new issue