示例#1
0
def send_message_mp(
        author,
        n_topic,
        text,
        send_by_mail=True,
        direct=False):
    """
    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.save()

    n_topic.last_message = post
    n_topic.save()

    # send email
    if send_by_mail:
        if direct:
            subject = u"{} : {}".format(settings.ZDS_APP['site']['litteral_name'], n_topic.title)
            from_email = u"{} <{}>".format(settings.ZDS_APP['site']['litteral_name'],
                                           settings.ZDS_APP['site']['email_noreply'])
            for part in n_topic.participants.all():
                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, [part.email])
                msg.attach_alternative(message_html, "text/html")
                try:
                    msg.send()
                except:
                    msg = None
        else:
            for part in n_topic.participants.all():
                send_email(author, n_topic, part, pos)

            send_email(author, n_topic, n_topic.author, pos)

    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_mp(author,
            users,
            title,
            subtitle,
            text,
            send_by_mail=True,
            leave=True,
            direct=False):
    """Send MP at members."""

    # Creating the thread
    n_topic = PrivateTopic()
    n_topic.title = title[:80]
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.save()

    # Add all participants on the MP.
    for part in users:
        n_topic.participants.add(part)

    # Addi 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 = 1
    post.save()

    n_topic.last_message = post
    n_topic.save()

    # send email
    if send_by_mail:
        if direct:
            subject = "ZDS : " + n_topic.title
            from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY)
            for part in users:
                message_html = get_template('email/mp/direct.html').render(
                    Context({'msg': emarkdown(text)}))
                message_txt = get_template('email/mp/direct.txt').render(
                    Context({'msg': text}))

                msg = EmailMultiAlternatives(subject, message_txt, from_email,
                                             [part.email])
                msg.attach_alternative(message_html, "text/html")
                try:
                    msg.send()
                except:
                    msg = None
        else:
            subject = "ZDS - MP: " + n_topic.title
            from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY)
            for part in users:
                message_html = get_template('email/mp/new.html').render(
                    Context({
                        'username':
                        part.username,
                        'url':
                        settings.SITE_URL + n_topic.get_absolute_url(),
                        'author':
                        author.username
                    }))
                message_txt = get_template('email/mp/new.txt').render(
                    Context({
                        'username':
                        part.username,
                        'url':
                        settings.SITE_URL + n_topic.get_absolute_url(),
                        'author':
                        author.username
                    }))

                msg = EmailMultiAlternatives(subject, message_txt, from_email,
                                             [part.email])
                msg.attach_alternative(message_html, "text/html")
                try:
                    msg.send()
                except:
                    msg = None
    if leave:
        move = n_topic.participants.first()
        n_topic.author = move
        n_topic.participants.remove(move)
        n_topic.save()

    return n_topic
示例#4
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
示例#5
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
示例#6
0
文件: mps.py 项目: Aer-o/zds-site
def send_mp(
        author,
        users,
        title,
        subtitle,
        text,
        send_by_mail=True,
        leave=True,
        direct=False):
    """Send MP at members."""

    # Creating the thread
    n_topic = PrivateTopic()
    n_topic.title = title[:80]
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.save()

    # Add all participants on the MP.
    for part in users:
        n_topic.participants.add(part)

    # Addi 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 = 1
    post.save()

    n_topic.last_message = post
    n_topic.save()

    # send email
    if send_by_mail:
        if direct:
            subject = "ZDS : " + n_topic.title
            from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY)
            for part in users:
                message_html = get_template('email/mp/direct.html').render(
                    Context({
                        'msg': emarkdown(text)
                    })
                )
                message_txt = get_template('email/mp/direct.txt').render(
                    Context({
                        'msg': text
                    })
                )

                msg = EmailMultiAlternatives(
                    subject, message_txt, from_email, [
                        part.email])
                msg.attach_alternative(message_html, "text/html")
                try:
                    msg.send()
                except:
                    msg = None
        else:
            subject = "ZDS - MP: " + n_topic.title
            from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY)
            for part in users:
                message_html = get_template('email/mp/new.html').render(
                    Context({
                        'username': part.username,
                        'url': settings.SITE_URL + n_topic.get_absolute_url(),
                        'author': author.username
                    })
                )
                message_txt = get_template('email/mp/new.txt').render(
                    Context({
                        'username': part.username,
                        'url': settings.SITE_URL + n_topic.get_absolute_url(),
                        'author': author.username
                    })
                )

                msg = EmailMultiAlternatives(
                    subject, message_txt, from_email, [
                        part.email])
                msg.attach_alternative(message_html, "text/html")
                try:
                    msg.send()
                except:
                    msg = None
    if leave:
        move = n_topic.participants.first()
        n_topic.author = move
        n_topic.participants.remove(move)
        n_topic.save()

    return n_topic