Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions redpanal/social/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

from django.db import models
from django.core.mail import send_mail
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.auth.models import User
Expand Down Expand Up @@ -152,9 +153,46 @@ def save(self, *args, **kwargs):
if mentioned_users:
self.mentioned_users.add(*mentioned_users)

send_email_to_related_users(self)


class Meta:
ordering = ['-created_at']

def send_email_to_related_users(msg):
sender = msg.user
recipients = []
# main recipient
obj = msg.content_object # an audio or project or empty
if hasattr(obj, 'user'):
recipients.append(obj.user)
# add mentioned users
recipients.extend(msg.mentioned_users.all())
to_emails = [user.email for user in recipients]
# remove duplicates
to_emails = set(to_emails)
# remove self
to_emails.discard(msg.user.email)

username = (sender.userprofile.realname or "") + " @" + str(sender.username)
email_msg = f"""
The user {username} sent you the following message:

{msg.msg}

You can continue the conversation in https://redpanal.org
"""

if to_emails:
send_mail(
'Hey te escribieron en RedPanal',
email_msg,
from_email=None,
recipient_list=to_emails,
fail_silently=False,
)


def message_created_signal(sender, instance, created, **kwargs):
if created:
action.send(instance.user, verb='commented', action_object=instance)
Expand Down