示例#1
0
def send_mp(
    author,
    users,
    title,
    subtitle,
    text,
    send_by_mail=True,
    leave=True,
    direct=False,
    mark_as_read=False,
    hat=None,
    automatically_read=None,
):
    """
    Send a private message in a new private topic.

    :param author: sender of the message and author of the private topic
    :param users: list of users receiving the message (participants of the private topic)
    :param title: title of the private topic
    :param subtitle: subtitle of the private topic
    :param text: content of the private message
    :param send_by_mail:
    :param direct: send a mail directly without mp (ex : ban members who wont connect again)
    :param leave: if True, do not add the sender to the topic
    :param mark_as_read:
    :param hat: hat with which to send the private message
    :param automatically_read: a user or a list of users that will automatically be marked as having read of the mp
    :raise UnreachableUserError:
    """

    # Creating the thread
    limit = PrivateTopic._meta.get_field("title").max_length
    n_topic = PrivateTopic()
    n_topic.title = title[:limit]
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.save()

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

    topic = send_message_mp(author, n_topic, text, send_by_mail, direct, hat)
    if mark_as_read:
        mark_read(topic, author)
    if automatically_read:
        if not isinstance(automatically_read, list):
            automatically_read = [automatically_read]
        for not_notified_user in automatically_read:
            mark_read(n_topic, not_notified_user)
    if leave:
        topic.remove_participant(topic.author)
        topic.save()

    return topic
示例#2
0
文件: mps.py 项目: sylafrs/zds-site
def send_mp(author,
            users,
            title,
            subtitle,
            text,
            send_by_mail=True,
            leave=True,
            direct=False,
            mark_as_read=False,
            hat=None,
            automaticaly_read=None):
    """
    Send MP at members.
    Most of the param are obvious, excepted :

    :param direct: send a mail directly without mp (ex : ban members who wont connect again)
    :param leave: the author leave the conversation (usefull for the bot : it wont read the response a member could
    send)
    :param automaticaly_read: a user or a list of users that will automatically be marked as reader of the mp
    """

    # Creating the thread
    limit = PrivateTopic._meta.get_field('title').max_length
    n_topic = PrivateTopic()
    n_topic.title = title[:limit]
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.save()

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

    topic = send_message_mp(author, n_topic, text, send_by_mail, direct, hat)
    if mark_as_read:
        mark_read(topic, author)
    if automaticaly_read:
        if not isinstance(automaticaly_read, list):
            automaticaly_read = [automaticaly_read]
        for not_notified_user in automaticaly_read:
            mark_read(n_topic, not_notified_user)
    if leave:
        move = topic.participants.first()
        topic.author = move
        topic.participants.remove(move)
        topic.save()

    return topic
示例#3
0
def send_mp(
    author,
    users,
    title,
    subtitle,
    text,
    send_by_mail=True,
    leave=True,
    direct=False,
    hat=None,
    automatically_read=None,
):
    """
    Send a private message in a new private topic.

    :param author: sender of the message and author of the private topic
    :param users: list of users receiving the message (participants of the private topic)
    :param title: title of the private topic
    :param subtitle: subtitle of the private topic
    :param text: content of the private message
    :param send_by_mail:
    :param direct: send a mail directly without mp (ex : ban members who wont connect again)
    :param leave: if True, do not add the sender to the topic
    :param hat: hat with which to send the private message
    :param automatically_read: a user or a list of users that will automatically be marked as having read of the mp
    :raise UnreachableUserError:
    """

    n_topic = PrivateTopic.create(title=title,
                                  subtitle=subtitle,
                                  author=author,
                                  recipients=users)
    signals.topic_created.send(sender=PrivateTopic,
                               topic=n_topic,
                               by_email=send_by_mail)

    topic = send_message_mp(author, n_topic, text, send_by_mail, direct, hat)

    if automatically_read:
        if not isinstance(automatically_read, list):
            automatically_read = [automatically_read]
        for not_notified_user in automatically_read:
            mark_read(n_topic, not_notified_user)
    if leave:
        topic.remove_participant(topic.author)
        topic.save()

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

    # Creating the thread
    limit = PrivateTopic._meta.get_field('title').max_length
    n_topic = PrivateTopic()
    n_topic.title = title[:limit]
    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)

    topic = send_message_mp(author, n_topic, text, send_by_mail, direct)
    if mark_as_read:
        mark_read(topic, author)

    if leave:
        move = topic.participants.first()
        topic.author = move
        topic.participants.remove(move)
        topic.save()

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

    # Creating the thread
    limit = PrivateTopic._meta.get_field('title').max_length
    n_topic = PrivateTopic()
    n_topic.title = title[:limit]
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.save()

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

    topic = send_message_mp(author, n_topic, text, send_by_mail, direct, hat)
    if mark_as_read:
        mark_read(topic, author)

    if leave:
        move = topic.participants.first()
        topic.author = move
        topic.participants.remove(move)
        topic.save()

    return topic
示例#6
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
示例#7
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