def save(self, sender, send=True): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] recipients_cleaned = [] for r in recipients: if r.split("_")[0]==u"class": class_id = int(r.split("_")[1]) classroom_object = Classroom.objects.get(id=class_id) trainers_ids = [int(trainer['id']) for trainer in classroom_object.trainers.values()] trainee_ids = [int(trainee.user.id) for trainee in Trainee.objects.filter(classroom_id=class_id)] recipients_cleaned.extend(trainers_ids) recipients_cleaned.extend(trainee_ids) else: user_id = int(r.split("_")[1]) recipients_cleaned.append(user_id) recipients_cleaned = list(set(recipients_cleaned)) recipients = [User.objects.get(pk=key) for key in recipients_cleaned] new_message = Message.objects.create(body=body, sender=sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message, creator=sender) thread.all_msgs.add(new_message) for recipient in recipients: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender) sender_part.replied_at = sender_part.read_at = now() sender_part.save() thread.save() # save this last, since this updates the search index message_composed.send(sender=Message, message=new_message, recipients=recipients) #send notifications print "notification within def save: ", notification if send and notification: if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID: for r in recipients: reply_email = create_reply_email(sendgrid_settings.THREADED_MESSAGES_ID, r, thread) notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender, from_email=reply_email.get_from_email(), headers={'Reply-To': reply_email.get_reply_to_email()}) else: notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender) return (thread, new_message)
def save(self, sender, send=True): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] recipients_cleaned = list(set(recipients)) recipients = [User.objects.get(pk=key) for key in recipients_cleaned] new_message = Message.objects.create(body=body, sender=sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message, creator=sender) thread.all_msgs.add(new_message) for recipient in recipients: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender) sender_part.replied_at = sender_part.read_at = now() sender_part.save() thread.save() # save this last, since this updates the search index message_composed.send(sender=Message, message=new_message, recipients=recipients) #send notifications print "notification within def save: ", notification if send and notification: if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID: for r in recipients: reply_email = create_reply_email( sendgrid_settings.THREADED_MESSAGES_ID, r, thread) notification.send( recipients, "received_email", { "thread": thread, "message": new_message }, sender=sender, from_email=reply_email.get_from_email(), headers={'Reply-To': reply_email.get_reply_to_email()}) else: notification.send(recipients, "received_email", { "thread": thread, "message": new_message }, sender=sender) return (thread, new_message)
def save(self, sender, send=True): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] recipients_cleaned = list(set(recipients)) recipients = [User.objects.get(pk=key) for key in recipients_cleaned] new_message = Message.objects.create(body=body, sender=sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message, creator=sender) thread.all_msgs.add(new_message) for recipient in recipients: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender) sender_part.replied_at = sender_part.read_at = now() sender_part.save() thread.save() # save this last, since this updates the search index message_composed.send(sender=Message, message=new_message, recipients=recipients) #send notifications print "notification within def save: ", notification if send and notification: if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID: for r in recipients: reply_email = create_reply_email(sendgrid_settings.THREADED_MESSAGES_ID, r, thread) notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender, from_email=reply_email.get_from_email(), headers={'Reply-To': reply_email.get_reply_to_email()}) else: notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender) return (thread, new_message)
def save(self, sender, send=True): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] new_message = Message.objects.create(body=body, sender=sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message, creator=sender) thread.all_msgs.add(new_message) for recipient in recipients: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender) sender_part.replied_at = sender_part.read_at = datetime.datetime.now() sender_part.save() thread.save() #save this last, since this updates the search index #send notifications if send and notification: if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID: for r in recipients: reply_email = create_reply_email( sendgrid_settings.THREADED_MESSAGES_ID, r, thread) notification.send(recipients, "received_email", { "thread": thread, "message": new_message }, sender=sender, from_email=reply_email.get_reply_email()) else: notification.send(recipients, "received_email", { "thread": thread, "message": new_message }, sender=sender) return (thread, new_message)
def save(self, sender, send=True): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] new_message = Message.objects.create(body=body, sender=sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message, creator=sender) thread.all_msgs.add(new_message) for recipient in recipients: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender) sender_part.replied_at = sender_part.read_at = datetime.datetime.now() sender_part.save() thread.save() #save this last, since this updates the search index #send notifications if send and notification: if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID: for r in recipients: reply_email = create_reply_email(sendgrid_settings.THREADED_MESSAGES_ID, r, thread) notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender, from_email=reply_email.get_reply_email()) else: notification.send(recipients, "received_email", {"thread": thread, "message": new_message}, sender=sender) return (thread, new_message)