Exemplo n.º 1
0
    def index(self):
        md5_list = request.POST.getall("md5[]")
        mime_type_list = request.POST.getall("mime-type[]")
        file_name_list = request.POST.getall("filename[]")

        message_text = request.str_POST['Mail']
        message = GroupMailingListMessage.parseMessage(message_text)
        group = message.getGroup()
        author = message.getAuthor()

        if group is None:
            return "Silent bounce!"

        if not group.mailinglist_enabled:
            if author is not None and group.is_member(author):
                meta.set_active_user(author.id)
                request.environ['repoze.who.identity'] = author.id
                if not group.forum_categories:
                    return 'Ok!'
                post = make_forum_post(author,
                                       message.getSubject(),
                                       message.getBody(),
                                       group.id,
                                       category_id=group.forum_categories[0].id,
                                       thread_id=None,
                                       controller='forum')
                meta.Session.add(post)
                meta.Session.commit()
                return 'Ok!'
            return "Silent bounce!"

        try:
            message = GroupMailingListMessage.fromMessageText(message_text)
        except MessageAlreadyExists:
            return "Ok!"

        if message is None:
            return "Silent bounce!"

        if message.author is not None:
            meta.set_active_user(message.author.id)
            request.environ['repoze.who.identity'] = message.author.id
        else:
            meta.set_active_user('')

        meta.Session.add(message)

        meta.Session.commit() # to keep message and attachment ids stable
        attachments = []
        for md5, mimetype, filename in zip(md5_list,
                                           mime_type_list,
                                           file_name_list):
            if message.author is not None:
                meta.set_active_user(message.author.id)
                request.environ['repoze.who.identity'] = message.author.id
            else:
                meta.set_active_user('')

            # XXX we are not filtering nonsense files like small
            # images, pgp signatures, vcards and html bodies yet.
            f = File(filename,
                     filename,
                     mimetype=mimetype,
                     md5=md5)
            f.parent = message
            meta.Session.add(f)
            attachments.append(f)
            meta.Session.commit() # to keep attachment ids stable

        message.attachments.extend(attachments)

        if not message.in_moderation_queue:
            # only send emails for messages that don't have to be
            # moderated first
            self._queueMessage(message)

        meta.Session.commit()
        # Only send actual emails if commit succeeds
        self._sendQueuedMessages()
        return "Ok!"