notify talk edition

This commit is contained in:
Élie Bouttier 2016-07-06 18:19:00 +02:00
parent 92cacd30a7
commit eb0cf5010a
3 changed files with 29 additions and 12 deletions

View File

@ -4,7 +4,7 @@ from django.dispatch import receiver
from accounts.models import Participation
from proposals.models import Talk
from proposals.signals import new_talk
from proposals.signals import *
from .models import ConversationAboutTalk, ConversationWithParticipant, Message
@ -25,20 +25,31 @@ def create_conversation_about_talk(sender, instance, created, **kwargs):
conversation.save()
@receiver(new_talk, dispatch_uid="Notify new talk")
def notify_new_talk(sender, instance, **kwargs):
def check_talk(talk):
# Subscribe reviewer for these topics to conversations
reviewers = User.objects.filter(participation__topic__talk=instance)
instance.conversation.subscribers.add(*reviewers)
for user in instance.speakers.all():
participation = Participation.on_site.get(user=user)
reviewers = User.objects.filter(participation__topic__talk=talk)
talk.conversation.subscribers.add(*reviewers)
for user in talk.speakers.all():
participation, created = Participation.on_site.get_or_create(user=user, site=talk.site)
participation.conversation.subscribers.add(*reviewers)
# Notification of this new talk
message = Message(conversation=instance.conversation, author=instance.proposer,
@receiver(talk_added, dispatch_uid="Notify talk added")
def notify_talk_added(sender, instance, author, **kwargs):
check_talk(instance)
message = Message(conversation=instance.conversation, author=author,
content='The talk has been proposed.')
message.save()
@receiver(talk_edited, dispatch_uid="Notify talk edited")
def notify_talk_edited(sender, instance, author, **kwargs):
check_talk(instance)
message = Message(conversation=instance.conversation, author=author,
content='The talk has been modified.')
message.save()
@receiver(post_save, sender=Message, dispatch_uid="Notify new message")
def notify_new_message(sender, instance, created, **kwargs):
if not created:

View File

@ -1,3 +1,8 @@
from django.dispatch import Signal
new_talk = Signal(providing_args=["sender", "instance"])
__all__ = [ 'talk_added', 'talk_edited' ]
talk_added = Signal(providing_args=["sender", "instance", "author"])
talk_edited = Signal(providing_args=["sender", "instance", "author"])

View File

@ -15,8 +15,8 @@ from accounts.utils import is_orga
from .forms import TalkForm, TopicForm, TopicOrgaForm
from .models import Talk, Topic, Vote
from .signals import new_talk
from .utils import allowed_talks
from .signals import *
def home(request):
@ -53,12 +53,13 @@ def talk_edit(request, talk=None):
if request.method == 'POST' and form.is_valid():
if hasattr(talk, 'id'):
talk = form.save()
talk_edited.send(talk.__class__, instance=talk, author=request.user)
messages.success(request, 'Talk modified successfully!')
else:
form.instance.site = get_current_site(request)
form.instance.proposer = request.user
talk = form.save()
new_talk.send(talk.__class__, instance=talk)
talk_added.send(talk.__class__, instance=talk, author=request.user)
messages.success(request, 'Talk proposed successfully!')
return redirect(talk.get_absolute_url())
return render(request, 'proposals/talk_edit.html', {