Beispiel #1
0
    def createMessage(self,
                      project_id,
                      title,
                      category_id,
                      body='',
                      extended_body='',
                      private=0,
                      notifiers=[],
                      attachments=[],
                      milestone_id=None):
        """Create message
        
        REST: POST /projects/#{project_id}/posts.xml

        Creates a new message, optionally sending notifications
        to a selected list of people. Note that you can also upload
        files using this function, but you need to upload the files
        first and then attach them.
        
        XML Request:
            <request>
                <post>
                    <category-id>#{category_id}</category-id>
                    <title>#{title}</title>
                    <body>#{body}</body>
                    <extended-body>#{extended_body}</extended-body>
                    <private>1</private> <!-- only for firm employees -->
                </post>
                <notify>#{person_id}</notify>
                <notify>#{person_id}</notify>
                ...
                <attachments>
                    <name>#{name}</name> <!-- optional -->
                    <file>
                        <file>#{temp_id}</file> <!-- the id of the previously uploaded file -->
                        <content-type>#{content_type}</content-type>
                        <original_filename>#{original_filename}</original-filename>
                    </file>
                </attachments>
                <attachments>...</attachments>
                ...
            </request>

        Response:
            Returns HTTP status code 201 ("Created") on success, with the
            Location header set to the "Get message" URL for the new message.
            The new message ID can be extracted from that URL. On failure,
            a non-200 status code will be returned, possibly with error
            information in XML format as the response's content.
        """
        # ensure that we got correct input
        assert isinstance(project_id, int)
        assert isinstance(category_id, int)
        assert isinstance(title, str)
        if milestone_id is not None:
            assert isinstance(milestone_id, int)

        path = '/projects/%d/posts.xml' % project_id
        message = Message(category_id=category_id,
                          title=title,
                          project_id=project_id,
                          private=private,
                          body=body,
                          extended_body=extended_body)
        if milestone_id is not None:
            message.milestone_id = milestone_id

        notifiers = ''.join(['<notify>%d</notify>' % id for id in notifiers])
        attachments = ''.join(
            [attachment.serialize() for attachments in attachments])
        body = """<request>%s%s%s</request>""" % (message.serialize(),
                                                  notifiers, attachments)

        response = self.post(path, data=body)

        # successfuly created entry
        if response.status == 201:
            message.id = int(
                dict(response.headers)['location'].split('/')[-1][:-4])
            return message
        return self.getErrors(response.contents)
Beispiel #2
0
        # TODO: this is not working either, ForbiddenError is raised
        rootElement = self.fromXML(
            self.get('/projects/%d/post_categories' % projects[0].id).contents)
        categories = rootElement.getElementsByTagName('post-category')
        if not len(categories
                   ) > 0:  # can't do anything if we have not categories yet
            return None
        else:
            category = Category.load(categories[0])

        # create message using legacy API, cause new REST based API
        # won't return any useful information in it's response
        path = '/projects/%d/msg/create' % projects[0].id
        message = Message(
            category_id=category.id,
            title='temporary message to take login for client person')
        response = self.post(path,
                             data="""<request>%s</request>""" %
                             message.serialize())
        if response.status == 201:  # successfuly created entry
            message_id = int(
                dict(response.headers)['location'].split('/')[-1][:-4])
            self.destroyMessage(message_id)
            return message
        else:
            return None

    # Message API Calls

    def createMessage(self,