示例#1
0
def send_message_mp(author,
                    n_topic,
                    text,
                    send_by_mail=True,
                    direct=False,
                    hat=None):
    """
    Send a post in an MP.
    Most of the param are obvious, excepted :
    * direct : send a mail directly without mp (ex : ban members who wont connect again)
    * leave : the author leave the conversation (usefull for the bot : it wont read the response a member could send)
    """

    # Getting the position of the post
    if n_topic.last_message is None:
        pos = 1
    else:
        pos = n_topic.last_message.position_in_topic + 1

    # Add the first message
    post = PrivatePost()
    post.privatetopic = n_topic
    post.author = author
    post.text = text
    post.text_html = emarkdown(text)
    post.pubdate = datetime.now()
    post.position_in_topic = pos
    post.hat = hat
    post.save()

    n_topic.last_message = post
    n_topic.save()

    if not direct:
        signals.new_content.send(sender=post.__class__,
                                 instance=post,
                                 by_email=send_by_mail)

    if send_by_mail and direct:
        subject = '{} : {}'.format(settings.ZDS_APP['site']['literal_name'],
                                   n_topic.title)
        from_email = '{} <{}>'.format(
            settings.ZDS_APP['site']['literal_name'],
            settings.ZDS_APP['site']['email_noreply'])
        for recipient in n_topic.participants.values_list('email', flat=True):
            message_html = render_to_string('email/direct.html',
                                            {'msg': emarkdown(text)})
            message_txt = render_to_string('email/direct.txt', {'msg': text})

            msg = EmailMultiAlternatives(subject, message_txt, from_email,
                                         [recipient])
            msg.attach_alternative(message_html, 'text/html')
            try:
                msg.send()
            except Exception as e:
                logger.exception('Message was not sent to %s due to %s',
                                 recipient, e)

    return n_topic
示例#2
0
文件: mps.py 项目: josephcab/zds-site
def send_message_mp(
        author,
        n_topic,
        text,
        send_by_mail=True,
        direct=False,
        hat=None):
    """
    Send a post in an MP.
    Most of the param are obvious, excepted :
    * direct : send a mail directly without mp (ex : ban members who wont connect again)
    * leave : the author leave the conversation (usefull for the bot : it wont read the response a member could send)
    """

    # Getting the position of the post
    if n_topic.last_message is None:
        pos = 1
    else:
        pos = n_topic.last_message.position_in_topic + 1

    # Add the first message
    post = PrivatePost()
    post.privatetopic = n_topic
    post.author = author
    post.text = text
    post.text_html = emarkdown(text)
    post.pubdate = datetime.now()
    post.position_in_topic = pos
    post.hat = hat
    post.save()

    n_topic.last_message = post
    n_topic.save()

    if not direct:
        signals.new_content.send(sender=post.__class__, instance=post, by_email=send_by_mail)

    if send_by_mail and direct:
        subject = '{} : {}'.format(settings.ZDS_APP['site']['literal_name'], n_topic.title)
        from_email = '{} <{}>'.format(settings.ZDS_APP['site']['literal_name'],
                                      settings.ZDS_APP['site']['email_noreply'])
        for recipient in n_topic.participants.values_list('email', flat=True):
            message_html = render_to_string('email/direct.html', {'msg': emarkdown(text)})
            message_txt = render_to_string('email/direct.txt', {'msg': text})

            msg = EmailMultiAlternatives(subject, message_txt, from_email, [recipient])
            msg.attach_alternative(message_html, 'text/html')
            try:
                msg.send()
            except Exception as e:
                logger.exception('Message was not sent to %s due to %s', recipient, e)

    return n_topic
示例#3
0
def send_message_mp(author,
                    n_topic,
                    text,
                    send_by_mail=True,
                    direct=False,
                    hat=None,
                    no_notification_for=None):
    """
    Send a post in an MP.

    :param author: sender of the private message
    :param n_topic: topic in which it will be sent
    :param text: content of the message
    :param send_by_mail: if True, also notify by email
    :param direct: send a mail directly without private message (ex : banned members who won't connect again)
    :param hat: hat attached to the message
    :param no_notification_for: list of participants who won't be notified of the message
    """

    # Getting the position of the post
    if n_topic.last_message is None:
        pos = 1
    else:
        pos = n_topic.last_message.position_in_topic + 1

    # Add the first message
    post = PrivatePost()
    post.privatetopic = n_topic
    post.author = author
    post.text = text
    post.text_html = emarkdown(text)
    post.pubdate = datetime.now()
    post.position_in_topic = pos
    post.hat = hat
    post.save()

    n_topic.last_message = post
    n_topic.save()

    if not direct:
        signals.message_added.send(sender=post.__class__,
                                   post=post,
                                   by_email=send_by_mail,
                                   no_notification_for=no_notification_for)

    if send_by_mail and direct:
        subject = "{} : {}".format(settings.ZDS_APP["site"]["literal_name"],
                                   n_topic.title)
        from_email = "{} <{}>".format(
            settings.ZDS_APP["site"]["literal_name"],
            settings.ZDS_APP["site"]["email_noreply"])
        for recipient in n_topic.participants.values_list("email", flat=True):
            message_html = render_to_string("email/direct.html",
                                            {"msg": emarkdown(text)})
            message_txt = render_to_string("email/direct.txt", {"msg": text})

            msg = EmailMultiAlternatives(subject, message_txt, from_email,
                                         [recipient])
            msg.attach_alternative(message_html, "text/html")
            try:
                msg.send()
            except Exception as e:
                logger.exception("Message was not sent to %s due to %s",
                                 recipient, e)
    if no_notification_for:
        if not isinstance(no_notification_for, list):
            no_notification_for = [no_notification_for]
        for not_notified_user in no_notification_for:
            mark_read(n_topic, not_notified_user)

    # There's no need to inform of the new participant
    # because participants are already notified through the `message_added` signal.
    # If we tried to add the bot, that's fine (a better solution would be welcome though)
    with suppress(NotReachableError):
        n_topic.add_participant(author, silent=True)
        n_topic.save()

    return n_topic