diff --git a/communication/models.py b/communication/models.py index c0d19a0..220d29d 100644 --- a/communication/models.py +++ b/communication/models.py @@ -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 diff --git a/event/models.py b/event/models.py index c1c89ce..d0e4f94 100644 --- a/event/models.py +++ b/event/models.py @@ -3,15 +3,10 @@ from django.utils.translation import ugettext_lazy as _ from django.core.validators import RegexValidator from django.core.exceptions import FieldError from django.db import models -from communication.models import Commentable, Notifying +from communication.models import SubscriptionMixin -class Event(Commentable): - commentable_model = models.OneToOneField( - Commentable, - related_name="event_model", - parent_link=True - ) +class Event(SubscriptionMixin, models.Model): title = models.CharField( _("nom de l'évènement"), max_length=200, @@ -19,7 +14,6 @@ class Event(Commentable): slug = models.SlugField( _('identificateur'), unique=True, - primary_key=True, help_text=_("Seulement des lettres, des chiffres ou" "les caractères '_' ou '-'."), ) @@ -36,7 +30,8 @@ class Event(Commentable): beginning_date = models.DateTimeField(_('date de début')) ending_date = models.DateTimeField( _('date de fin'), - blank=True + blank=True, + null=True, ) class Meta: @@ -93,7 +88,7 @@ class ActivityTag(models.Model): return self.name -class ActivityTemplate(Commentable): +class ActivityTemplate(SubscriptionMixin, models.Model): title = models.CharField( _("nom de l'activité"), max_length=200, diff --git a/requirements.txt b/requirements.txt index 54d9b30..41fb065 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,8 @@ Pillow channels django-bootstrap-form==3.2.1 django-widget-tweaks +django-notifications +django-contrib-comments # Production specific daphne