Beispiel #1
0
    def createTimeEntryForProject(self,
                                  project_id,
                                  hours='',
                                  date=None,
                                  person_id=None,
                                  description=''):
        """Create entry (for a given project)
        
        REST: POST /projects/#{project_id}/time_entries.xml
        
        Creates a new time entry for the given todo item.
        
        XML Request:
            <time-entry>
                <person-id>#{person-id}</person-id>
                <date>#{date}</date>
                <hours>#{hours}</hours>
                <description>#{description}</description>
            </time-entry>

        Response:
            Returns HTTP status code 201 (Created) on success, with the
            Location header set to the URL of the new time entry. The
            integer ID of the entry may be extracted from that URL.
        """
        # ensure that we got numerical project id
        assert isinstance(project_id, int)

        path = '/projects/%d/time_entries.xml' % project_id
        # normalize all data for entry
        if date is None:
            # add current date
            date = time.strftime('%Y-%m-%d')
        # don't know how to get id of authenticated user
        if person_id is None:
            # add authenticated user
            person = self.getAuthenticatedPerson()
            if person is not None:
                person_id = person.id

        entry = TimeEntry(person_id=person_id,
                          project_id=project_id,
                          date=date,
                          hours=hours,
                          description=description)
        response = self.post(path, data=entry.serialize())

        # successfuly created entry
        if response.status == 201:
            entry.id = int(dict(response.headers)['location'].split('/')[-1])
            return entry
        return self.getErrors(response.contents)