示例#1
0
def pm_write(sender, recipient, subject, body='', skip_notification=False,
        auto_archive=False, auto_delete=False, auto_moderators=None):
    """
    Write a message to a User.
    Contrary to pm_broadcast(), the message is archived and/or deleted on
    the sender side only if requested.
    The message may come from an untrusted application, a gateway for example,
    so it may be useful to involve some auto moderators in the processing.

    Optional arguments:
        ``skip_notification``: if the normal notification event is not wished
        ``auto_archive``: to mark the message as archived on the sender side
        ``auto_delete``: to mark the message as deleted on the sender side
        ``auto_moderators``: a list of auto-moderation functions
    """
    message = Message(subject=subject, body=body, sender=sender, recipient=recipient)
    initial_status = message.moderation_status
    if auto_moderators:
        message.auto_moderate(auto_moderators)
    else:
        message.moderation_status = STATUS_ACCEPTED
    message.clean_moderation(initial_status)
    if auto_archive:
        message.sender_archived = True
    if auto_delete:
        message.sender_deleted_at = now()
    message.save()
    if not skip_notification:
        message.notify_users(initial_status, _get_site())
示例#2
0
def create_message(sender, recipient, subject, body='', skip_notification=False, auto_archive=False, auto_delete=False, auto_moderators=None, sent_at=None):
    if not user_is_valid(sender):
        raise InvalidSenderException()
    if not user_is_valid(recipient):
        raise InvalidRecipientException()
    if subject is None or subject == '':
        raise NoMessageSubjectException('this message has an empty subject line. Messages without subject cannot be sent')
    if body is None or body == '':
        raise NoMessageBodyException('this message has an empty message body. Empty message cannot be sent')
    message = Message(subject=subject, body=body, sender=sender, recipient=recipient)
    if not sent_at is None:
        message.sent_at = sent_at
    initial_status = message.moderation_status
    if auto_moderators:
        message.auto_moderate(auto_moderators)
    else:
        message.moderation_status = STATUS_ACCEPTED
    message.clean_moderation(initial_status)
    if auto_archive:
        message.sender_archived = True
    if auto_delete:
        message.sender_deleted_at = now()
    message.save()
    if not skip_notification:
        message.notify_users(initial_status, _get_site())
    return message
示例#3
0
def pm_write_corp(sender, recipient, subject, body='', msgType=None,auto_moderators=None):
    if auto_moderators == None:
        auto_moderators= [mod1]
    else:
        auto_moderators.append(mod1)
        
    message = Message(subject=subject, body=body, sender=sender, recipient=recipient)
    initial_status = message.moderation_status

    if auto_moderators:
        message.auto_moderate(auto_moderators)
    else:
        message.moderation_status = STATUS_ACCEPTED
    message.clean_moderation(initial_status)
    message.save()
    message.notify_users(initial_status,msgType=msgType)
    def create(self,
               validated_data,
               recipient=None,
               parent=None,
               reply_all=None,
               auto_moderators=[]):
        """
        Save as many messages as there are recipients.

        Additional actions:
        - If it's a reply, build a conversation
        - Call auto-moderators
        - Notify parties if needed

        Return False if one of the messages is rejected.

        """

        if validated_data and 'recipients' in validated_data:  # action is "create"
            recipients = validated_data['recipients']
            new_messsage = DmMessage(subject=validated_data['subject'],
                                     body=validated_data['body'],
                                     sender=self.sender,
                                     moderation_status=STATUS_ACCEPTED)
        elif parent:  # action is "reply"
            if reply_all:
                recipients = validated_data['recipients']
            else:
                recipients = parent.sender
            quoted = parent.quote(*(format_subject, format_body))
            # print quoted, parent.subject
            sbjct = validated_data['subject'] if (
                validated_data
                and 'subject' in validated_data) else quoted['subject']
            # bdy = validated_data['body'] if (validated_data and 'body' in validated_data) else format_body(parent.sender, parent.body)
            new_messsage = DmMessage(subject=sbjct,
                                     body=validated_data['body'],
                                     sender=self.sender,
                                     moderation_status=STATUS_ACCEPTED)

        # print type(recipients), new_messsage.subject, new_messsage.body

        if parent and not parent.thread_id:  # at the very first reply, make it a conversation
            parent.thread = parent
            parent.save()
            # but delay the setting of parent.replied_at to the moderation step
        if parent:
            new_messsage.parent = parent
            new_messsage.thread_id = parent.thread_id

        initial_moderation = new_messsage.get_moderation()
        initial_dates = new_messsage.get_dates()
        initial_status = new_messsage.moderation_status
        if recipient:
            if isinstance(recipient,
                          get_user_model()) and recipient in recipients:
                recipients.remove(recipient)
            recipients.insert(0, recipient)
        is_successful = True

        if isinstance(recipients, get_user_model()):  # change to list type
            recipients = [recipients]

        for r in recipients:
            usr_model = get_user_model()
            if isinstance(r, get_user_model()):
                new_messsage.recipient = r
            else:
                new_messsage.recipient = usr_model.objects.get(email=r)
            new_messsage.pk = None  # force_insert=True is not accessible from here
            new_messsage.auto_moderate(auto_moderators)
            new_messsage.clean_moderation(initial_status)
            new_messsage.clean_for_visitor()
            m = new_messsage.save()
            if new_messsage.is_rejected():
                is_successful = False
            new_messsage.update_parent(initial_status)
            # new_messsage.notify_users(initial_status, self.site)

            # some resets for next reuse
            if not isinstance(r, get_user_model()):
                new_messsage.email = ''
            new_messsage.set_moderation(*initial_moderation)
            new_messsage.set_dates(*initial_dates)
        return is_successful