Ejemplo n.º 1
0
    def _command(self, command='issues', command_url='issues.xml', **kwargs):

        force = kwargs.get('force', False)
        limit = kwargs.get('limit', 100)
        offset = kwargs.get('offset', 0)
        sort = kwargs.get('sort', 'id:desc')
        filter = kwargs.get('filter', None)
        project = kwargs.get('project', None)
        object_id = kwargs.get('object_id', None)
        me_only = kwargs.get('me_only', False)
        element_filter = kwargs.get('element', command)
        includes = kwargs.get('includes', None)

        params = []

        if includes is not None:
            params += (('include', includes),)

        if offset is not None:
            params += (('offset', offset),)

        if sort is not None:
            params += (('sort', sort),)

        if limit is not None:
            params += (('limit', limit),)

        if project:
            params += (('project_id', self.aliases.get(project, project)),)

        if me_only:
            params += (('assigned_to_id', self.me),)

        params_key = '-'.join(['%s%s' % (p[0], p[1]) for p in params])
        params_key += 'filter%s' % filter
        params_key += 'object_id%s' % object_id
        key = 'command%sparams%s' % (command, params_key)

        data = RedmineData(key, url=self.url, command=command)

        if not force:
            if data.load():
                return (True, data)

        rm = Redmine(self.url, key=self.key)

        try:
            # As of Feb 2013 the pyredmine library has finally had a bunch of updates
            # - including cacheing! The response from RedMine.open is now a string rather
            # than a raw ElementTree instance so must convert now.
            issues = ET.fromstring(rm.open(command_url, parms=params)).findall(element_filter)
        except urllib2.HTTPError:
            return (False, 'There was an HTTP error - do you have permission to access?')

        issue_data = list()

        for issue in issues:
            issue_id = issue.find('id').text

            if command == 'projects':
                name = issue.find('identifier').text
            elif command == 'issue_statuses':
                name = issue.find('name').text
            else:
                name = issue.find('project').get('name')

            try:
                description = issue.find('description').text
            except AttributeError:
                description = 'N/A'

            try:
                subject = issue.find('subject').text
            except AttributeError:
                subject = 'N/A'

            history = list()
            if command == 'issues' and object_id:
                journals = issue.find('journals').findall('journal')
                for j in journals:
                    history.append([
                        j.find('user').get('name'),
                        j.find('created_on').text,
                        j.find('notes').text,
                    ])

            if filter is None or (filter in subject.lower() or filter in name.lower()):
                issue_data.append((issue_id, name, subject.replace('\n', ''), description, history))

        data.cache(issue_data)
        return (True, data)